Sei sulla pagina 1di 50

13BEE011

INDEX
S.N
o

DATE

TITLE OF THE EXPERIMENT

1.

03.1.1
5

ARRAY IMPLEMENTATION OF LIST


ADT

2.

08.1.1
5

LINKED LIST IMPLEMENTATION OF


LIST ADT

3.

20.1.1
5

CURSOR IMPLEMENTATION OF LIST


ADT

4.

05.2.1
5

ARRAY IMPLEMENTATION OF STACK


ADT

5.

05.02.
15

LINKED LIST IMPLEMENTATION OF


STACK ADT

6.

17.2.1
5

BALANCED PARENTHESIS USING


STACK ADT

7.

19.2.1
5

EVALUATING POSTFIX EXPRESSION


USING LINKED LIST ADT

8.a

26.2.1
5

ARRAY IMPLEMENTATION OF QUEUE


ADT

8.b
.

26.2.1
5

LINKED LIST IMPLEMENTATION OF


QUEUE ADT

9.

04.3.1
5

SEARCH TREE ADT BINARY SEARCH


TREE

10.

19.3.1
5

HEAPSORT

11.

31.3.1
5

QUICKSORT

PAGE
No.

MARKS

SIGNATURE
OF FACULTY

Page:1

13BEE011

Expt no: 1
Date: 3.1.15

ARRAY IMPLEMENTATION OF LIST ADT

Aim:
To create list using arrays and to perform manipulation on the list.

Algorithm:
1.Start
2 . Print 1 . Create 2 . Print 3 . Find 4 . Find k 5 . Insert first 6 . Insert last 7 . Insert middle 8. Insert
last 9 .delete last 10 .Delete k 11 . Exit
3 . do
4 . Read the choice
5 . If choice= 1 , Call create function go to step 16
6 . If choice=2, Call print() ,go to step17
7 . If choice =3, Call find () ,go to step18
8 . If choice =4, Call findk(),go to step 19
9 . If choice=5, Call insertfirst(),Go to step20
10 . If choice=6, Call insert last(), Go to step 21
11 . If choice=7,Call Insert middle(),Go to step 22
12 . If choice=8,Call deletefirst(),Go to step 23
13 . If choice=9,Call deletelast(),Go to step 24
14 . If choice=10,Call deleteK(),Go to step 25
15 . If Choice=11,Go to step 26
16 . Read data and store as list, Go to step 4.
17 . Print the present list elements , Go to step 4.
18 . .Find the position of the elemnt, Go to step 4.
19 . find the element in the position, Go to step 4.
20 . Insert the given elements in the first position,Go to step 4.
21 . insert the given element in the last position,Go to step 4.
22 . insert the given element elemen t in the given position,Go to step 4.
23 . Delete the first element in the list, Goto step 4.
Page:2

13BEE011
24 . Delete the last element in the list, Goto step 4.
25 . Delete the element in the given position,Go to step 4.

Program:
#include<stdio.h>
#include<conio.h>
int i,s,a[40],ch,n,w,flag=0,count=0;
void create()
{printf("enter the a size");
scanf("%d",&s);
printf("enter the elements");
for(i=0;i<s;i++)
{scanf("%d",&a[i]);
count++;
}}
void print()
{printf("the elements in the list are");
for(i=0;i<s;i++)
{printf("%d",a[i]);
}}
void find()
{ printf("enter the no to find");
scanf("%d",&n);
for(i=0;i<s;i++)
{ if(n==a[i])
{ flag=1;
printf("element is%d is found at%d",n,i++);
break;}}
if(flag==0)
{printf("the element is not found");
}}
Page:3

