External Lab Pgms
External Lab Pgms
5) Linear Search
8) Selection Sort
9) Insertion Sort
1
1) Stack using Array
#include<stdio.h>
#include<stdlib.h>
int top,choice,i,size,item,stack[20];
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("enter the stack size:");
scanf("%d",&size);
printf("\nSTACK OPERATION USING ARRAYS.");
printf("-----------------------------\n\t");
printf("1,.PUSH \n\t 2.POP \n\t 3. DISPLAY \n\t 4. EXIT \n\t");
do
{
printf("enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit");
}
default:
{
printf("please enter the valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=size-1)
2
{
printf("stack is overflow");
}
else
{
printf("enter the pushed elements :");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop()
{
if(top<0)
{
printf("stack is underflow:");
}
else
{
item=stack[top];
top=top-1;
}
printf("the poped elements is %d",item);
}
void display()
{
printf("the elements are:");
for(i=top;i>=0;i--)
{
printf("%d---->",stack[i]);
}
}
//OUTPUT
-----------------------------
1,.PUSH
2.POP
3. DISPLAY
4. EXIT
3
enter the pushed elements :11
4
2) Stack using Linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void push()
{
int data;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("unable to allocate memory");
}
else
{
printf("enter the elements:");
scanf("%d",&data);
if(head=='\0')
{
temp->data=data;
temp->link='\0';
head=temp;
}
else
{
temp->data=data;
temp->link=head;
head=temp;
}
}
}
void pop()
{
struct node *temp;
int item;
if(head==NULL)
{
printf("underflow");
}
else
{
item=head->data;
temp=head;
head=head->link;
free(temp);
}
printf("%d",item);
}
void display()
{
5
struct node *temp;
int i;
temp=head;
if(temp==NULL)
{
printf("\n STACK IS UNDERFL0W.");
}
else
{
printf("the elements are:");
while(temp!=NULL)
{
printf("%5d------------------->",temp->data);
temp=temp->link;
}
}
}
void display();
void push();
void pop();
int main()
{
int choice,data;
printf("STACK USING SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1.PUSH \n\t 2.POP \n\t 3.DISPLAY \n\t 4.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit point");
}
default:
{
6
printf("enter the valid choice (1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
OUTPUT
1.PUSH
2.POP
3.DISPLAY
4.exit
7
3) Queue using Array
#include<stdio.h>
#include<stdlib.h>
int rear,front,choice,i,size,item,queue[20];
void enqueue(void);
void dequeue(void);
void display(void);
int main()
{
front=0;
rear=0;
printf("enter the queue size:");
scanf("%d",&size);
printf("\nQUEUE OPERATION USING ARRAYS.");
printf("-----------------------------\n\t");
printf("1,.ENQUEUE \n\t 2. DEQUEUE \n\t 3. DISPLAY \n\t 4. EXIT \n\t");
do
{
printf("enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit");
}
default:
{
printf("please enter the valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void enqueue()
{
8
printf("enter the enqueue elements :");
scanf("%d",&item);
if(rear==size)
{
printf("queue is overflow");
}
else
{
if(front==0&&rear==0)
{
front=1;
}
rear=rear+1;
queue[rear]=item;
}
}
void dequeue()
{
if(front==0)
{
printf("queue is underflow:");
}
else
{
item=queue[front];
if(front==rear)
{
rear=0;
front=0;
}
else
{
front=front+1;
}
}
printf("the dequeue elements is %d",item);
}
void display()
{
printf("the elements are:");
for(i=front;i<=rear;i++)
{
printf("%d---->",queue[i]);
}
}
OUTPUT
9
QUEUE OPERATION USING ARRAYS.
-----------------------------
1,.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
10
4) Queue using Linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*front,*rear;
void enqueue()
{
int data;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("overflow");
}
else
{
printf("enter the elements:");
scanf("%d",&data);
temp->data=data;
if(front=='\0')
{
front=temp;
rear=temp;
front->link='\0';
rear->link='\0';
}
else
{
rear->link=temp;
rear=temp;
rear->link='\0';
}
}
}
void dequeue()
{
struct node *temp;
if(front==NULL)
{
printf("underflow");
}
else
{
temp=front;
front=front->link;
free(temp);
}
}
void display()
{
11
struct node *temp;
temp=front;
if(temp==NULL)
{
printf("\n QUEUE IS UNDERFL0W.");
}
else
{
printf("the elements are:");
while(temp!=NULL)
{
printf("%5d------------------->",temp->data);
temp=temp->link;
}
}
}
void display();
void enqueue();
void dequeue();
int main()
{
int choice;
printf("STACK USING SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1.enqueue \n\t 2.dequeue \n\t 3.DISPLAY \n\t 4.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit point");
}
default:
{
printf("enter the valid choice (1/2/3/4)");
12
}
}
}
while(choice!=4);
return 0;
}
//OUTPUT
----------------------
1.enqueue
2.dequeue
3.DISPLAY
4.exit
exit point
13
5) Linear Search
#include<stdio.h>
void main()
{
int i,found=0,n,location=0,key;
printf("enter the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key value:");
scanf("%d",&key);
i=0;
while(i<n&&found==0)
{
if(a[i]==key)
{
found=1;
location=i;
}
else
{
i=i+1;
}
}
if(found==1)
{
printf("search is successfull.");
printf("key elements is=%d",key);
printf("location is =%d",location);
}
else
{
printf("search is unsuccessfull.");
}
}
//OUTPUT1
search is successfull.
location is =3
14
//OUTPUT2
search is unsuccessfull.
15
6) Binary Search [Non Recursive]
#include<stdio.h>
void main()
{
int i,key,n,low,high,mid;
printf("enter the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements in ascending order:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key element:");
scanf("%d",&key);
low=0;
high=n-1;
while(high>=low)
{
mid=(low+high)/2;
if(key==a[mid])
{
break;
}
else
{
if(key>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
}
if(key!=a[mid])
printf("the key is not found");
else
printf("the key element is found at location=%d",mid+1);
}
OUTPUT1
enter the array size:5
enter the array elements in ascending order:2
4
6
8
10
enter the key element:4
the key element is found at location=2
16
OUTPUT2
17
7) Exchange Sort [Bubble Sort]
#include<stdio.h>
void main()
{
int n,i,j,t=0;
printf("enter the n value:");
scanf("%d",&n);
int a[n];
printf("enter the array values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("elements are:");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
//logic
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("sorted array elements are:");
for(j=0;j<n;j++)
{
printf("%d",a[j]);
}
}
OUTPUT
elements are:3 2 8 5 1
18
8) Selection Sort
#include<stdio.h>
int main()
{
int n,i,j,temp,loc,min;
printf("enetr the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
//logic
for(i=0;i<n;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
OUTPUT
52814
19
9) Insertion Sort
#include<stdio.h>
int main()
{
int n,i,j,temp;
printf("enetr the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
//logic
for(i=0;i<n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
OUTPUT:
20
10) Quick sort [Non recursion]
#include<stdio.h>
void quick_sort(int[],int ,int );
int partition(int[],int ,int );
int main()
{
int n,i;
printf("enetr the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quick_sort(a,0,n-1);
OUTPUT:
22
11) Heap Sort
#include<stdio.h>
void create(int []);
void down_adjust(int [],int);
void main()
{
int n,i,last,temp;
printf("enter the array size:");
scanf("%d",&n);
int heap[n];
printf("enter the array elements:");
for(i=1;i<=n;i++)
{
scanf("%d",&heap[i]);
}
heap[0]=n;
create(heap);
while(heap[0]>1)
{
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}
down_adjust(heap,i);
}
}
24
12) Conversion of infix to postfix
#include<stdio.h>
char stack[20];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
main()
char exp[20];
char *e, x;
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c",*e);
push(*e);
printf("%c", x);
else
printf("%c",pop());
push(*e);
e++;
while(top != -1)
printf("%c",pop());
OUTPUT:
12+
26
13) Evaluation of postfix
#include<stdio.h>
int stack[20];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{
stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1=1,n2=3,n3,num;
printf("enter the expression:");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isdigit(*e))
{
num=*e-48;
push(num);
}
else
{
n1=pop();
n2=pop();
switch(*e)
{
case '+':
{
n3=n1+n2;
break;
}
case '-':
{
n3=n2-n1;
break;
}
case '*':
{
n3=n1*n2;
break;
}
case '/':
{
n3=n2/n1;
break;
}
}
27
push(n3);
}
e++;
}
printf("\n result of expression %s=%d\n",exp,pop());
return 0;
}
OUTPUT:
result of expression 1 2 - = -1
28
14) Single linked list insertion
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void createlist(int n);
void traverse();
void insertatbeginning(int data);
void insertatend(int data);
void insertatanyposition(int key,int data);
int main()
{
int n,data,choice,key;
printf("SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1. createlist \n\t 2.traverse \n\t 3.insertatbeginning \n\t
4.insertatend \n\t 5.insertatanyposition \n\t 6.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("enter the total number of nodes:");
scanf("%d",&n);
createlist(n);
break;
}
case 2:
{
traverse();
break;
}
case 3:
{
printf("enter the data to be insert at front:");
scanf("%d",&data);
insertatbeginning(data);
break;
}
case 4:
{
printf("enter the data to be inserted at last:");
scanf("%d",&data);
insertatend(data);
break;
}
case 5:
{
29
printf("enter the key:");
scanf("%d",&key);
printf("enter the data to be insert at desired position:");
scanf("%d",&data);
insertatanyposition(key,data);
break;
}
case 6:
{
printf("exit point");
}
default:
{
printf("enter the valid choice (1-6)");
}
}
}
while(choice!=6);
return 0;
}
void createlist(int n)
{
struct node *new,*ptr;
int data,i;
head=(struct node *)malloc(sizeof(struct node));
if(head==NULL)
{
printf("list is empty.");
}
printf("enter the data of node 1:");
scanf("%d",&data);
head->data=data;
head->link= NULL;
ptr=head;
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory .");
}
printf("enter the data node %d:",i);
scanf("%d",&data);
new->data=data;
new->link=NULL;
ptr->link=new;
ptr=ptr->link;
}
}
void traverse()
{
struct node *ptr;
if(head==NULL)
30
{
printf("list is empty");
}
ptr=head;
while(ptr!=NULL)
{
printf("data-->=%d\t",ptr->data);
ptr=ptr->link;
}
}
OUTPUT:
1. createlist
2.traverse
3.insertatbeginning
4.insertatend
5.insertatanyposition
6.exit
32
data-->=11 data-->=22 data-->=33 data-->=44
exit point
33
15) Single linked list Deletion
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void createlist(int n);
void traverse();
void deleteatbeginning(void);
void deleteatend(void);
void deleteatanyposition(int position);
int main()
{
int n,data,choice,position;
printf("SINGLE LINKED LIST");
printf("----------------------\n");
printf("\n\t 1. createlist \n\t 2.traverse \n\t 3.deleteatbeginning \n\t
4.deleteatend \n\t 5.deleteatanyposition \n\t 6.exit \n\t");
do
{
printf("enter the choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("enter the total number of nodes:");
scanf("%d",&n);
createlist(n);
break;
}
case 2:
{
traverse();
break;
}
case 3:
{
deleteatbeginning();
break;
}
case 4:
{
deleteatend();
break;
}
case 5:
{
printf("enter the positon to be deleted:");
scanf("%d",&position);
deleteatanyposition(position);
break;
34
}
case 6:
{
printf("exit point");
}
default:
{
printf("enter the valid choice (1-6)");
}
}
}
while(choice!=6);
return 0;
}
void createlist(int n)
{
struct node *new,*ptr;
int data,i;
head=(struct node *)malloc(sizeof(struct node));
if(head==NULL)
{
printf("list is empty.");
}
printf("enter the data of node 1:");
scanf("%d",&data);
head->data=data;
head->link= NULL;
ptr=head;
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory .");
}
printf("enter the data node %d:",i);
scanf("%d",&data);
new->data=data;
new->link=NULL;
ptr->link=new;
ptr=ptr->link;
}
}
void traverse()
{
struct node *ptr;
if(head==NULL)
{
printf("list is empty");
}
ptr=head;
while(ptr!=NULL)
{
35
printf("data-->=%d\t",ptr->data);
ptr=ptr->link;
}
}
void deleteatbeginning()
{
struct node *ptr;
if(head==NULL)
{
printf("list is empty.");
}
else
{
ptr=head;
head=head->link;
printf("data deleted successfully.");
}
}
void deleteatend()
{
struct node *ptr,*ptr1;
if(head==NULL)
{
printf("list is empty.");
}
else
{
ptr=head;
ptr1=head;
while(ptr->link==NULL)
{
ptr1=ptr;
ptr=ptr->link;
}
if(ptr==head)
{
head=NULL;
}
else
{
ptr1->link=NULL;
}
printf("data deleted successfully:");
}
}
if(head==NULL)
36
{
printf("list is empty.");
}
else
{
ptr=head;
ptr1=head;
for(i=2;i<=n;i++)
{
ptr1=ptr;
ptr=ptr->link;
if(ptr==NULL)
break;
}
if(ptr!=NULL)
{
if(ptr==head)
{
head=head->link;
ptr->link=ptr->link;
ptr1->link=NULL;
free(ptr);
}
else
{
printf("invalid position unable to delete.");
}
}
}
}
OUTPUT:
1. createlist
2.traverse
3.deleteatbeginning
4.deleteatend
5.deleteatanyposition
6.exit
37
enter the data node 3:3
Data→=2 data→=3
Data→=2
38
16) Single linked list Traverse
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void createlist(int n);
void traverse();
int main()
{
int n,data;
printf("enter the total number of nodes:");
scanf("%d",&n);
createlist(n);
printf("\n data in the list.");
traverse();
return 0;
}
void createlist(int n)
{
struct node *new,*ptr;
int data,i;
head=(struct node *)malloc(sizeof(struct node));
if(head==NULL)
{
printf("list is empty.");
}
printf("enter the data of node 1:");
scanf("%d",&data);
head->data=data;
head->link= NULL;
ptr=head;
for(i=2;i<=n;i++)
{
new=(struct node *)malloc(sizeof(struct node));
if(new==NULL)
{
printf("unable to allocate memory .");
}
printf("enter the data node %d:",i);
scanf("%d",&data);
new->data=data;
new->link=NULL;
ptr->link=new;
ptr=ptr->link;
}
}
void traverse()
{
39
struct node *ptr;
if(head==NULL)
{
printf("list is empty");
}
else
{
ptr=head;
while(ptr!=NULL)
{
printf("%d-------->",ptr->data);
ptr=ptr->link;
}
}
}
OUTPUT:
40
17) Removing duplicate elements(ordered & unordered)
#include<stdio.h>
void main()
{
int n,i,j,k;
printf("enetr the array size:");
scanf("%d",&n);
int a[n];
printf("enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
OUTPUT:
enetr the array size:4
1
6
41
2
1
New array is :
Array after sorting:
1 2 6
42
18) Matrix Multiplication
#include<stdio.h>
void main()
{
int i,m,n,p,q,k,j,sum=0;
printf("enter the rows and columns size:");
scanf("%d %d",&m,&n);
int a[m][n],c[m][n];
printf("enter the array elements :");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d\t",&a[i][j]);
}
}
printf("enter the row and cloumn size:");
scanf("%d %d",&p,&q);
int b[p][q];
if(n!=p)
{
printf("matrix is not multiplied.");
}
else
{
printf("enter the elements are:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d\t",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+(a[i][k]*b[k][j]);
}
c[i][j]=sum;
sum=0;
}
}
printf("\nproduct of matrices are:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
}
43
}
OUTPUT:
enter the rows and columns size:2 2
enter the array elements :1
4
Product of matrices are:7 10 15 22
44
19) Quick sort(Recursion)
#include<stdio.h>
int a[50];
void qsort(int,int);
int split(int,int);
int main()
int n,i;
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(0,n-1);
for(i=0;i<n;i++)
printf("%5d",a[i]);
return 0;
int s;
if(start>=end)
return;
s=split(start,end);
qsort(start,s-1);
qsort(s+1,end);
45
{
int p=a[start];
int i=start,j=end,temp;
while(i<j)
while(a[i]<=p)
i++;
while(a[j]>p)
j--;
if(i<j)
temp=a[i],a[i]=a[j],a[j]=temp;
a[start]=a[j];
a[j]=p;
return j;
OUTPUT:
46
20) Binary Search [Recursive]
#include<stdio.h>
int binarysearch(int a[],int low,int high,int x)
{
int mid=(low+high)/2;
if(low>high)
return(-1);
if(a[mid]==x)
return (mid);
if(a[mid]<x)
return binarysearch(a,mid+1,high,x);
else
return binarysearch(a,low,mid-1,x);
}
int main()
{
int pos,search_item,len,i;
printf("enter the array size:");
scanf("%d",&len);
int a[len];
printf("enter the array elements:");
for(i=0;i<len;i++)
{
scanf("%d",&a[i]);
}
printf("enter the elements to search");
scanf("%d",&search_item);
pos=binarysearch(a,0,len-1,search_item);
if(pos<0)
printf("cannot find the element %d in array\n",search_item);
else
printf("position of %d in the array is %d",search_item,pos+1);
return 0;
}
OUTPUT: