data structure basic and sorting

56
13BEE011 INDEX S.N o DATE TITLE OF THE EXPERIMENT PAGE No. MARKS SIGNATURE OF FACULTY 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:1

Upload: balaji

Post on 13-Jan-2016

17 views

Category:

Documents


0 download

DESCRIPTION

basic data structure program

TRANSCRIPT

Page 1: data structure basic and sorting

13BEE011

INDEX

S.No DATE TITLE OF THE EXPERIMENT PAGE No. MARKSSIGNATURE OF

FACULTY

1. 03.1.15 ARRAY IMPLEMENTATION OF LIST ADT

2. 08.1.15 LINKED LIST IMPLEMENTATION OF LIST ADT

3. 20.1.15 CURSOR IMPLEMENTATION OF LIST ADT

4. 05.2.15 ARRAY IMPLEMENTATION OF STACK ADT

5. 05.02.15 LINKED LIST IMPLEMENTATION OF STACK ADT

6. 17.2.15 BALANCED PARENTHESIS USING STACK ADT

7. 19.2.15 EVALUATING POSTFIX EXPRESSION USING LINKED LIST ADT

8.a 26.2.15 ARRAY IMPLEMENTATION OF QUEUE ADT

8.b. 26.2.15 LINKED LIST IMPLEMENTATION OF QUEUE ADT

9. 04.3.15 SEARCH TREE ADT BINARY SEARCH TREE

10. 19.3.15 HEAPSORT

11. 31.3.15 QUICKSORT

Expt no: 1 Page:1

Page 2: data structure basic and sorting

13BEE011 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.

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()

Page:2

Page 3: data structure basic and sorting

13BEE011{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");

}}

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);

}

Page:3

Page 4: data structure basic and sorting

13BEE011void 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++;

s++;}}

void insertlast()

{ int ele;

printf("enter the no");

scanf("%d",&ele);

if(!isfull())

{ a[count]=ele;

count++; s++;

}

Else

printf("\nlist is empty");

Page:4

Page 5: data structure basic and sorting

13BEE011}

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++)

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)

Page:5

Page 6: data structure basic and sorting

13BEE011return 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.deletemiddle\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;

case 9:insertmiddle();break;

case 10:deletefirst();break;

case 11:deletelast();break;

case 12:deletemiddle();break;

}}while(ch!=13);

getch();

}

Page:6

Page 7: data structure basic and sorting

13BEE011Output:

Result:

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

Expt no: 2

Page:7

Page 8: data structure basic and sorting

13BEE011 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. Start

2. Create of the structures with members data and a pointer variable of the struct data type next

3. Create a linked list in which first member is head and last member is tail

4. Read the choice

5. If choice =1, then call find pos() and go to step 15

6. If choice =2 , then call find ele() and go to step 16

7. If choice=3 , then call insert f() and go to step 17

8. If choice=4 , then call insertl() and go to step 18

9. If choice =5, then call insertaf() and go to step 19

10. If choice=6, then call deletef() and go to step 20

11. If choice=7, then call deletel() and go to step 21

12. If choice=8, then call deleteaf () and go to step 22

13. If choice =9, then call display () an go to step 23

14. If choice=10, then go to step 24

15. Find the element in the given position and go to step 4

16. Find the position of the given element and go to step 4

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;

Page:8

Page 9: data structure basic and sorting

13BEE011}*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)

{ 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)

Page:9

Page 10: data structure basic and sorting

13BEE011{ 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;

}

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)

Page:10

Page 11: data structure basic and sorting

13BEE011{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;

}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");

Page:11

Page 12: data structure basic and sorting

13BEE011 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);

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;

Page:12

Page 13: data structure basic and sorting

13BEE011case 9:

display(); break;

} }while(choice!=10);

}

Output:

Result:

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

Expt no:3

Page:13

Page 14: data structure basic and sorting

13BEE011Date: 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.

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;

Page:14

Page 15: data structure basic and sorting

13BEE011clrscr();

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)

{

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()

Page:15

Page 16: data structure basic and sorting

13BEE011{

int i;

printf("\t\t curser implementation");

printf("\t\t....\n");

printf("\ts.no \t element \t next position\n");

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);

Page:16

Page 17: data structure basic and sorting

13BEE011}

else

{

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;

}}

