0% found this document useful (0 votes)
5 views65 pages

Lab Programs

The document contains a series of C programming exercises that implement various algorithms and data structures, including linear and binary search, array insertion and deletion, sorting algorithms (insertion, bubble, selection), stack and queue operations using both arrays and linked lists, and linked list operations (insertion, deletion, display). Each section provides a code snippet demonstrating the implementation of the respective algorithm or data structure. The programs are designed to help users understand fundamental programming concepts and data manipulation techniques.

Uploaded by

joel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views65 pages

Lab Programs

The document contains a series of C programming exercises that implement various algorithms and data structures, including linear and binary search, array insertion and deletion, sorting algorithms (insertion, bubble, selection), stack and queue operations using both arrays and linked lists, and linked list operations (insertion, deletion, display). Each section provides a code snippet demonstrating the implementation of the respective algorithm or data structure. The programs are designed to help users understand fundamental programming concepts and data manipulation techniques.

Uploaded by

joel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Lab Programs

1. Write a program to implement linear searching

#include<stdio.h>

void main()

int number[25], n, data, i, flag = 0;

printf("\n Enter the number of elements:");

scanf("%d", &n);

printf("\n Enter the elements:");

for(i = 0; i<n; i++)

scanf("%d", &number[i]);

printf("\n Enter the element to be Searched:");

scanf("%d", &data);

for( i = 0; i<n; i++)

if(number[i] == data)

flag = 1;

break;

if(flag == 1)

printf("\n Data found at location:%d", i+1);

else
printf("\n Data not found");

}
2. Write a program to implement Binary searching

#include<stdio.h>

int main()

int number[25], n, data, i, flag = 0, low, high, mid;

printf("\n Enter the number of elements: ");

scanf("%d", &n);

printf("\n Enter the elements in ascending order: ");

for(i = 0; i < n; i++)

scanf("%d", &number[i]);

printf("\n Enter the element to be searched: ");

scanf("%d", &data);

low = 0;

high = n-1;

while(low <= high)

mid = (low + high)/2;

if(number[mid] == data)

flag = 1;

break;

else

{
if(data < number[mid])

high = mid - 1;

else

low = mid + 1;

} if(flag == 1)

printf("\n Data found at location: %d", mid + 1);

else

printf("\n Data Not Found ");

}
3. Write a program to implement Array insertion

#include<stdio.h>

void main()

int a[10],value,pos,size,i;

printf("\n enter the number of elements in the array");

scanf("%d",&size);

for(i=0;i<size;i++)

scanf("%d",&a[i]);

printf("enter the position to insert the element");

scanf("%d",&pos);

printf("\n enter the value");

scanf("%d",&value);

for(i=size;i>=pos-1;i--)

a[i+1]=a[i];

a[pos-1]=value;

size++;

for(i=0;i<size;i++)

printf("%d\n",a[i]);
}

}
4. Write a program to perform deletion operation in an array

#include<stdio.h>

void main()

int a[10],value,pos,size,i;

printf("\n enter the number of elements in the array");

scanf("%d",&size);

for(i=0;i<size;i++)

scanf("%d",&a[i]);

printf("enter the position to delete the element");

scanf("%d",&pos);

for(i=pos-1;i<=size-1;i++)

a[i]=a[i+1];

size--;

for(i=0;i<size;i++)

printf("%d\n",a[i]);

}
5. Write a program to implement Insertion sort

#include <stdio.h>

int main()

int n, i, j, temp;

int arr[10];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i =0; i<n;i++)

scanf("%d", &arr[i]);

for (i =1; i <n;i++)

temp=arr[i];

j=i-1;

while (j>=0 && arr[j]>temp)

arr[j+1]=arr[j];

j--;

arr[j+1]=temp;
}

printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)

printf("%d\n", arr[i]);

return 0;

}
6. Write a Program to implement bubble sorting

#include<stdio.h>

int main()

int a[10],i,j,temp,n;

printf("\n Enter the max no.of Elements to Sort: \n");

scanf("%d",&n);

printf("\n Enter the Elements : \n");

for(i=0; i<n; i++)

scanf("%d",&a[i]);

for(i=0; i<n-1; i++)

for(j=0; j<n-1; j++)

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}
printf("Sorted list in ascending order:\n");

for(i=0; i<n; i++)

printf("%d\n",a[i]);

return 0;

}
7. Write a Program to implement Selection sort

#include<stdio.h>

int main()

int i, j,n, temp, arr[25];

printf("Enter the number of elements :\n");

scanf("%d",&n);

printf("Enter the elements :\n");

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

for(i=0;i<n;i++){

for(j=i+1;j<n;j++)

if(arr[i]>arr[j])

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

printf("Sorted elements: ");


for(i=0;i<n;i++)

printf(" %d",arr[i]);

return 0;

}
8. Write a program to implement stack operation using array