13BEE011
void findk()
{ int k;
printf("enter the position where the element is to find");
scanf("%d",&k);
if(k>=s)
{ printf("\nelement is not present");}
else
printf("the %d element is found at %d",a[k-1],k);
}
void insertfirst()
{ int ele;
printf("enter the number to be inserted");
scanf("%d",&ele);
if(!isfull())
{for(i=s;i>0;i--)
a[i]=a[i-1];
a[0]=ele;
count++;s++;
}}
void insertmiddle()
{

int k,ele;

printf("enter the position");


scanf("%d",&k);
printf("enter the element to insert");
scanf("%d",&ele);
if((pos<=s)&&(!isfull()))
{ for(i=s;i>k;i--)
a[i]=a[i-1];
a[k-1]=ele;
count++;
Page:4

13BEE011
s++;}}
void insertlast()
{ int ele;
printf("enter the no");
scanf("%d",&ele);
if(!isfull())
{ a[count]=ele;
count++; s++;
}
Else
printf("\nlist is empty");
}
void deletefirst()
{ if(!isempty())
{ for(i=0;i<s;i++)
a[i]=a[i+1];
count--; s-} }
void deletelast()
{ if(!isempty())
count--;s--;
else
printf("\nlist is empty");
}
void deletemiddle()
{ int pos;
printf("enter the position to be deleted");
scanf("%d",&pos);
if(!isempty())
{ for(i=pos;i<s;i++)
Page:5

13BEE011
a[pos-1]=a[pos];
count--;s--;}
else
printf("\nlist is empty");
}
int isempty()
{ if(count==0)
return 1;
else
return 0;
}
int isfull()
{ if(count==40)
return 1;
else
return 0;
}
void main()
{clrscr();
do
{ printf("1.create\n2.print\n3.find\n4.findk\n5.insertfirst()\n6.insertlast\n7.insertk\n8.deletefirst\n9.deletelast\n10.del
etemiddle\n11.exit");
printf("enter ur choice");
scanf("%d",&ch);
switch(ch)
{ case 1:create();break;
case 2:print();break;
case 3:find();break;
case 6:findk();break;
case 7:insertfirst();break;
case 8:insertlast();break;
Page:6

13BEE011
case 9:insertmiddle();break;
case 10:deletefirst();break;
case 11:deletelast();break;
case 12:deletemiddle();break;
}}while(ch!=13);
getch();
}

Output:

Page:7

13BEE011

Result:
Thus the program for creating list using arrays and manipulating it is successfully compiled and executed.

Expt no: 2
Date: 8.1.15

LINKED LIST IMPLEMENTATION OF LIST

Aim:
To create list using linked list and to perform manipulation on the list.

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

Start
Create of the structures with members data and a pointer variable of the struct data type next
Create a linked list in which first member is head and last member is tail
Read the choice
If choice =1, then call find pos() and go to step 15
If choice =2 , then call find ele() and go to step 16
If choice=3 , then call insert f() and go to step 17
If choice=4 , then call insertl() and go to step 18
If choice =5, then call insertaf() and go to step 19
If choice=6, then call deletef() and go to step 20
If choice=7, then call deletel() and go to step 21
If choice=8, then call deleteaf () and go to step 22
If choice =9, then call display () an go to step 23
If choice=10, then go to step 24
Find the element in the given position and go to step 4
Find the position of the given element and go to step 4
Page:8

13BEE011
17. Insert the element in the first position and the next to the address of the head node and go to step 4
18. Insert the element in the last position and make it tail node go to step 4
19. Insert the given element in the position after the element user has given go to step 4
20. Delete the first element in the list and go to step 4
21. Delete the last element in the list and go to step 4
22. Delete the element after the given element and go to step 4
23. Display all the element in the list go to step 4
24. Stop

Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{ int data;
struct node *next;
}*k1,*k,*temp,*head=NULL,*tail=NULL;
void findpos(int ele)
{ int count=1;
temp=head;
while(temp->data!=ele)
{temp=temp->next;
count++;
if(temp==NULL)
{ printf("\nelement not present");
break;
}}
if(temp!=NULL)
printf("\nthe position of the element is %d",count);
}
void findele(int pos)
{ int count=1; temp=head;
while(count!=pos)
Page:9

13BEE011
{ temp=temp->next;
count++;
if(temp==NULL)
{ printf("\nelement is not present");
break;}}
if(temp!=NULL)
printf("\nelement in the given position is %d",temp->data);
}
void insertf(int ele)
{ temp=(struct node *) malloc(sizeof(struct node));
temp->data=ele;
temp->next=head;
head=temp;
}
void insertl(int ele)
{ temp=(struct node *)malloc(sizeof(struct node));
tail->next=temp;
tail=temp;
tail->data=ele;
tail->next=NULL;
printf("\n%d",tail->data);
}
void insertaf(int ele,int ele1)
{ k1=(struct node *)malloc(sizeof(struct node));
temp=head;
while(temp->data!=ele1)
temp=temp->next;
k1->data=ele;
k1->next=temp->next;
temp->next=k1;
Page:10

13BEE011
}
void deletef()
{ temp=head;
head=temp->next;
free(temp);
}
void deletel()
{ temp=head;
while(temp->next!=NULL)
{ k=temp;
temp=temp->next;
}
k->next=NULL;
tail=k;
free(temp);
}
void deleteaf(int ele)
{temp=head;
while(temp->data!=ele)
temp=temp->next;
k=temp->next;
temp->next=k->next;
free(k);
}
void display()
{
temp=head;
do
{ printf("\n%d",temp->data);
temp=temp->next;
Page:11

13BEE011
}while(temp!=NULL);
}
void main()
{
int choice,x,ele,ele1,pos;clrscr(); printf("\nenter the elements of the linked list");
do
{ scanf("%d",&x);
temp=(struct node *) malloc(sizeof(struct node));
temp->data=x;temp->next=NULL;
if(head==NULL)
{ head=temp;
tail=temp;
}
else if(x!=0)
{
tail->next=temp;
tail=temp;}
}while(x!=0);
printf("\n 1.findpos\n 2.findele\n3.insert first\n 4.insert last\n 5.insert after\n 6.delete first\n 7.delete last \n 8.delete
after \n 9.display \n 10.exit");
do
{ printf("\nenter the choice");
scanf("%d",&choice);
switch(choice)
{ case 1:
printf("\nenter the position");
scanf("%d",&pos);
findpos(pos); break;
case 2:
printf("\nenter the element");
scanf("%d",&ele);
Page:12

13BEE011
findele(ele); break;
case 3:
printf("\nenter the element");
scanf("%d",&ele);
insertf(ele);break;
case 4:
printf("\nenter the element");
scanf("%d",&ele);
insertl(ele);break;
case 5:
printf("\nenter the element and after which it should be inserted ");
scanf("%d%d",&ele,&ele1);
insertaf(ele,ele1);break;
case 6:
deletef();break;
case 7:
deletel(); break;
case 8:
printf("\nenter the element");
scanf("%d",&ele);
deleteaf(ele);break;
case 9:
display(); break;
} }while(choice!=10);
}

Output:

Page:13

13BEE011

Result:
Thus the program for creating list using linked list and manipulating it is successfully compiled and
executed.

Expt no:3
Date: 22.1.15

CURSOR IMPLEMENTATION OF LIST

Aim:
To implement list as cursor and to perform manipulation on the list.

Algorithm:
1. start.
2. read the elements in order and store the next subscribe in the next of the element.
3. then choose the operation you want to do.
Page:14

13BEE011
4. if it is the insertion operation then find last by using next of 0. Then insert it in that place then make next of 0
as inserted element.
5. if it is a deleton operation then delete that element you want then make next of the

previous = to the next of

the element and make next of 0 as deleted elements subscribe.


6. then display then display with the content of the file.
7. stop.

Program:
#include<stdio.h>
#include<conio.h>
void insert(int);
void initial(void);
void disp();
void del(int);
int findprevious(int);
void curserfree(int);
struct node
{
int element;
int next;
};
typedef struct node curser;
struct node curserspace[10];
void main()
{
int opt,element;
clrscr();
initial();
while(1)
{
printf("\n 1.insert \n 2.delete \n 3.disp \n 4.exit\n Enter the option that you wish to continue");
scanf("%d",&opt);
switch(opt)
Page:15

13BEE011
{
case 1: printf("Enter the data");
scanf("%d",&element);
insert(element);
break;
case 2:printf("enter the element to delete");
scanf("%d",&element);
del(element);
break;
case 3: disp();
break;
case 4:exit(1);
}}}
void initial()
{
int i;
for(i=0;i<10;i++)
{
curserspace[i].element=0;
curserspace[i].next=i+1;
}
curserspace[10-1].element=0;
curserspace[10-1].next=0;
}
void disp()
{
int i;
printf("\t\t curser implementation");
printf("\t\t....\n");
printf("\ts.no \t element \t next position\n");
Page:16

13BEE011
for(i=0;i<10;i++)
printf("\t%d\t%d\t%d\n",i,curserspace[i].element,curserspace[i].next);
}
void insert(int x)
{
int temp;
temp=curseralloc();
if(temp==0)
printf("error");
else
curserspace[temp].element=x;
}
int curseralloc()
{
int p,t;
p=curserspace[0].next;
curserspace[0].next=curserspace[p].next;
return p;
}
void del(int x)
{
int p,temp;
p=findprevious(x);
if(p==0)
{
temp=1;
curserfree(temp);
}
else
{
Page:17

13BEE011
temp=curserspace[p].next;
curserspace[p].next=curserspace[temp].next;
curserfree(temp);
}
}
int findprevious(int x)
{int p=0,i;
if (curserspace[i].element==0)
return 0;
for(i=0;x!=curserspace[i].next;i++)
p=curserspace[i].next;
printf("p=%d\t",p);
for(i=0;curserspace[i].next!=p;i++)
{
p=i;
}
printf("test=%d\n",i);
return p;
}
void curserfree(int p)
{
int i;
for(i=curserspace[p].next;i<10-1;i++)
{
if(curserspace[i].next==curserspace[0].next)
{
curserspace[i].next=p;
break;
}}
curserspace[p].next=curserspace[0].next;
Page:18

13BEE011
curserspace[p].element=0;
curserspace[0].next=p;
}

Output:

Result:
Thus the program for cursor implementation of list using arrays and manipulation is successfully compiled
and executed.
Page:19

13BEE011

Expt no: 4
Date: 5.2.15

ARRAY IMPLEMENTATION OF STACK

Aim:
To create stack using arrays and to perform manipulation on the stack.

Algorithm:
1 . Start
2 . Read the choice.
3 . If choice=1then call push(),Go to step 8.
4 . If choice=1then call push(),Go to step 8.
5 .If choice =3then call pop(), GO TO step 10
6 . If choice=4then call display (), Go to step 11
7 . If choice=5 Go to step12
8 .Check for overflow condition and push the read value , Go to step2
9. Check for underflow condition and pop the top element , Go to step 2
10. Display the top element go to step2
11. Display the stack with all elements after checking under flow condition Go to step2
12. STOP

Program:
#include<stdio.h>
#include<conio.h>
const int maxsize=10;
int stack[10],top=-1,k;
void push()
{ if(top+1==maxsize)
printf("stack is overflow");
else
{ printf("the elemet to push");
scanf("%d",&k);
Page:20

13BEE011
stack[++top]=k;
} }
void pop()
{ if(top==-1)
printf("underflow");
else
top--;
}
void peep()
{ if(top==-1)
{printf("underflow");
}
else
printf("%d",stack[top]);
}
void display()
{ int i;
if(top==-1)
printf("underflow");
else
{ for(i=0;i<=top;i++)
printf("\t%d",stack[i]);} }
void main()
{int choice;
clrscr();
do
{ printf("\n1.pop\n2.push\n3.peep\n4.display\n");
printf("enter ur choice");
scanf("%d",&choice);
switch(choice)
Page:21

13BEE011
{ case 1:pop();break;
case 2:push();break;
case 3:peep();break;
case 4:display();break;}}
while(choice!=5);}

Output:

Page:22

13BEE011

Result:
Thus the program for creating stack using arrays and manipulating it is successfully compiled and executed.

Expt no: 5
Date:

IMPLEMENTATION OF STACK USING LINKED

5.2.15

LIST

Aim:
To create stack using linked list and to perform manipulation on the stack

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

Start
Create of the structures with members data and a pointer variable of the struct data type next
Read the choice.
If choice=1 then call push(),Go to step 8.
If choice=2 then call push(),Go to step 8.
If choice =3 then call pop(), Go to step 10
If choice=4 then call display (), Go to step 11
If choice=5 Go to step12
Check for overflow condition and push the read value , Go to step2
Check for underflow condition and pop the top element , Go to step3
Display the top element go to step2
Display the stack with all elements after checking under flow condition Go to step2
Stop

Program:
#include<stdio.h>
#include<conio.h>
struct node
{ int data;
struct node *next;
}*temp,*top=NULL;
void push(int ele)
{ temp=(struct node*) malloc(sizeof(struct node));
temp->data=ele;
Page:23

13BEE011
temp->next=top;
top=temp;
}
void pop()
{ if(top==NULL)
printf("\nstack underflow");
else
{ printf("\n popped element is %d",top->data);
temp=top;
top=top->next;
free(temp);
}}
void peep()
printf("\n peep element is %d",top->data);
void display()
{ if(top==NULL)
printf("\n stack underflow");
else
{temp=top;
while(temp!=NULL)
{printf("\n%d",temp->data);
temp=temp->next;
}}}
void main()
{ int choice,ele; clrscr();
printf("\n 1.push 2.pop 3.peep 4.display 5.exit");
do
{ printf("\nenter the choice");
scanf("%d",&choice);
switch(choice)
Page:24

13BEE011
{ case 1:
printf("\n enter the element to be pushed");
scanf("%d",&ele);
push(ele); break;
case 2:
pop();break;
case 3:
peep(); break;
case 4:
display();
break;
}
}while(choice!=5);
}

Output:

Page:25

13BEE011

Result:
Thus the program for creating stack using linked list and manipulating it is successfully compiled and
executed.

Expt no:6
Date: 17.2.15

BALANCED PARANTHESIS USING STACK ADT

Aim:
To check whether the given expression is balanced in terms of paranthesis using stack ADT.

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

Start
Read the expression exp.
True is defined as 1 and false is defined as 0.
Initialise valid as true
If exp[i] is not equal to \0.
If exp[i] is( or [ or {
Push exp[i] in to the stack.
If exp[i] is) or ] or }
If top is -1, valid is false go to step 14
Else pop the element and store it in temp.
If exp[i] is ) and temp is { or [, change valid to false.
If exp[i] is ] and temp is { or (, change valid to false.
If exp[i] is } and temp is ( or [, change valid to false.
Increment i, go to step 5.
If top>=0, valid is false.
If valid is true , it is valid expression.
Else it is invalid expression.
Stop.

Program:
#include<stdio.h>
#include<conio.h>
#define max 20
#define true 1
Page:26

13BEE011
#define false 0
int top=-1;
int stack[max];
void push(char item)
{ if (top==(max-1))
printf("stack overflow");
else
{ top=top+1;
stack[top]=item;
}}
char pop()
{ if(top==-1)
printf("stack underflow\n");
else
return(stack[top--]);
return 0;
}
void main()
{ char exp[max],temp;
int i,valid=true;
printf("enter an algeberic expression:");
gets(exp);
for(i=0;i<=max;i++)
{ if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(exp[i]);
if(exp[i]==')' || exp[i]=='}' || exp[i]=='}')
if(top==-1)
valid=false;
else
{ temp=pop();
Page:27

13BEE011
if(exp[i]==')' && (temp=='{' || temp=='['))
valid= false;
if (exp[i]=='}' && (temp=='(' || temp=='['))
valid= false;
if(exp[i]==']' &&(temp=='(' ||emp=='{'))
valid=false;
}}
if(top>=0)
valid =false;
if(valid==true)
printf("valid expression \n");
else
printf("invalid expression\n);
getch();}

Output:

Page:28

13BEE011

Result:
Thus the program for checking the parenthesis balance is successfully compiled and executed.

Expt no: 7
Date: 17.2.15

EVALUATING POSTFIX EXPRESSION USING


LINKED LIST

Aim:
To evaluate the given postfix expression with the help of the stack ADT using linked list.

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

Start
Declare a structure with an integer and a pointer variable and objects.
Get a postfix expression as input.
Initialise i=0.
If expr[i] is not equal to\ 0.
If expr[i] is digit , then push it into the stack.
Increment i and go to step 5.
If expr[i] is not digit, pop twice and save it in two variables.
If expr[i]=+, then add two elements
If expr[i]=-, then subtract two elements
If expr[i]=*, then multiply two elements
If expr[i]=/, then divide two elements
Increment i, goto step 5.
Print the element in the stack.
Stop.

Program:
#include<stdio.h>
#include<conio.h>
struct node
{ int data;
Page:29

13BEE011
struct node *next;
}*temp,*top=NULL;
void push(int ele)
{ temp=(struct node*) malloc(sizeof(struct node));
temp->data=ele;
temp->next=top;
top=temp;
}
stack pop()
{ temp=top;
top=top->next;
return(temp->data);
}
void main()
{ char expr[20]; int op1,op2;
printf(\nEnter the postfix expression);
gets(expr);
for(i=0;expr[i]!=\0;i++)
{ if(isdigit(expr[i])
push(expr[i]-0);
else
{ op1=pop();
op2=pop();
switch(expr[i])
{ case +: push(op1+op2); break;
case -: push(op2-op1); break;
case *: push(op1*op2); break;
case /: push(op2/op1); break;
}}
printf(\nthe answer is %d,top->data);
Page:30

13BEE011
getch();
}

Output:

Result:
Thus the program for evaluating the postfix expression is successfully compiled and executed.

Expt no: 8a
Date: 26.2.15

ARRAY IMPLEMENTATION OF QUEUE

Aim:
To create queue using arrays and to perform manipulation on the queue

Algorithm
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

Start
Create an array named queue for defined maxsize
Read the choice
If choice =1,call enqueue() and Go to 7
If choice =2, call dequeue () and Go to 8
If choice=3, call display() and Go to 9
If choice=4 Go to 10
Add the given element to rear end of queue Go to 2
Remove the front element of queue Go to 2
Display all element Go to 2
Stop

Program:
#include<stdio.h>
#include<conio.h>
int front=-1,rear=-1,k,queue[20],maxsize=20,k;
void enqueue(int ele)
{ if(front==-1)
front++;
Page:31

13BEE011
if(rear==maxsize-1)
printf("overflow");
else
{rear++;
queue[rear]=k;
}}
void dequeue()
{ if(front==-1||front=rear+1)
printf("overflow");
else
front++;
}
void display()
{ int i;
if((front==-1)||(front==rear+1))
printf("not possible");
else
{ for(i=front;i<=rear;i++)
printf("%d",queue[i]);
}}

void main()
{ int ele,choice;
clrscr();
do
{
printf("\nenter ur choice\n1.enqueue \n2.dequeue\n.3.display..\n4.exit");
scanf("%d",&choice);
switch(choice)
{
Page:32

13BEE011
case 1:
scanf("%d",&ele);
enqueue();break;
case 2:dequeue();break;
case 3:display();break;
}}
while(choice!=4);
}

Output:

Page:33

13BEE011

Result:
Thus the program for creating queue using arrays and manipulating it is successfully compiled and executed.

Expt no: 8b
Date: 26.2.15

LINKED LIST IMPLEMENTATION OF QUEUE

Aim:
To create queue using linked list and to perform manipulation on the queue

Algorithm
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

Start
Create of the structures with members data and a pointer variable of the struct data type next
Create an array named queue for defined maxsize
Read the choice
If choice =1,call enqueue() and Go to 7
If choice =2, call dequeue () and Go to 8
If choice=3, call display() and Go to 9
If choice=4 Go to 10
Add the given element to rear end of queue Go to 2
Remove the front element of queue Go to 2
Display all element Go to 2
Stop

Program:
#include<stdio.h>
#include<conio.h>
struct node
Page:34

13BEE011
{ int data;
struct node *next;
}*temp,*front=NULL,*rear=NULL;
void enqueue(int ele)
{ temp=(struct node*) malloc(sizeof(struct node));
if(front==NULL)
front=temp;
else
rear->next=temp;
temp->data=ele;
temp->next=NULL;
rear=temp;
}
void dequeue()
{ if(front==NULL)
printf("\nqueue underflow");
else
{ temp=front;
front=temp->next;
free(temp);
}}
void display()
{ if(front==NULL)
printf("\nqueue underflow");
else
{ temp=front;
while(temp!=NULL)
{ printf("\n%d",temp->data);
temp=temp->next;
}}}
Page:35

13BEE011
void main()
{ int choice,ele; clrscr();
printf("\n 1.enqueue 2.dequeue 3.display 4.exit");
do
{printf("\nenter the choice");
scanf("%d",&choice);
switch(choice)
{ case 1:
printf("\n enter the element to be enqueued");
scanf("%d",&ele);
enqueue(ele); break;
case 2:
dequeue();break;
case 3:
display(); break;
}}while(choice!=4);
}

Output:

Page:36

13BEE011

Result:
Thus the program for creating queue using linked list and manipulating it is successfully compiled and
executed.

Expt no: 9
Date: 4.3.15

SEARCH TREE ADT BINARY SEARCH

Aim:
To create binary search tree and to perform manipulation on the created tree.

Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

Start
Declare a structure with data , left and right address inn name of node.
Read the elements and pass it to insert function unless the element is zero. Go to step 12.
Read the choice
If choice is 1, call preorder function , go to step 13.
If choice is 2, call postorder function , go to step 14.
If choice is 3, call inorder function , go to step 15.
If choice is 4, read the element and call delete function , go to step 16.
If choice is 5, read the element and call insert function , go to step 12.
If choice is 6, go to step 11.
Stop.
Create a memory location and insert element in left if it is small else insert in right by traversing down the

tree. Go to previously called step.


13. Print the root first then print left and then right, go to step 4.
Page:37

13BEE011
14. Print the left elements first then print right elements and then root, go to step 4.
15. Print the left first then print root and then right, go to step 4.
16. If the element to be deleted has both left and right elements place minimum element in its place and change
left address, go to step 4.
17. If the element is having only left or right element, replace the element to be ddeleted with that left or right
element , go to step 4.

Program:
#include<stdio.h>
#include<conio.h>
struct node
{int data;
struct node *left;
struct node *right;
};
typedef struct node *tree; tree root=NULL;
tree insert(int x,tree t)
{ if(t==NULL)
{ t=(struct node *)malloc(sizeof(struct node));
t->data=x;
t->right=NULL;
t->left=NULL;
}
else if(x<t->data)
t->left=insert(x,t->left);
else
t->right=insert(x,t->right);
return t;
}
void preorder(tree t)
{if(t!=NULL)
{ printf("\t%d",t->data);
preorder(t->left);
Page:38

13BEE011
preorder(t->right);
}}
void postorder(tree t)
{ if(t!=NULL)
{ postorder(t->left);
postorder(t->right);
printf("\t%d",t->data);
} }
void inorder(tree t)
{ if(t!=NULL)
{ inorder(t->left);
printf("\t%d",t->data);
inorder(t->right);
}}
tree findmin(tree t)
{ if(t==NULL)
return t;
else if(t->left==NULL)
return t;
else
return findmin(t->left);
}
tree deletex(int n, tree t)
{ tree temp;
if(t==NULL)
printf("\nNo element");
else if(n>t->data)
t->right=deletex(n,t->right);
else if(n<t->data)
t->left=deletex(n,t->left);
Page:39

13BEE011
else if(t->left&&t->right)
{ temp=findmin(t->right);
t->data=temp->data;
t->right=deletex(t->data,t->right);
}
else
{ temp=t;
if(t->left==NULL)
t=t->right;
if(t->right==NULL)
t=t->left;
free(temp);
}
return t;
}
void main()
{ int ele,ch; tree x; clrscr();
printf("\n enter the elements");
do
{ scanf("%d", &ele);
if(ele!=0)
root=insert(ele,root);
}while(ele!=0);
printf("\n1.preorder display 2.postorder 3.inorder 4.delete 5.insert 6.exit");
do
{ printf("\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
Page:40

13BEE011
printf("\npreorder display");
preorder(root); break;
case 2:
printf("\npostorder display");
postorder(root); break;
case 3:
printf("\ninorder display");
inorder(root);break;
case 4:
printf("\n enter element to be deleted");
scanf("%d", &ele);
x=deletex(ele,root); printf("\n %u",x); break;
case 5:
printf("\n enter element to be inserted");
scanf("%d", &ele);
root=insert(ele,root); break;
}}while(ch!=6);}

Output:

Page:41

13BEE011

Result:
Thus the program for creating binary search tree and manipulating it is successfully compiled and executed.

Expt no: 10
Date: 19.3.15

HEAP SORT

Aim:
To sort the given set of elements using heap sort.

Algorithm:
1.
2.
3.
4.
5.

Start
Read the number of elements n
Read the elements to be sorted as array a.
Call heap sort function with parameters a and n. Go to step 7.
Display sorted elements.
Page:42

13BEE011
6. Stop.
7. Initialise n/2 to i
8. If i>0, go to step 9, else go to step 11.
9. Call percolate down function with a , i, n . go to step 15.
10. Decrement i, go to step 8.
11. Initialise i=n.
12. If j>=2, go to step 13 else go to step 5.
13. Swap a[1] and a[i] to delete maximum term.
14. Call percolate down function with a , 1, i-1 . go to step 15.
15. Initialise temp to a[i].
16. Leftchild is defined as 2*i.
17. If leftchild(i)<=n, go to step 18 else go to step 23.
18. Assign left child(i) to child
19. If child not equal to n and a[child+1]> a[child]
20. Increment child.
21. If temp< a[child], go to step 22, else goto step 23.
22. Assign a[child] to a[i], assign child to i, goto step 17.
23. Assign temp to a[i], go to previous called statement.

Program:
#include<stdio.h>
#include<conio.h>
#define leftchild(i) 2*i
void percdown(int a[], int i,int n);
void swap(int *x, int *y);
void heapsort(int a[],int n)
{ int i;
for(i=n/2;i>0;i--)
percdown(a,i,n);
for(i=n;i>=2;i--)
{ swap(&a[1],&a[i]);
percdown(a,1,i-1);
} }
void percdown(int a[],int i,int n)
{ int child,temp;
for(temp=a[i];leftchild(i)<=n;i=child)
{ child=leftchild(i);
if((child!=n)&&(a[child+1]>a[child]))
child++;
Page:43

13BEE011
if(temp<a[child])
a[i]=a[child];
else
break;
}
a[i]=temp;
}
void swap(int *x,int *y)
{ int temp;
temp=*x;
*x=*y;
*y=temp;
}
void main()
{ int i,n,a[30]; clrscr();
printf("\nenter the size of array");
scanf("%d",&n);
printf("\nenter the elements");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
for(i=1;i<=n;i++)
printf("\t%d",a[i]);
getch();
}

Output:

Page:44

13BEE011

Result:
Thus the program for performing heap sort is successfully compiled and executed.

Expt no: 11
Date: 31.3.15

QUICK SORT

Aim:
To sort the given set of elements using quick sort.

Algorithm:
1.
2.
3.
4.

Start.
Read the number of elements n.
Read the elements to be sorted in array a.
Call quick sort function with parameters a and n. Go to step 7.
Page:45

13BEE011
5. Display the sorted elements.
6. Stop.
7. Call qsort function with a, 0, n-1. Go to step 8.
8. Assign left to i and right to j.
9. If left +3<=right, go to step 10 else go to step 18
10. Call median function with a, left and right and store its retuned value in pivot. Go to step 19.
11. If a[++i]<pivot, go to step 11 else go to step 12
12. If a[--j]>pivot, go to step 12 else go to step 13
13. If i<j, swap a[i] and a[j], go to step 11 else go to step 15
14. Swap a[i] and a[right-1].
15. Call qsort with a, left, i-1, go to step 8.
16. Call qsort with a, i+1,right, go to step 8.
17. Go to step 5.
18. Perform insertion sort for terms between left and right. Go to step 5.
19. Centre is assigned to average of left and right.
20. If a[left]>a[centre], swap a[left] and a[centre]
21. If a[left]>a[right], swap a[left] and a[right]
22. If a[right]<a[centre], swap a[right] and a[centre]
23. swap a[right-1] and a[centre]
24. return a[right-1]
25. go to step 11.

Program:
#include<stdio.h>
#include<conio.h>
#define cutoff 3
void swap(int *a,int *b)
{ int temp;
temp=*a;
*a=*b;
*b=temp;
}
int median(int a[], int left,int right)
{ int centre=(right+left)/2;
if(a[left]>a[centre])
swap(&a[left],&a[centre]);
if(a[left]>a[right])
swap(&a[left],&a[right]);
if(a[right]<a[centre])
swap(&a[right],&a[centre]);
swap(&a[right-1],&a[centre]);
Page:46

13BEE011
return (a[right-1]);
}
void insertionsort(int a[],int left,int right)
{ int j,p,temp;
for(p=left;p<right;p++)
{ j=p;
while((a[j+1]<a[j])&&(j>=left))
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
j--;
}}}
void qsort(int a[], int left, int right)
{ int pivot,i=left,j=right-1;
if(left+cutoff<=right)
{ pivot=median(a,left,right);
for(;;)
{while(a[++i]<pivot)
{}
while(a[--j]>pivot)
{}
if(i<j)
swap(&a[i],&a[j]);
else
break;
}
swap(&a[i],&a[right-1]);
qsort(a,left,i-1);
qsort(a,i+1,right);
}
Page:47

13BEE011
else
insertionsort(a,left,right);
}
void quicksort(int a[],int n)
{ qsort(a,0,n-1);
}
void display(int a[],int n)
{ int i;
for(i=0;i<n;i++)
printf("\t %d", a[i]);
}
void main()
{ int i,n,a[30]; clrscr();
printf("\nenter the size of array");
scanf("%d",&n);
printf("\nenter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,n);
display(a,n);
getch();}

Output:

Page:48

13BEE011

Result:
Thus the program for performing quick sort is successfully compiled and executed.

Page:49

13BEE011

Page:50

Potrebbero piacerti anche