Page:17

Page 18: data structure basic and sorting

13BEE011curserspace[p].next=curserspace[0].next;

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.

Expt no: 4

Page:18

Page 19: data structure basic and sorting

13BEE011 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);

stack[++top]=k;

} }

void pop()

Page:19

Page 20: data structure basic and sorting

13BEE011 { 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)

{ case 1:pop();break;

case 2:push();break;

case 3:peep();break;

case 4:display();break;}}

while(choice!=5);}

Page:20

Page 21: data structure basic and sorting

13BEE011Output:

Result:

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

Expt no: 5 IMPLEMENTATION OF STACK USING LINKED

Page:21

Page 22: data structure basic and sorting

13BEE011 Date: 5.2.15 LIST

Aim:

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

Algorithm:

1. Start

2. Create of the structures with members data and a pointer variable of the struct data type next

3. Read the choice.

4. If choice=1 then call push(),Go to step 8.

5. If choice=2 then call push(),Go to step 8.

6. If choice =3 then call pop(), Go to step 10

7. If choice=4 then call display (), Go to step 11

8. If choice=5 Go to step12

9. Check for overflow condition and push the read value , Go to step2

10. Check for underflow condition and pop the top element , Go to step3

11. Display the top element go to step2

12. Display the stack with all elements after checking under flow condition Go to step2

13. 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;

temp->next=top;

top=temp;

}

void pop()

{ if(top==NULL)

printf("\nstack underflow");

Page:22

Page 23: data structure basic and sorting

13BEE011else

{ 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)

{ case 1:

printf("\n enter the element to be pushed");

scanf("%d",&ele);

push(ele); break;

case 2:

pop();break;

case 3:

peep(); break;

Page:23

Page 24: data structure basic and sorting

13BEE011case 4:

display();

break;

}

}while(choice!=5);

}

Output:

Result:

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

Expt no:6

Page:24

Page 25: data structure basic and sorting

13BEE011Date: 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. Start

2. Read the expression exp.

3. True is defined as 1 and false is defined as 0.

4. Initialise valid as true

5. If exp[i] is not equal to ‘\0’.

6. If exp[i] is’(‘ or ‘[‘ or ‘{‘

7. Push exp[i] in to the stack.

8. If exp[i] is’)‘ or ‘]‘ or ‘}‘

9. If top is -1, valid is false go to step 14

10. Else pop the element and store it in temp.

11. If exp[i] is ‘)’ and temp is ‘{‘ or ‘[‘, change valid to false.

12. If exp[i] is ‘]’ and temp is ‘{‘ or ‘(‘, change valid to false.

13. If exp[i] is ‘}’ and temp is ‘(‘ or ‘[‘, change valid to false.

14. Increment i, go to step 5.

15. If top>=0, valid is false.

16. If valid is true , it is valid expression.

17. Else it is invalid expression.

18. Stop.

Program:

#include<stdio.h>

#include<conio.h>

#define max 20

#define true 1

#define false 0

int top=-1;

int stack[max];

void push(char item)

{ if (top==(max-1))

printf("stack overflow");

else

Page:25

Page 26: data structure basic and sorting

13BEE011{ 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();

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;

Page:26

Page 27: data structure basic and sorting

13BEE011if(valid==true)

printf("valid expression \n");

else

printf("invalid expression\n);

getch();}

Output:

Result:

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

Page:27

Page 28: data structure basic and sorting

13BEE011Expt no: 7 EVALUATING POSTFIX EXPRESSION USING

Date: 17.2.15 LINKED LIST

Aim:

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

Algorithm:

1. Start

2. Declare a structure with an integer and a pointer variable and objects.

3. Get a postfix expression as input.

4. Initialise i=0.

5. If expr[i] is not equal to’\ 0’.

6. If expr[i] is digit , then push it into the stack.

7. Increment i and go to step 5.

8. If expr[i] is not digit, pop twice and save it in two variables.

9. If expr[i]=’+’, then add two elements

10. If expr[i]=’-’, then subtract two elements

11. If expr[i]=’*’, then multiply two elements

12. If expr[i]=’/’, then divide two elements

13. Increment i, goto step 5.

14. Print the element in the stack.

15. 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;

temp->next=top;

top=temp;

}

Page:28

Page 29: data structure basic and sorting

13BEE011stack 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);

getch();

}

Output:

Result:

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

Page:29

Page 30: data structure basic and sorting

13BEE011Expt 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. Start

2. Create an array named queue for defined maxsize

3. Read the choice

4. If choice =1,call enqueue() and Go to 7

5. If choice =2, call dequeue () and Go to 8

6. If choice=3, call display() and Go to 9

7. If choice=4 Go to 10

8. Add the given element to rear end of queue Go to 2

9. Remove the front element of queue Go to 2

10. Display all element Go to 2

11. 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++;

if(rear==maxsize-1)

printf("overflow");

else

{rear++;

queue[rear]=k;

}}

void dequeue()

{ if(front==-1||front=rear+1)

printf("overflow");

Page:30

Page 31: data structure basic and sorting

13BEE011 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)

{

case 1:

scanf("%d",&ele);

enqueue();break;

case 2:dequeue();break;

case 3:display();break;

}}

while(choice!=4);

}