#include <stdio.h>

int stack[100],i,j,choice=0,n,top=-1;

void push();

void pop();

void show();

void main ()

printf("Enter the number of elements in the stack ");

scanf("%d",&n);

printf("*********Stack operations using array*********");

printf("\n \n");

while(choice != 4)

printf("Chose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:
{

push();

break;

case 2:

pop();

break;

case 3:

show();

break;

case 4:

printf("Exiting... ");

break;

default:

printf("Please Enter valid choice ");

}
};

void push ()

int val;

if (top == n )

printf("\n Overflow");

else

printf("Enter the value?");

scanf("%d",&val);

top = top +1;

stack[top] = val;

void pop ()

if(top == -1)

printf("Underflow");

else

top = top -1;


}

void show()

for (i=top;i>=0;i--)

printf("%d\n",stack[i]);

if(top == -1)

printf("Stack is empty");

}
9. Write a program to implement stack operations using linked list

#include <stdio.h>

#include <stdlib.h>

void push();

void pop();

void display();

struct node

int val;

struct node *next;

}*top,*newnode;

void main ()

int choice=0;

printf("\n*********Stack operations using linked list*********\n");

printf("\n \n");

while(choice != 4)

printf("\n\nChose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);
switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("Exiting... ");

break;

default:
{

printf("Please Enter valid choice ");

void push ()

int val;

newnode = (struct node*)malloc(sizeof(struct node));

printf("Enter the value");

scanf("%d",&val);

if(top==NULL)

newnode->val = val;

newnode-> next = NULL;

top=newnode;

else

newnode->val = val;

newnode->next = top;
top=newnode;

printf("Item pushed");

void pop()

int item;

struct node *temp;

if (top == NULL)

printf("Underflow");

else

temp= top;

top = top->next;

free(temp);

printf("Item popped");

}
}

void display()

int i;

struct node *temp;

temp=top;

if(top == NULL)

printf("Stack is empty\n");

else

printf("Printing Stack elements \n");

while(temp!=NULL)

printf("%d\n",temp->val);

temp = temp->next;

}
10. Write a program to implement queue operations using array

#include<stdio.h>

#include<stdlib.h>

void enqueue();

void dequeue();

void display();

int front = -1, rear = -1,size;

int queue[100];

void main ()

int choice;

printf("enter the size of the queue");

scanf("%d",&size);

while(choice != 4)

printf("\n*************************Main Menu*****************************\n");

printf("\n=================================================================\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");

printf("\nEnter your choice ?");

scanf("%d",&choice);

switch(choice)

case 1:

enqueue();
break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4: exit(0);

break;

default:

printf("\nEnter valid choice??\n");

void enqueue()

int item;

printf("\nEnter the element\n");

scanf("\n%d",&item);

if(rear == size-1)

printf("\nOVERFLOW\n");

return;
}

if(front == -1 && rear == -1)

front = 0;

rear = 0;

else

rear = rear+1;

queue[rear] = item;

printf("\nValue inserted ");

void dequeue()

int item;

if (front == -1 && rear==-1)

printf("\nUNDERFLOW\n");

return;

else if(front==rear)

{
front=rear=-1;

else

front++;

printf("\nvalue deleted ");

void display()

int i;

if(rear == -1)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

for(i=front;i<=rear;i++)

printf("\n%d\n",queue[i]);

}
11. Write a program to implement queue operations using linked list

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

}*front,*rear,*newnode;

void enqueue();

void dequeue();

void display();

void main ()

int choice;

while(choice != 4)

printf("\n*************************Main Menu*****************************\n");

printf("\n===========================================================

======\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");

printf("\nEnter your choice ?");

scanf("%d",& choice);
switch(choice)

case 1:

enqueue();

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nEnter valid choice??\n");

void enqueue()

int item;

newnode=(struct node *) malloc(sizeof(struct node));


printf("\nEnter value?\n");

scanf("%d",&item);

newnode-> data = item;

newnode->next=NULL;

if(front == NULL)

front = rear=newnode

else

rear -> next = newnode;

rear = newnode;

void dequeue ()

struct node *temp;

if(front == NULL)

printf("\nUNDERFLOW\n");

return;

else
{

temp= front;

front = front -> next;

free(temp);

void display()

struct node *temp;

temp= front;

if(front == NULL)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

while(temp!= NULL)

printf("\n%d\n",temp -> data);

temp = temp-> next;

}
12. Write a program to implement the following operations in single linked list
A. Insertion in front
B. Insertion at end
C. Display
D. Creation

#include<stdio.h>

#include<stdlib.h>

void display();

void insertf();

void insertl();

void create();

struct node

int data;

struct node *next;

}*head,*temp,*newnode,*prevnode;

