You are on page 1of 78

SCMS School of Engineering and Technology

Department of Computer Science and Engineering


Data Structures Lab-CSL201

Data Structure Lab Cycle 2022

1. ARRAY OPERATIONS

2. STACK USING ARRAY

3. INFIX TO POSTFIX CONVERSION

4. POSTFIX EVALUATION

5. QUEUE USING ARRAY

6. CIRCULAR QUEUE

7. SPARSE MATRIX ADDITION

8. POLYNOMIAL ADDITION

9. SINGLY LINKEDLIST

10. STACK USING LINKEDLIST

11. QUEUE USING LINKEDLIST

12. DOUBLY LINKEDLIST

13. POLYNOMIAL ADDITION USING LINKEDLIST

14. TREE TRAVERSAL

15. BREADTH FIRST SEARCH

16. DEPTH FIRST SEARCH

17. INSERTION SORT

18. BUBBLE SORT

19. SELECTION SORT

20. MERGESORT

21. QUICK SORT


PROGRAM NO:1
ARRAY OPERATIONS

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int a[20],i,n,pos,ch,item;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}n--;
while(1)
{
printf("\nMENU\n1.Insert into beginning\n2.Insert into end\n3.Insert into particular
position\n4.Delete from beginning\n5.Delete from end\n6.Delete from particular
position\n7.Display\n8.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(n>=19)
printf("\nInsertion is not possible");
else
{
for(i=n;i>=0;i--)
{
a[i+1]=a[i];
}
n++;
printf("\nEnter the element to be inserted: ");
scanf("%d",&item);
a[0]=item;
}
break;
case 2:
if(n>=19)
printf("\nInsertion is not possible");
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d",&item);
a[n+1]=item;
n++;
}
break;
case 3:
if(n>=19)
printf("\nInsertion is not possible");
else
{
printf("\nEnter the position: ");
scanf("%d",&pos);
for(i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
n++;
printf("\nEnter the element to be inserted: ");
scanf("%d",&item);
a[pos]=item;
}
break;
case 4:
item=a[0];
for(i=0;i<=n-1;i++)
{

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

}
n--;
printf("\nDeleted element is %d",item);
break;
case 5:
item=a[n];
n--;
printf("\nDeleted item is %d",item);
break;
case 6:
printf("\nEnter the position: ");
scanf("%d",&pos);
item=a[pos];
for(i=pos;i<=n-1;i++)
{
a[i]=a[i+1];
}
n--;
printf("\nDeleted element is %d",item);
break;
case 7:
printf("\nArray is: ");
for(i=0;i<=n;i++)
{
printf("%d\t",a[i]);
}
break;
case 8:
exit(0);
}
}
getch();
}
OUTPUT

Enter the number of elements: 5

Enter the elements: 1 2 3 4 5

MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 1

Enter the element to be inserted: 6

MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 2

Enter the element to be inserted: 7

MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 7

Array is: 6 1 2 3 4 5 7
MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 3

Enter the position: 2

Enter the element to be inserted: 8

MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 7

Array is: 6 1 8 2 3 4 5 7
MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 4

Deleted element is 6
MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 5

Deleted item is 7
MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 7

Array is: 1 8 2 3 4 5
MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 6

Enter the position: 1

Deleted element is 8
MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 7

Array is: 1 2 3 4 5
MENU
1.Insert into beginning
2.Insert into end
3.Insert into particular position
4.Delete from beginning
5.Delete from end
6.Delete from particular position
7.Display
8.Exit
Enter your choice: 8
PROGRAM NO:2
STACK USING ARRAY

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 3
int main()
{
int top=-1, item, ch, i, a[MAXSIZE];
while(1)
{
printf("\nMENU \n 1. PUSH\n 2. POP \n 3. DISPLAY \n 4. EXIT \n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(top==(MAXSIZE-1))
printf("\nStack is full!");
else
{
if(top==-1)
top=0;
else
{
top++;
}
printf("\nEnter the element: ");
scanf("%d", &item);
a[top]=item;
}
break;
case 2:
if(top==-1)
printf("\nDeletion not possible!");
else
{
item=a[top];
top--;
printf("The deleted item is : %d", item);
}
break;
case 3:
if(top==-1)
printf("No items to display!");
else
{
printf("Elements are: ");
for(i=top; i>=0; i--)
{
printf("%d ", a[i]);
}
}
break;

case 4:
exit(0);
}
}
getch();
}
OUTPUT

MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 1

Enter the element: 10

MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 1

Enter the element: 20

MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 1

Enter the element: 30

MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 1

Stack is full!
MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 3
Elements are: 30 20 10
MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 2
The deleted item is : 30
MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 3
Elements are: 20 10
MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 2
The deleted item is : 20
MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 2
The deleted item is : 10
MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 2

Deletion not possible!


MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 3
No items to display!
MENU
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your choice: 4
PROGRAM NO:3
INFIX TO POSTFIX CONVERSION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void push (char);
char pop();
int precd(char);
char infix[30], postfix[20];
char stack[20];
int top=-1;
int main()
{
int i=0, j=0;
char c, item;
printf("\nenter the infix expression: ");
gets(infix);
while(infix[i]!='\0')
{
c=infix[i];
switch(c)
{
case '(':
push(c);
break;

case ')' :
item=pop();
while(item!='(')
{
postfix[j]=item;
j++;
item=pop();
}
break;
case '^':
case '/':
case '*':
case '+':
case '-':
while((precd(stack[top]))>=(precd(c)))
{
item=pop();
postfix[j]=item;
j++;
}
push(c);
break;
default:
postfix[j]=c;
j++;
}
i++;
}
while(top>=0)
{
item=pop();
postfix[j]=item;
j++;
}
postfix[j]='\0';
printf("\nThe postfix expression is : ");
puts(postfix);
getch();
}
void push(char n)
{
top++;
stack[top]=n;
}
char pop()
{
char b;
b=stack[top];
top--;
return b;
}
int precd(char n)
{
if(n=='^')
return 7;
else if(n=='/' || n=='*')
return 6;
else if(n=='+' || n=='-')
return 5;
else
return 4;
}
OUTPUT

enter the infix expression: (a*b)/c

The postfix expression is : ab*c/


PROGRAM NO:4
POSTFIX EVALUATION

#include<stdio.h>
#include<conio.h>
int stack[20];
int top=-1;
void push(char);
int pop();
void main()
{
char p[20];
int value[20],i=0,opr,op1,op2,op3,result;
printf("enter the postfix expression: ");
gets(p);
while(p[i]!='\0')
{
if(isalpha(p[i]))
{
printf("enter the value of %c : ",p[i]);
scanf("%d",&value[i]);
}
i++;
}
i=0;
while(p[i]!='\0')
{
if(isalpha(p[i]))
{
push(value[i]);

}
else
{
opr=p[i];
op2=pop();
op1=pop();
switch(opr)
{
case '*':
op3=op1*op2;
break;
case '/':
op3=op1/op2;
break;
case'+':
op3=op1+op2;
break;
case'-':
op3=op1-op2;
break;
}
push(op3);
}
i++;
}
result=pop();
printf("result=%d",result);
getch();
}
void push(char n)
{
top++;
stack[top]=n;

}
int pop()
{
char b;
b=stack[top];
top--;
return b;
}
OUTPUT

enter the postfix expression: abc*+


enter the value of a : 2
enter the value of b : 3
enter the value of c : 4
result=14
PROGRAM NO:5
QUEUE USING ARRAY

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 3
int main()
{
int ch, front=-1, rear=-1, q[max], item, i;
while(1)
{
printf("\n\t QUEUE\n\n 1. Insert \n 2. Delete \n 3. Display \n 4. Exit\n Enter ur choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(rear==max-1)
{
printf("Queue is full ! ");
}
else
{
if(front==-1 && rear==-1)
{
front=0; rear=0;
}
else
rear++;
printf("Enter the inserted item : ");
scanf("%d", &item);
q[rear]=item;
}
break;
case 2:
if((front==-1 && rear==-1) || front==rear+1)
{
printf("Queue is empty!");
}
else
{
if(front==-1)
{
printf("Queue is empty ! ");
}
else
{
item=q[front];
front++;
printf("Deleted item is : %d", item);
}
}
break;
case 3:
if((front==-1 && rear==-1) || front==rear+1)
{
printf("Queue is empty ! ");
}
else
{
printf("\nElements are :");
for(i=front; i<=rear; i++)
printf("%d ", q[i]);
}
break;
case 4:
exit(0);
break;
}
}
getch();
}
OUTPUT

QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 1
Enter the inserted item : 10

QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 1
Enter the inserted item : 20

QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 1
Enter the inserted item : 30

QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 1
Queue is full !
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 3

Elements are :10 20 30


QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 2
Deleted item is : 10
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 3

Elements are :20 30


QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 1
Queue is full !
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 2
Deleted item is : 20
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 2
Deleted item is : 30
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 2
Queue is empty!
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 3
Queue is empty !
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 1
Queue is full !
QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice:4
PROGRAM NO:6
CIRCULAR QUEUE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 3
int main()
{
int front=-1,rear=-1,q[max],i=0,ch,item=0;
while(1)
{
printf("\n\t CIRCULAR QUEUE\n\n 1. Insert \n 2. Delete \n 3. Display \n 4. Exit\n\nEnter ur
choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if((front==0 && rear==max-1) || front==rear+1)
{
printf("\nQueue is full !");
}
else
{
if(front==-1 && rear==-1)
{
front=0; rear=0;
}
else if(rear==max-1)
{
rear=0;
}
else
{
rear++;
}
printf("\nEnter the inserted item: ");
scanf("%d",&item);
q[rear]=item;
}
break;
case 2:
if(front==-1 && rear==-1)
{
printf("Queue is empty!");
}
else
{
if(front==max-1)
{
item=q[front];
front=0;
}
else if(front==rear)
{
item=q[front];
front=-1;
rear=-1;
}
else
{
item=q[front];
front++;
}
printf("\nDeleted Item is %d ", item);
}
break;
case 3:
if(front==-1 && rear ==-1)
{
printf("\nNo items to display!");
}
else if(front<=rear)
{
printf("\nElements are: ");
for(i=front; i<=rear; i++)
printf("\t%d",q[i]);

}
else
{
printf("\nElements are: ");
for(i=front; i<=max-1; i++)
printf("\t%d", q[i]);
for(i=0; i<=rear; i++)
printf("\t%d", q[i]);
}
break;
case 4:
exit(0);
break;
}
}
}
OUTPUT

CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 1

Enter the inserted item: 5

CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 1

Enter the inserted item: 10

CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 1

Enter the inserted item: 15

CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 1

Queue is full !
CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 3

Elements are: 5 10 15
CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 2

Deleted Item is 5
CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 3

Elements are: 10 15
CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 1

Enter the inserted item: 20

CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit

Enter ur choice: 3

Elements are: 10 15 20
CIRCULAR QUEUE

1. Insert
2. Delete
3. Display
4. Exit
Enter ur choice: 4
PROGRAM NO:7
SPARSE MATRIX ADDITION

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],d[5][5],e[5][5],n,m,r,s,i,j,p=1,q=1,v;
printf("\nenter the order of first matrix : ");
scanf("%d%d",&m,&n);
printf("\nenter the order of second matrix : ");
scanf("%d%d",&r,&s);
printf("\nenter the first matrix :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nsparse form of first matrix is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=0)
{
c[p][0]=i;
c[p][1]=j;
c[p][2]=a[i][j];
p++;
}
}
}
c[0][0]=m;
c[0][1]=n;
c[0][2]=p-1;
for(i=0;i<=c[0][2];i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("\nenter the second matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nsparse form of second matrix is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(b[i][j]!=0)
{
d[q][0]=i;
d[q][1]=j;
d[q][2]=b[i][j];
q++;
}
}
}
d[0][0]=r;
d[0][1]=s;
d[0][2]=q-1;
for(i=0;i<=d[0][2];i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
i=1;j=1;v=1;
if((m==r)&&(n==s))
{
while((i<=c[0][2])&&(j<=d[0][2]))
{
if((c[i][0]==d[j][0])&&(c[i][1]==d[j][1]))
{
e[v][0]=c[i][0];
e[v][1]=c[i][1];
e[v][2]=c[i][2]+d[j][2];
i++;
v++;
j++;
}
else if((c[i][0]==d[j][0])&&(c[i][1]!=d[j][1]))
{
if(c[i][1]<d[j][1])
{
e[v][0]=c[i][0];
e[v][1]=c[i][1];
e[v][2]=c[i][2];
i++;
v++;
}
else
{
e[v][0]=d[j][0];
e[v][1]=d[j][1];
e[v][2]=d[j][2];
j++;
v++;
}
}
else if(c[i][0]!=d[j][0])
{
if(c[i][0]<d[j][0])
{
e[v][0]=c[i][0];
e[v][1]=c[i][1];
e[v][2]=c[i][2];
i++;
v++;
}
else
{
e[v][0]=d[j][0];
e[v][1]=d[j][1];
e[v][2]=d[j][2];
j++;
v++;
}
}
}
while(i<=c[0][2])
{
e[v][0]=c[i][0];
e[v][1]=c[i][1];
e[v][2]=c[i][2];
i++;
v++;
}
while (j<=d[0][2])
{
e[v][0]=d[j][0];
e[v][1]=d[j][1];
e[v][2]=d[j][2];
j++;
v++;
}
e[0][0]=m;
e[0][1]=n;
e[0][2]=v-1;
printf("\nresultant matrix matrix is :\n");
for(i=0;i<=e[0][2];i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",e[i][j]);
}
printf("\n");
}
}
else
{
printf("addition is not possible");
}
getch();
}
OUTPUT

enter the order of first matrix : 3 3

enter the order of second matrix : 3 3

enter the first matrix :


004
050
602

sparse form of first matrix is :


3 3 4
0 2 4
1 1 5
2 0 6
2 2 2

enter the second matrix :


002
040
300

sparse form of second matrix is :


3 3 3
0 2 2
1 1 4
2 0 3

resultant matrix matrix is :


3 3 4
0 2 6
1 1 9
2 0 9
2 2 2
PROGRAM NO:8
POLYNOMIAL ADDITION

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20][20], b[20][20], c[20][20];
int i, j, k, m, n, p, q;
printf("\nEnter the number of term of 1st polynomial: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nEnter the coefficient and power of %dth term: ", i);
scanf("%d%d", &a[i][0], &a[i][1]);
}
printf("\nThe 1st polynomial is: ");
for(i=0; i<n; i++)
{
printf("%dx^%d", a[i][0], a[i][1]);
if(i<(n-1))
{
printf("+");
}
}
printf("\n\nEnter the number of term of 2st polynomial: ");
scanf("%d",&m);
for(i=0; i<m; i++)
{
printf("\nEnter the coefficient and power of %dth term: ", i);
scanf("%d%d", &b[i][0], &b[i][1]);
}
printf("\n\nThe 2st polynomial is: ");
for(i=0; i<m; i++)
{
printf("%dx^%d", b[i][0], b[i][1]);
if(i<(m-1))
{
printf("+");
}
}
i=0; j=0; k=0;
while((i<n) && (j<m))
{
if(a[i][1]==b[j][1])
{
c[k][0]=a[i][0]+b[j][0];
c[k][1]=a[i][1];
i++;
j++;
k++;
}
else if(a[i][1]>b[j][1])
{
c[k][0]=a[i][0];
c[k][1]=a[i][1];
i++;
k++;
}
else
{
c[k][0]=b[j][0];
c[k][1]=b[j][1];
j++;
k++;
}
}
while(i<n)
{
c[k][0]=a[i][0];
c[k][1]=a[i][1];
k++;
i++;
}
while(j<m)
{
c[k][0]=b[j][0];
c[k][1]=b[j][1];
k++;
j++;
}
printf("\n\nThe resultant polynomial is: ");
for(i=0; i<k; i++)
{
printf("%dx^%d", c[i][0], c[i][1]);
if(i<(k-1))
{
printf("+");
}
}
getch();
}
OUTPUT

Enter the number of term of 1st polynomial: 3

Enter the coefficient and power of 0th term: 7 2

Enter the coefficient and power of 1th term: 6 1

Enter the coefficient and power of 2th term: 3 0

The 1st polynomial is: 7x^2+6x^1+3x^0

Enter the number of term of 2st polynomial: 2

Enter the coefficient and power of 0th term: 5 1

Enter the coefficient and power of 1th term: 2 0

The 2st polynomial is: 5x^1+2x^0

The resultant polynomial is: 7x^2+11x^1+5x^0


PROGRAM NO:9
SINGLY LINKEDLIST

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *ptr;
};
int main()
{
typedef struct node NODE;
NODE *start=NULL, *temp, *p;
int i, ch, pos, item;
while(1)
{
printf("\n\n\tSingle Linked List\n1. Insert into beginning\n2. Insert into end\n3. Insert into
particular position\n4. Delete from beginning\n5. Delete from end"
"\n6. Delete from particular position\n7. Display\n8. Exit\n\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d", &temp->data);
if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{
temp->ptr=start;
start=temp;
}
break;
case 2:
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d", &temp->data);
if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{
p=start;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
temp->ptr=NULL;
}
break;
case 3:
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d",& temp->data);
if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{ p=start;
printf("Enter position: ");
scanf("%d", &pos );
if(pos==1)
{
temp->ptr=start;
start=temp;
}
else
{
for(i=1; i<pos-1; i++)
{
p=p->ptr;
}
temp->ptr=p->ptr;
p->ptr=temp;
}
}
break;
case 4:
if(start==NULL)
{
printf("No elements to delete!");
}
else
{
if(start->ptr==NULL)
{
temp=start;
item=temp->data;
free(temp);
start=NULL;
}
else
{
temp=start;
item=temp->data;
start=start->ptr;
free(temp);
}
}
break;
case 5:
if(start==NULL)
{
printf("No elements to display!");
}
else
{
if(start->ptr==NULL)
{
temp=start;
item=temp->data;
free(temp);
start=NULL;
}
else
{
p=start;
temp=p->ptr;
while(temp->ptr!=NULL)
{
p=p->ptr;
temp=temp->ptr;
}
p->ptr=NULL;
free(temp);
}
}
break;
case 6:
if(start==NULL)
{
printf("List is empty");
}
else
{
printf("Enter the position: ");
scanf("%d", &pos);
if(pos==1)
{
temp=start;
start=start->ptr;
free(temp);
}
else
{
p=start;
for(i=1; i<pos-1; i++)
{
p=p->ptr;
}
temp=p->ptr;
p->ptr=temp->ptr;
free(temp);
}
}
break;
case 7:
if(start==NULL)
{
printf("No elements");
}
else
{
p=start;
while(p->ptr!=NULL)
{
printf("%d ", p->data);
p=p->ptr;
}
printf("%d", p->data);
}
break;
case 8:
exit(0);
}
}
getch();
}
OUTPUT

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 1


Enter the data to be inserted: 1

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 1


Enter the data to be inserted: 2

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 1


Enter the data to be inserted: 3

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit
Enter your choice: 2
Enter the data to be inserted: 4

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 7


3 2 1 4

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 3


Enter the data to be inserted: 5
Enter position: 1

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 7


5 3 2 1 4

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 3


Enter the data to be inserted: 6
Enter position: 3

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 7


5 3 6 2 1 4

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 4

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 5

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 7


3 6 2 1

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 6


Enter the position: 3

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 7


3 6 1

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 6


Enter the position: 1

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice: 7


6 1

Single Linked List


1. Insert into beginning
2. Insert into end
3. Insert into particular position
4. Delete from beginning
5. Delete from end
6. Delete from particular position
7. Display
8. Exit

Enter your choice:8


PROGRAM NO:10
STACK USING LINKEDLIST

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *ptr;
};
int main()
{
typedef struct node NODE;
NODE *start=NULL, *temp, *p;
int i, ch, pos, item;
while(1)
{
printf("\n\n\tStack Using Linked List\n1. Insert into beginning\n2. Delete from beginning\n3.
Display\n4.Exit\n\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d", &temp->data);
if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{
temp->ptr=start;
start=temp;
}
break;
case 2:
if(start==NULL)
{
printf("No elements to delete!");
}
else
{
if(start->ptr==NULL)
{
temp=start;
item=temp->data;
printf("Deleted element is %d", temp->data);
free(temp);
start=NULL;
}
else
{
temp=start;
item=temp->data;
start=start->ptr;
free(temp);
printf("Deleted element: %d", item);
}
}
break;
case 3:
if(start==NULL)
{
printf("No elements");
}
else
{
printf("\nElements are: ");
p=start;
while(p->ptr!=NULL)
{
printf("%d ", p->data);
p=p->ptr;
}
printf("%d", p->data);
}
break;
case 4:
exit(0);
}
}
getch();
}
OUTPUT

Stack Using Linked List


1. Insert into beginning
2. Delete from beginning
3. Display
4.Exit

Enter your choice: 1


Enter the data to be inserted: 1

Stack Using Linked List


1. Insert into beginning
2. Delete from beginning
3. Display
4.Exit

Enter your choice: 1


Enter the data to be inserted: 2

Stack Using Linked List


1. Insert into beginning
2. Delete from beginning
3. Display
4.Exit

Enter your choice: 1


Enter the data to be inserted: 3

Stack Using Linked List


1. Insert into beginning
2. Delete from beginning
3. Display
4.Exit

Enter your choice: 3

Elements are: 3 2 1

Stack Using Linked List


1. Insert into beginning
2. Delete from beginning
3. Display
4.Exit

Enter your choice: 2


Deleted element: 3
Stack Using Linked List
1. Insert into beginning
2. Delete from beginning
3. Display
4.Exit

Enter your choice: 3

Elements are: 2 1

Stack Using Linked List


1. Insert into beginning
2. Delete from beginning
3. Display
4.Exit

Enter your choice: 4


PROGRAM NO:11
QUEUE USING LINKEDLIST

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *ptr;
};
int main()
{
typedef struct node NODE;
NODE *start=NULL, *temp, *p;
int i, ch, pos, item;
while(1)
{
printf("\n\n\tQueue Using Linked List\n1.Insert\n2.Delete\n3.Display\n4. Exit\n\nEnter your
choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d", &temp->data);

if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{
p=start;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
temp->ptr=NULL;
}
break;
case 2:
if(start==NULL)
{
printf("No elements to delete!");
}
else
{
if(start->ptr==NULL)
{
temp=start;
item=temp->data;
printf("Deleted element is %d", temp->data);
free(temp);
start=NULL;
}
else
{
temp=start;
item=temp->data;
start=start->ptr;
free(temp);
printf("Deleted element: %d", item);
}
}
break;
case 3:
if(start==NULL)
{
printf("No elements");
}
else
{
printf("\nElements are: ")
p=start;
while(p->ptr!=NULL)
{
printf("%d ", p->data);
p=p->ptr;
}
printf("%d", p->data);
}
break;
case 4:
exit(0);
}
}
getch();
}
OUTPUT

Queue Using Linked List


1.Insert
2.Delete
3.Display
4. Exit

Enter your choice: 1


Enter the data to be inserted: 1

Queue Using Linked List


1.Insert
2.Delete
3.Display
4. Exit

Enter your choice: 1


Enter the data to be inserted: 2

Queue Using Linked List


1.Insert
2.Delete
3.Display
4. Exit

Enter your choice: 1


Enter the data to be inserted: 3

Queue Using Linked List


1.Insert
2.Delete
3.Display
4. Exit

Enter your choice: 3

Elements are: 1 2 3

Queue Using Linked List


1.Insert
2.Delete
3.Display
4. Exit

Enter your choice: 2


Deleted element: 1
Queue Using Linked List
1.Insert
2.Delete
3.Display
4. Exit

Enter your choice: 3

Elements are: 2 3

Queue Using Linked List


1.Insert
2.Delete
3.Display
4. Exit

Enter your choice:


PROGRAM NO:12
DOUBLY LINKEDLIST

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev,*next;
};
int main()
{
typedef struct node NODE;
NODE *start=NULL, *temp, *p;
int i, ch, pos, item;
while(1)
{
printf("\n\n\tDoublY Linked List \n1.Insert into beginning\n2.Insert into end\n3.Delete from
beginning\n4.Delete from end\n5. Display\n6.Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d", &temp->data);
if(start==NULL)
{
temp->prev=NULL;
temp->next=NULL;
start=temp;
}
else
{
temp->next=start;
start->prev=temp;
temp->prev=NULL;
start=temp;
}
break;
case 2:
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d", &temp->data);
if(start==NULL)
{
temp->prev=NULL;
temp->next=NULL;
start=temp;
}
else
{
p=start;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
temp->next=NULL;
temp->prev=p;
}
break;
case 3:
if(start==NULL)
{
printf("No elements to delete!");
}
else
{
if(start->next==NULL)
{
temp=start;
item=temp->data;
printf("Deleted element is %d", temp->data);
free(temp);
start=NULL;
}
else
{
temp=start;
item=temp->data;
start=start->next;
start->prev=NULL;
free(temp);
printf("Deleted element: %d", item);
}
}
break;
case 4:
if(start==NULL)
{
printf("No elements to display!");
}
else
{
if(start->next==NULL)
{
temp=start;
item=temp->data;
printf("Deleted element is %d", temp->data);
free(temp);
start=NULL;
}
else
{
p=start;
temp=p->next;

while(temp->next!=NULL)
{
p=p->next;
temp=temp->next;
}
p->next=NULL;
printf("Deleted element is %d", temp->data);
free(temp);
}
}
break;
case 5:
if(start==NULL)
{
printf("\nNo elements");
}
else
{
printf("\nElements are: ");
p=start;
while(p->next!=NULL)
{
printf("%d ->", p->data);
p=p->next;
}
printf("%d ", p->data);

}
break;
case 6:
exit(0);
}
}
getch();
}
OUTPUT

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 1


Enter the data to be inserted: 10

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 1


Enter the data to be inserted: 20

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 2


Enter the data to be inserted: 30

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 2


Enter the data to be inserted: 40
DoublY Linked List
1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 5

Elements are: 20 ->10 ->30 ->40

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 3


Deleted element: 20

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 5

Elements are: 10 ->30 ->40

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 4


Deleted element is 40

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit
Enter your choice: 5

Elements are: 10 ->30

DoublY Linked List


1.Insert into beginning
2.Insert into end
3.Delete from beginning
4.Delete from end
5. Display
6.Exit

Enter your choice: 6


PROGRAM NO:13
POLYNOMIAL ADDITION USING LINKEDLIST

#include<stdio.h>
#include<stdlib.h>
struct poly
{
int coeff;
int expo;
struct poly *ptr;
};
int main()
{
typedef struct poly POLY;
int co,ex,i,m,n;
POLY *start1=NULL,*start2=NULL,*start3=NULL,*temp,*p;
printf("\nEnter the number of terms in first polynomial: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(POLY*)malloc(sizeof(POLY));
printf("\nEnter the coefficient and exponent: ");
scanf("%d%d",&co,&ex);
temp->coeff=co;
temp->expo=ex;
temp->ptr=NULL;
if(start1==NULL)
{
start1=temp;
}
else
{
p=start1;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
}
}
printf("\nFirst polynomial is: ");
for(p=start1;p!=NULL;p=p->ptr)
{
printf("%dX^%d",p->coeff,p->expo);
if(p->ptr!=NULL)
printf("+");
}
printf("\nEnter the number of terms in second polynomial: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
temp=(POLY*)malloc(sizeof(POLY));
printf("\nEnter the coefficient and exponent: ");
scanf("%d%d",&co,&ex);
temp->coeff=co;
temp->expo=ex;
temp->ptr=NULL;
if(start2==NULL)
{
start2=temp;
}
else
{
p=start2;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
}
}
printf("\nSecond polynomial is: ");
for(p=start2;p!=NULL;p=p->ptr)
{
printf("%dX^%d",p->coeff,p->expo);
if(p->ptr!=NULL)
printf("+");
}
while((start1!=NULL)&&(start2!=NULL))
{
if(start1->expo==start2->expo)
{
temp=(POLY*)malloc(sizeof(POLY));
temp->coeff=start1->coeff+start2->coeff;
temp->expo=start1->expo;
temp->ptr=NULL;
start1=start1->ptr;
start2=start2->ptr;
}
else if(start1->expo>start2->expo)
{
temp=(POLY*)malloc(sizeof(POLY));
temp->coeff=start1->coeff;
temp->expo=start1->expo;
temp->ptr=NULL;
start1=start1->ptr;
}
else
{
temp=(POLY*)malloc(sizeof(POLY));
temp->coeff=start2->coeff;
temp->expo=start2->expo;
temp->ptr=NULL;
start2=start2->ptr;
}
if(start3==NULL)
{
start3=temp;
}
else
{
p=start3;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
}
}
while(start1!=NULL)
{
temp=(POLY*)malloc(sizeof(POLY));
temp->coeff=start1->coeff;
temp->expo=start1->expo;
temp->ptr=NULL;
start1=start1->ptr;

if(start3==NULL)
{
start3=temp;
}
else
{
p=start3;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
}
}
while(start2!=NULL)
{
temp=(POLY*)malloc(sizeof(POLY));
temp->coeff=start2->coeff;
temp->expo=start2->expo;
temp->ptr=NULL;
start2=start2->ptr;
if(start3==NULL)
{
start3=temp;
}
else
{
p=start3;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
}
}
printf("\nResultant polynomial is: ");
for(p=start3;p!=NULL;p=p->ptr)
{
printf("%dX^%d",p->coeff,p->expo);
if(p->ptr!=NULL)
printf("+");
}
}
OUTPUT

