Lab Programs
Lab Programs
#include<stdio.h>
void main()
scanf("%d", &n);
scanf("%d", &number[i]);
scanf("%d", &data);
if(number[i] == data)
flag = 1;
break;
if(flag == 1)
else
printf("\n Data not found");
}
2. Write a program to implement Binary searching
#include<stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &number[i]);
scanf("%d", &data);
low = 0;
high = n-1;
if(number[mid] == data)
flag = 1;
break;
else
{
if(data < number[mid])
high = mid - 1;
else
low = mid + 1;
} if(flag == 1)
else
}
3. Write a program to implement Array insertion
#include<stdio.h>
void main()
int a[10],value,pos,size,i;
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
scanf("%d",&pos);
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;
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
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];
scanf("%d", &n);
scanf("%d", &arr[i]);
temp=arr[i];
j=i-1;
arr[j+1]=arr[j];
j--;
arr[j+1]=temp;
}
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;
scanf("%d",&n);
scanf("%d",&a[i]);
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");
printf("%d\n",a[i]);
return 0;
}
7. Write a Program to implement Selection sort
#include<stdio.h>
int main()
scanf("%d",&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(" %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 ()
scanf("%d",&n);
printf("\n \n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
{
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
printf("Exiting... ");
break;
default:
}
};
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
scanf("%d",&val);
stack[top] = val;
void pop ()
if(top == -1)
printf("Underflow");
else
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;
}*top,*newnode;
void main ()
int choice=0;
printf("\n \n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting... ");
break;
default:
{
void push ()
int val;
scanf("%d",&val);
if(top==NULL)
newnode->val = val;
top=newnode;
else
newnode->val = val;
newnode->next = top;
top=newnode;
printf("Item pushed");
void pop()
int item;
if (top == NULL)
printf("Underflow");
else
temp= top;
top = top->next;
free(temp);
printf("Item popped");
}
}
void display()
int i;
temp=top;
if(top == NULL)
printf("Stack is empty\n");
else
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 queue[100];
void main ()
int choice;
scanf("%d",&size);
while(choice != 4)
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
scanf("%d",&choice);
switch(choice)
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4: exit(0);
break;
default:
void enqueue()
int item;
scanf("\n%d",&item);
if(rear == size-1)
printf("\nOVERFLOW\n");
return;
}
front = 0;
rear = 0;
else
rear = rear+1;
queue[rear] = item;
void dequeue()
int item;
printf("\nUNDERFLOW\n");
return;
else if(front==rear)
{
front=rear=-1;
else
front++;
void display()
int i;
if(rear == -1)
printf("\nEmpty queue\n");
else
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;
}*front,*rear,*newnode;
void enqueue();
void dequeue();
void display();
void main ()
int choice;
while(choice != 4)
printf("\n*************************Main Menu*****************************\n");
printf("\n===========================================================
======\n");
scanf("%d",& choice);
switch(choice)
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
void enqueue()
int item;
scanf("%d",&item);
newnode->next=NULL;
if(front == NULL)
front = rear=newnode
else
rear = newnode;
void dequeue ()
if(front == NULL)
printf("\nUNDERFLOW\n");
return;
else
{
temp= front;
free(temp);
void display()
temp= front;
if(front == NULL)
printf("\nEmpty queue\n");
else
while(temp!= NULL)
}
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;
}*head,*temp,*newnode,*prevnode;
int main()
int c;
printf("menu\n");
do
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;
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
head=newnode;
}
else
newnode->next=head;
head=newnode;
void display()
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
void insertl()
int x;
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;
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;
}*head,*temp,*newnode,*prevnode;
int main()
int c;
printf("menu\n");
do
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()
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
void insertmid()
{
int x,pos;
scanf("%d",&x);
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;
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;
}*head,*temp,*newnode,*prevnode;
int main()
int c;
printf("menu\n");
do
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);
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;
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
head=temp=newnode;
else
temp->next=newnode;
temp=newnode;
void display()
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;
}*head,*temp,*newnode,*prevnode;
int main()
int c;
printf("menu\n");
do
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()
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
void create()
{ int x;
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;
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
int data;
}*head,*temp,*newnode,*tail;
int main()
int c;
printf("menu\n");
do
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;
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()
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
void insertl()
int 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;
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
int data;
}*head,*temp,*newnode,*tail;
int main()
int c;
printf("menu\n");
do
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()
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
}
void createdll()
int x;
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->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
int data;
}*head,*temp,*newnode,*tail;
int main()
int c;
printf("menu\n");
do
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()
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);
void deletelast()
if(tail==NULL)
printf("nothing to delete"); }
else
temp=tail;
tail=tail->prev;
tail->next=NULL;
free(temp);
void createdll()
int x;
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
int data;
}*head,*temp,*newnode,*tail;
int main()
int c;
printf("menu\n");
do
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()
temp=head;
while(temp!=NULL)
printf("%d->",temp->data);
temp=temp->next;
}
void deletemid()
int i,pos;
if(head==NULL)
printf("nothing to delete");
else
temp=head;
for(i=1;i<pos;i++)
temp=temp->next;
printf("node deleted\n");
void createdll()
int 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;