int main()

int c;

printf("menu\n");

printf("\n 1.Insertf 2.Insertl 3.display 4.create 5.exit\n");

do

printf("enter your choice");

scanf("%d",&c);

switch(c)
{

case 1:insertf();

break;

case 2:insertl();

break;

case 3:display();

break;

case 4:create();

break;

case 5:exit(0);

}while(1);

return 0;

void insertf()

int x;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data at begnning");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

if(head==NULL)

head=newnode;
}

else

newnode->next=head;

head=newnode;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

void insertl()

int x;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data at end");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;
if(head==NULL)

head=temp=newnode;

else

temp=head;

while(temp->next!=NULL)

temp=temp->next;

temp->next=newnode;

void create()

{ int x;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data ");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

if(head==NULL)

head=temp=newnode;
}

else

temp->next=newnode;

temp=newnode;

}
13. Write a program to implement the following operations in single linked list
A. Insertion at middle
B. Display
C. Creation

#include<stdio.h>

#include<stdlib.h>

void display();

void insertmid();

void create();

struct node

int data;

struct node *next;

}*head,*temp,*newnode,*prevnode;

int main()

int c;

printf("menu\n");

printf("\n 1.Insertmid 2.display 3.create 4.exit\n");

do

printf("enter your choice");

scanf("%d",&c);

switch(c)

{
case 1:insertmid();

break;

case 2:display();

break;

case 3:create();

break;

case 4:exit(0);

}while(1);

return 0;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

void insertmid()
{

int x,pos;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\n enter the data ");

scanf("%d",&x);

printf("enter the position you want to insert"); scanf("%d",&pos);

newnode->data=x;

newnode->next=NULL;

if(head==NULL)

head=temp=newnode;

else

temp=head;

for(int i=1;i<pos-1;i++)

temp=temp->next;

newnode->next=temp->next;

temp->next=newnode;

void create()
{ int x;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data ");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

if(head==NULL)

head=temp=newnode;

else

temp->next=newnode;

temp=newnode;

}
14. Write a program to implement the following operations in single linked list
A. Deletion in front
B. Deletion at end
C. Creation
D. Display

#include<stdio.h>

#include<stdlib.h>

void display();

void deletefront();

void deletelast();

void create();

struct node

int data;

struct node *next;

}*head,*temp,*newnode,*prevnode;

int main()

int c;

printf("menu\n");

printf("\n 1.deletefront 2.deletelast 3.display 4.create 5.exit\n");

do

printf("enter your choice");

scanf("%d",&c);

switch(c)
{

case 1:deletefront();

break;

case 2:deletelast();

break;

case 3:display();

break;

case 5:exit(0);

case 4:create();

break;

}while(1);

return 0;

void deletefront()

if(head==NULL)

printf("nothing to delete");

else

temp=head;
head=head->next;

free(temp);

printf("The node is deleted\n"); }

void deletelast()

if(head==NULL)

printf("nothing to delete");

else

temp=head;

while(temp->next!=0)

prevnode=temp;

temp=temp->next;

prevnode->next=NULL;

free(temp);

printf("Node is deleted\n");

}
void create()

{ int x;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\n enter the data ");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

if(head==NULL)

head=temp=newnode;

else

temp->next=newnode;

temp=newnode;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

{
printf("%d->",temp->data);

temp=temp->next;

}
15. Write a program to implement the following operations in single linked list
A. Deletion at middle
B. Creation
C. Display

#include<stdio.h>

#include<stdlib.h>

void display();

void deletemid();

void create();

struct node

int data;

struct node *next;

}*head,*temp,*newnode,*prevnode;

int main()

int c;

printf("menu\n");

printf("\n 1.deletemid 2.display 3.create 4.exit\n");

do

printf("enter your choice");

scanf("%d",&c);

switch(c)

{
case 1:deletemid();

break;

case 2:display();

break;

case 4:exit(0);

case 3:create();

break;

}while(1);

return 0;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

void create()
{ int x;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data ");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

if(head==NULL)

head=temp=newnode;

else

temp->next=newnode;

temp=newnode;

void deletemid()

int i,pos;

printf("enter the position to delete"); scanf("%d",&pos);

if(head==NULL)

printf("nothing to delete");

}
else

temp=head;

for(i=1;i<pos;i++)

prevnode=temp;

temp=temp->next;

prevnode->next=temp->next;

free(temp);

printf("node deleted");

}
16. Write a program to implement the following operations in doubly linked list
A. Insertion in front
B. Insertion at end
C. Display
D. Creation

#include<stdio.h>

#include<stdlib.h>