Enter the number of terms in first polynomial: 3

Enter the coefficient and exponent: 5 2

Enter the coefficient and exponent: 6 1

Enter the coefficient and exponent: 3 0

First polynomial is: 5X^2+6X^1+3X^0

Enter the number of terms in second polynomial: 2

Enter the coefficient and exponent: 2 2

Enter the coefficient and exponent: 4 1

Second polynomial is: 2X^2+4X^1

Resultant polynomial is: 7X^2+10X^1+3X^0


PROGRAM NO:14
TREE TRAVERSAL

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
int number;
struct node*left;
struct node*right;
};
typedef struct node *nodeptr;
nodeptr p,q,ptree;
nodeptr maketree(int x);
void set_left(nodeptr,int);
void set_right(nodeptr,int);
nodeptr maketree(int x)
{
nodeptr temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return(temp);
}
void set_right(nodeptr temp,int x)
{
temp->right=maketree(x);
}
void set_left(nodeptr temp,int x)
{
temp->left=maketree(x);
}
void pretrav(nodeptr tree)
{
if(tree!=NULL)
{
printf("->%d",tree->data);
pretrav(tree->left);
pretrav(tree->right);
}
}
void intrav(nodeptr tree)
{
if(tree!=NULL)
{
intrav(tree->left);
printf("->%d",tree->data);
intrav(tree->right);
}
}
void posttrav(nodeptr tree)
{
if(tree!=NULL)
{
posttrav(tree->left);
posttrav(tree->right);
printf("->%d",tree->data);
}
}
int main()
{
int number;
printf("Enter the elements: ");
scanf("%d",&number);
ptree=maketree(number);
scanf("%d",&number);
while(number!=-1)
{
p=q=ptree;
while(number!=p->data&&q!=NULL)
{
p=q;
if(number>p->data)
q=p->right;
else
q=p->left;
}
if(number==p->data)
printf("void insertion");
else if(number<p->data)
set_left(p,number);
else
set_right(p,number);
scanf("%d",&number);
}
printf("\nPreorder: ");
pretrav(ptree);
printf("\n\nInorder: ");
intrav(ptree);
printf("\n\nPostorder: ");
posttrav(ptree);
}
OUTPUT

Enter the elements: 5 3 2 9 4 8 -1

Preorder: ->5->3->2->4->9->8

Inorder: ->2->3->4->5->8->9

Postorder: ->2->4->3->8->9->5
PROGRAM NO:15

BREADTH FIRST SEARCH

#include<stdio.h>
int a[10][10],visited[10],n;
void searchfrom(int k);
int main()
{
int i,j;
printf("Enter the no. of nodes: ");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j)
{
printf("Enter value %d,%d,element ",i,j);
scanf("%d",&a[i][j]);
}
printf("\nNodes visited in this order: ");
for(i=1;i<=n;i++)
if(visited [i]==0)
searchfrom(i);
}
void searchfrom(int k)
{
int i;
printf("->%d",k);
visited[k]=1;
for(i=1;i<=n;i++)
{
if(visited[i]==0)
if(a[k][i]!=0)
searchfrom(i);
break;
}
}
OUTPUT

Enter the no. of nodes: 5


Enter the adjacency matrix
Enter value 1,2,element 1
Enter value 1,3,element 1
Enter value 1,4,element 1
Enter value 1,5,element 0
Enter value 2,1,element 0
Enter value 2,3,element 0
Enter value 2,4,element 1
Enter value 2,5,element 0
Enter value 3,1,element 0
Enter value 3,2,element 0
Enter value 3,4,element 0
Enter value 3,5,element 1
Enter value 4,1,element 0
Enter value 4,2,element 0
Enter value 4,3,element 1
Enter value 4,5,element 1
Enter value 5,1,element 0
Enter value 5,2,element 0
Enter value 5,3,element 0
Enter value 5,4,element 0

