Program 1
Write a program to implement linear search.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
Program No: 2
Write a program to implement Binary search.
#include <stdio.h>
int main() {
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
else
last = middle - 1;
middle = (first + last)/2;
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0; }
Program No: 3
Write a program to implement Singly Linked List.
#include<stdio.h>
#include<stdlib.h>
struct emp{
int data;
struct emp *next;
};
struct emp *head, *p;
void creation();
void insertion_begin();
void insertion_last();
void display();
void delete_begin();
void main()
{ int choice;
printf("\n\n *************MENU ***************\n\n\n");
printf("\n 1. Creation \n 2. Insertion at beginning \n 3.Insertion at Last \n 4.Display");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice) {
case 1: creation();
break;
case 2:insertion_begin();
break;
case 3: insertion_last();
break;
case 4: display();
break;
default: break;
}// End of switch
}//End of main
void creation() {
int n, i;
printf("enter the no of nodes");
scanf("%d", &n);
head = (struct emp*)malloc(sizeof(struct emp));
head->next = NULL;
printf("enter the data");
scanf("%d", &head->data);
p=head;
for(i=0;i<n-1; i++){
p->next = (struct emp*)malloc(sizeof(struct emp));
p= p->next;
printf("enter data for node");
scanf("%d", &p->data);
p->next=NULL; }
main();
}//End of creation
void insertion_begin(){
int item=0;
p = (struct emp*)malloc(sizeof(struct emp));
if(p == NULL) {
printf("\nOVERFLOW\n"); }
else
{ p->data = item;
p->next = head;
head = p;
printf("\nNode inserted\n");
} main();
}//insertion
void insertion_last() {
struct emp *p,*N;
N = (struct emp*)malloc(sizeof(struct emp));
p=head;
while(p->next != NULL) {
p = p->next; }
p->next=N;
N->next=NULL;
void display() {
p=head;
while(p!=NULL){
printf("\n Node value : %d",p->data);
p=p->next; }
main(); }
void delete_begin() {
p = head;
head = head->next;
free(p); }
void delete_last() {
}
Program No: 4
Write a program to implement Circular Linked List.
#include <stdio.h>
#include <stdlib.h>
struct cll {
int data;
struct cll *next;};
struct cll *head = NULL, *tail = NULL, *newnode, *p, *q;
void create();
void display();
void insert_beg();
void insert_last();
void del_begin();
void del_last();
int main() {
int choice;
do {
printf("\n ********** MENU *********");
printf("\n 1. Creation");
printf("\n 2. Insertion at beginning");
printf("\n 3. Insertion at last");
printf("\n 4. Deletion from beginning");
printf("\n 5. Deletion from last");
printf("\n 6. Display");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: create(); break;
case 2: insert_beg(); break;
case 3: insert_last(); break;
case 4: del_begin(); break;
case 5: del_last(); break;
case 6: display(); break;
default: printf(" Not a valid option\n"); }
} while (choice >= 1 && choice <= 6);
return 0;
void create() {
int num, i;
printf("\n Enter number of nodes: ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
newnode = (struct cll*)malloc(sizeof(struct cll));
if (newnode == NULL) {
printf("\n Memory allocation failed");
return; }
printf("\n Enter data: ");
scanf("%d", &newnode->data);
if (head == NULL) {
head = newnode;
tail = newnode;
newnode->next = head; // Circular link
} else {
tail->next = newnode;
tail = newnode;
tail->next = head; // Circular link}
}
void display() {
struct cll *p = head;
if (head == NULL) {
printf("\n\n List is empty\n");
return;}
do {
printf("\n %d", p->data);
p = p->next;
} while (p != head);
printf("\n");}
void insert_beg() {
newnode = (struct cll*)malloc(sizeof(struct cll));
if (newnode == NULL) {
printf("\n Memory allocation failed");
return;}
printf("\n Enter data to be inserted at the beginning: ");
scanf("%d", &newnode->data);
if (tail == NULL) { // List is empty
head = newnode;
tail = newnode;
tail->next = head; // Circular link
} else {
newnode->next = head;
head = newnode;
tail->next = head; // Update tail's next
void insert_last() {
newnode = (struct cll*)malloc(sizeof(struct cll));
if (newnode == NULL) {
printf("\n Memory allocation failed");
return;}
printf("\n Enter data to be inserted at the last: ");
scanf("%d", &newnode->data);
if (tail == NULL) { // List is empty
head = newnode;
tail = newnode;
tail->next = head; // Circular link
} else {
tail->next = newnode;
tail = newnode;
tail->next = head; // Circular link
void del_begin() {
if (head == NULL) {
printf("\n List is empty, nothing to delete\n");
return;}
p = head;
if (head == tail) { // Only one node
head = NULL;
tail = NULL;
} else {
head = head->next;
tail->next = head; // Update tail's next}
free(p);
printf("\n Node has been deleted from the beginning\n");}
void del_last() {
if (head == NULL) {
printf("\n List is empty, nothing to delete\n");
return;}
p = head;
if (head == tail) { // Only one node
free(head);
head = NULL;
tail = NULL;
} else {
while (p->next != tail) {
p = p->next; // p points to second last node
free(tail);
tail = p;
tail->next = head; // Update tail's next
printf("\n Node has been deleted from the last\n");