void display();

void insertf();

void insertl();

void createdll();

struct node

struct node *prev;

int data;

struct node *next;

}*head,*temp,*newnode,*tail;

int main()

int c;

printf("menu\n");

printf("\n 1.Insertf 2.Insertl 3.display 4.Exit 5.create \n");

do

printf("enter your choice");

scanf("%d",&c);
switch(c)

case 1:insertf();

break;

case 2:insertl();

break;

case 3:display();

break;

case 4:exit(0);

case 5:createdll();

break;

}while(1);

return 0;

void insertf()

int x;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data at begnning");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL)
{

head=newnode;

else

newnode->next=head;

head->prev=newnode;

head=newnode;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

void insertl()

int x;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data at end");


scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL)

head=newnode;

else

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

void createdll()

int x;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\n enter the data ");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;
newnode->prev=NULL;

if(head==NULL)

head=tail=newnode;

else

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

}
17. Write a program to implement the following operations in doubly linked list
A. Insertion at middle
B. Display
C. Creation

#include<stdio.h>

#include<stdlib.h>

void display();

void insertmid();

void createdll();

struct node

struct node *prev;

int data;

struct node *next;

}*head,*temp,*newnode,*tail;

int main()

int c;

printf("menu\n");

printf("\n 1.Insertmid 2.display 3.Exit 4.create \n");

do

printf("enter your choice");

scanf("%d",&c);

switch(c)
{

case 1:insertmid();

break;

case 2:display();

break;

case 3:exit(0);

case 4:createdll();

break;

}while(1);

return 0;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

}
void createdll()

int x;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\n enter the data ");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL)

head=tail=newnode;

else

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

void insertmid()

int x,pos;

newnode=(struct node*)malloc(sizeof(struct node)); printf("\n enter the data ");


scanf("%d",&x);

printf("enter the position you want to insert"); scanf("%d",&pos);

newnode->data=x;

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL)

head=newnode;

else

temp=head;

for(int i=1;i<pos-1;i++)

temp=temp->next;

newnode->next=temp->next;

newnode->prev=temp;

temp->next=newnode;

newnode->next->prev=newnode;

}
18. Write a program to implement the following operations in doubly linked list
A. Deletion in front
B. Deletion at end
C. Display
D. Creation

#include<stdio.h>

#include<stdlib.h>

void display();

void deletefront();

void deletelast();

void createdll();

struct node

struct node *prev;

int data;

struct node *next;

}*head,*temp,*newnode,*tail;

int main()

int c;

printf("menu\n");

printf("\n 1.deletefront 2.deletelast 3.display 4.Exit 5.create \n");

do

printf("enter your choice");

scanf("%d",&c);
switch(c)

case 1:deletefront();

break;

case 2:deletelast();

break;

case 3:display();

break;

case 4:exit(0);

case 5:createdll();

break;

}while(1);

return 0;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

}
}

void deletefront()

if(head==NULL)

printf("nothing to delete"); }

else

temp=head;

head=head->next;

head->prev=NULL;

free(temp);

printf("The node is deleted\n"); }

void deletelast()

if(tail==NULL)

printf("nothing to delete"); }

else

temp=tail;

tail=tail->prev;

tail->next=NULL;
free(temp);

printf("The node is deleted\n"); }

void createdll()

int x;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\n enter the data ");

scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL)

head=tail=newnode;

else

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

}
19. Write a program to implement the following operations in doubly linked list
A. Deletion at middle
B. Display
C. Creation

#include<stdio.h>

#include<stdlib.h>

void display();

void deletemid();

void createdll();

struct node

struct node *prev;

int data;

struct node *next;

}*head,*temp,*newnode,*tail;

int main()

int c;

printf("menu\n");

printf("\n 1.deletemid 2.display 3.Exit 4.create \n");

do

printf("enter your choice");

scanf("%d",&c);

switch(c)
{

case 1:deletemid();

break;

case 2:display();

break;

case 3:exit(0);

case 4:createdll();

break;

}while(1);

return 0;

void display()

printf("\n The linked list \n");

temp=head;

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->next;

}
void deletemid()

int i,pos;

printf("enter the position to delete"); scanf("%d",&pos);

if(head==NULL)

printf("nothing to delete");

else

temp=head;

for(i=1;i<pos;i++)

temp=temp->next;

temp->next->prev=temp->prev; temp->prev->next=temp->next; free(temp);

printf("node deleted\n");

void createdll()

int x;

newnode=(struct node*)malloc(sizeof(struct node));

printf("\n enter the data ");


scanf("%d",&x);

newnode->data=x;

newnode->next=NULL;

newnode->prev=NULL;

if(head==NULL)

head=tail=newnode;

else

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

You might also like