Nodes visited in this order: ->1->2->3->4->5


PROGRAM NO:16

DEPTH FIRST SEARCH

#include<stdio.h>
int a[10][10],visited[10],n;
void searchfrom(int k);
int main()
{
int i,j;
printf("Enter the no:of nodes: ");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j)
{
printf("Enter the value of %d,%d,element ",i,j);
scanf("%d",&a[i][j]);
}
printf("Nodes visited in this order: ");
for(i=1;i<=n;i++)
if(visited[i]==0)
searchfrom(i);
}
void searchfrom(int k)
{
int i;
printf("->%d",k);
visited[k]=i;
for(i=1;i<=n;i++)
if(visited[i]==0)
if(a[k][i]!=0)
{
searchfrom(i);
}
}
OUTPUT

Enter the no:of nodes: 5


Enter the adjacency matrix
Enter the value of 1,2,element 0
Enter the value of 1,3,element 1
Enter the value of 1,4,element 0
Enter the value of 1,5,element 1
Enter the value of 2,1,element 1
Enter the value of 2,3,element 0
Enter the value of 2,4,element 0
Enter the value of 2,5,element 1
Enter the value of 3,1,element 0
Enter the value of 3,2,element 1
Enter the value of 3,4,element 0
Enter the value of 3,5,element 1
Enter the value of 4,1,element 1
Enter the value of 4,2,element 1
Enter the value of 4,3,element 0
Enter the value of 4,5,element 0
Enter the value of 5,1,element 0
Enter the value of 5,2,element 0
Enter the value of 5,3,element 1
Enter the value of 5,4,element 0
Nodes visited in this order: ->1->3->2->5->4
PROGRAM NO:17
INSERTION SORT

#include<stdio.h>
int main()
{
int i,j,a[20],n,temp,k;
printf("\nEnter the no.of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(k=0;k<=n-1;k++)
{
temp=a[k];
j=k-1;
while(temp<a[j]&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("\nSorted elements are: ");
for(i=0;i<=n-1;i++)
printf("%d ",a[i]);
}

OUTPUT

Enter the no.of elements: 5

Enter the elements: 80 10 90 40 50

Sorted elements are: 10 40 50 80 90


PROGRAM NO:18
BUBBLE SORT

#include<stdio.h>
int main()
{
int i,j,a[20],n,temp;
printf("Enter the no.of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;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("\nSorted elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}

OUTPUT

Enter the no.of elements: 5

Enter the elements: 90 50 10 95 45

Sorted elements are: 10 45 50 90 95


PROGRAM NO:19
SELECTION SORT

#include<stdio.h>
int main()
{
int i,j,n,a[20],temp;
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nSorted elements are: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

OUTPUT

Enter the number of terms: 5

Enter the elements: 10 5 50 25 30

Sorted elements are: 5 10 25 30 50


PROGRAM NO:20
MERGESORT

#include<stdio.h>
int l,a[10],n,b[10],i;
void merge(int a,int b,int c);
void mergesort(int a,int b);
int main()
{
printf("Enter the no.of elements: ");
scanf("%d",&n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
printf("\nSorted elements are: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void mergesort(int l,int h)
{
int mid;
if(l<h)
{
mid=(l+h)/2;
mergesort(l,mid);
mergesort(mid+1,h);
merge(l,mid,h);
}
}
void merge(int l,int mid,int h)
{
int hi=l,i=l,j=mid+1,k;
while(hi<=mid&&j<=h)
{
if(a[hi]<=a[j])
{
b[i]=a[hi];
hi=hi+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(hi<=mid)
{
for(k=hi;k<=mid;k++)
{
b[i]=a[k];
i=i+1;
}
}
else
{
for(k=j;k<=h;k++)
{
b[i]=a[j];
i++;
}
}
for(k=l;k<=h;k++)
{
a[k]=b[k];
}
}

OUTPUT

Enter the no.of elements: 6

Enter the elements: 30 10 60 5 80 90

Sorted elements are: 5 10 30 60 80 80


PROGRAM NO:21
QUICK SORT

#include<stdio.h>
#define max 20
int a[max],n,i,l,h;
int main()
{
void input(void);
input();
}
void input(void)
{
void output(int a[],int n);
void quicksort(int a[],int l,int h);
printf("Enter the no.of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
l=0;
h=n-1;
quicksort(a,l,h);
printf("\nSorted array: ");
output(a,n);
}
void quicksort(int a[],int l,int h)
{
int temp,key,low,high;
low=l;
high=h;
key=a[(low+high)/2];
do
{
while(key>a[low])
low++;
while(key<a[high])
high--;
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
}
while(low<=high);
if(l<high)
quicksort(a,l,high);
if(low<h)
quicksort(a,low,h);
}
void output(int a[],int n)
{
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

OUTPUT

Enter the no.of elements: 5


Enter the elements: 80 10 20 45 30

Sorted array: 10 20 30 45 80

You might also like