Page:31

Page 32: data structure basic and sorting

13BEE011Output:

Result:

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

Page:32

Page 33: data structure basic and sorting

13BEE011Expt 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. Start

2. Create of the structures with members data and a pointer variable of the struct data type next

3. Create an array named queue for defined maxsize

4. Read the choice

5. If choice =1,call enqueue() and Go to 7

6. If choice =2, call dequeue () and Go to 8

7. If choice=3, call display() and Go to 9

8. If choice=4 Go to 10

9. Add the given element to rear end of queue Go to 2

10. Remove the front element of queue Go to 2

11. Display all element Go to 2

12. Stop

Program:

#include<stdio.h>

#include<conio.h>

struct node

{ 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;

Page:33

Page 34: data structure basic and sorting

13BEE011rear=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;

}}}

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;

Page:34

Page 35: data structure basic and sorting

13BEE011case 3:

display(); break;

}}while(choice!=4);

}

Output:

Result:

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

Page:35

Page 36: data structure basic and sorting

13BEE011Expt 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. Start

2. Declare a structure with data , left and right address inn name of node.

3. Read the elements and pass it to insert function unless the element is zero. Go to step 12.

4. Read the choice

5. If choice is 1, call preorder function , go to step 13.

6. If choice is 2, call postorder function , go to step 14.

7. If choice is 3, call inorder function , go to step 15.

8. If choice is 4, read the element and call delete function , go to step 16.

9. If choice is 5, read the element and call insert function , go to step 12.

10. If choice is 6, go to step 11.

11. Stop.

12. 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.

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;

Page:36

Page 37: data structure basic and sorting

13BEE011tree 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);

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)

Page:37

Page 38: data structure basic and sorting

13BEE011{ 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);

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();

Page:38

Page 39: data structure basic and sorting

13BEE011printf("\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:

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);}

Page:39

Page 40: data structure basic and sorting

13BEE011Output:

Result:

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

Page:40

Page 41: data structure basic and sorting

13BEE011Expt no: 10

Date: 19.3.15 HEAP SORT

Aim:

To sort the given set of elements using heap sort.

Algorithm:

1. Start

2. Read the number of elements n

3. Read the elements to be sorted as array a.

4. Call heap sort function with parameters a and n. Go to step 7.

5. Display sorted elements.

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)

Page:41

Page 42: data structure basic and sorting

13BEE011{ 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++;

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++)

Page:42

Page 43: data structure basic and sorting

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

heapsort(a,n);

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

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

getch();

}

Output:

Result:

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

Page:43

Page 44: data structure basic and sorting

13BEE011Expt no: 11

Date: 31.3.15 QUICK SORT

Aim:

To sort the given set of elements using quick sort.

Algorithm:

1. Start.

2. Read the number of elements n.

3. Read the elements to be sorted in array a.

4. Call quick sort function with parameters a and n. Go to step 7.

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)

Page:44

Page 45: data structure basic and sorting

13BEE011{ 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]);

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)

Page:45

Page 46: data structure basic and sorting

13BEE011{}

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);

}

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);

Page:46

Page 47: data structure basic and sorting

13BEE011getch();}

Output:

Result:

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

Page:47

Page 48: data structure basic and sorting

13BEE011

Page:48

Page 49: data structure basic and sorting

13BEE011

Page:49