Download doc - Ds Lab Manual

Transcript
Page 1: Ds Lab Manual

GNANAMANI COLLEGE OF TECHNOLOGYDEPARTMENT OF INFORMATION TECHNOLOGY

DATA STRUCTURES LAB MANUAL

PROGRAM:1 \ \ ARRAY IMPLEMENTATON OF LIST ADT \\

AIM : To write a C program for array implementation of list ADT.

ALGORITHM:

1. Start the program.

2. Create an array with some elements by specifying the size of array.

3. Using insert function, insert the element in the array and push the element forward.

4. Display the elements of array.

5.Using delete function deletes the corresponding element and pop the element backward.

6. Display the elements of array.

7. Stop the program.

Page 2: Ds Lab Manual

PROGRAM:

#include<stdio.h>#include<conio.h>int a[10],b,c,d,f,i,n,t;void display();void insert();void delete();void main(){

int ch;clrscr();printf("Enter the size of the array :");scanf("%d",&n);printf("Enter the element :");for(i=0;i<n;i++)scanf("%d",&a[i]);do{

printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");printf("Enter your choice :\n");scanf("%d",&ch);

switch(ch) {

case 1:insert();break;

case 2:delete();break;

case 3:display();break;

case 4:exit(0);

}}while(ch<=3);}

Page 3: Ds Lab Manual

void insert() {

printf("Enter the element to be inserted :");scanf("%d",&b);printf("Enter the position to be inserted :");scanf("%d",&c);for(i=n-1;i>=c-1;i--)

a[i+1]=a[i];a[c-1]=b;n++;

}void display(){

printf("The array element are\n");for(i=0;i<n;i++)printf("%d\n",a[i]);

}void delete(){

printf("Enter the position to be deleted :");scanf("%d",&c);printf("The deleted element is%d\n",a[c-1]);for(i=c;i<n;i++)

a[i-1]=a[i];n--;

}

Page 4: Ds Lab Manual

OUTPUT :

Enter the size of the array : 3Enter the elements :1223521.Insert2.Delete3.Display4.Exit

Enter the choice : 1Enter the element to be inserted : 30Enter the position to be inserted : 3

1.Insert2.Delete3.Display4.ExitEnter the choice : 3

The list of elements are:12233052

1.Insert2.Delete3.Display4.ExitEnter the choice : 2Enter the position to be deleted : 2

Page 5: Ds Lab Manual

The deleted element is 231.Insert2.Delete3.Display4.ExitEnter the choice : 3

The list of elements are:123052

1.Insert2.Delete3.Display4.ExitEnter the choice : 4

RESULT: Thus the given C program has been executed successfully and the output is verified.

Page 6: Ds Lab Manual

PROGRAM : 2 \\LINKED LIST IMPLEMENTATION OF LIST ADT\\

AIM : To write a C program to perform all possible operation in singly linked list .

ALGORITHM:

1. Start the program.

2. Allocate a memory space for a node using malloc function.

3. Get the data to be inserted.

4. If the header node is fixed, insert the data as a first element otherwise insert

in a subsequent position.

5. To delete the particular node, get the data from the user and check whether the

data is found in the list, if not display data not found.

6. Check if node is null, then display no records to delete.

7. Othrwise delete the particular node.

8. Display the elements in the list.

9. Initialize count to zero , transfer the element and increment the count.

10. Get the element to be searched.

11. If the head is null then display no data in the list.

12. Otherwise check the data within particular element which is present in the list or not.

13. If it is present display stack element is found otherwise display element not found.

14. Stop the program.

Page 7: Ds Lab Manual

PROGRAM:

#include<stdio.h>#include<conio.h>#include<ctype.h>#define NULL0struct info{int data;struct info *next;};struct info *temp,*head,*disp;void add();void delete();void display();void search();int size();void main(){int choice;clrscr();do{printf("\n......singly linked list.....");printf("\n1 add record \n 2 delete record \n 3 display record \n 4size \n 5 search \n 6 exit \n");printf("enter your choice");scanf("%d",&choice);switch(choice){case 1:

add();break;

case 2:delete();break;

case 3:display();break;

case 4:printf("\n the size of the list is %d",size());break;

case 5:search();break;

Page 8: Ds Lab Manual

case 6:exit(0);

}}while(choice<=5);getch();}void add(){struct info *add;char proceed='Y';while(toupper(proceed)=='Y'){add=(struct info*)malloc(sizeof (struct info));printf("enter data ");scanf("%d",&add->data);fflush(stdin);if(head==NULL){head=add;add->next=NULL;temp=add;}else{temp->next=add;add->next=NULL;temp=add;}printf("\n want to proceed Y\n");proceed=getchar();fflush(stdin);}}

void delete(){struct info *curr,*prev;int tdata;if(head==NULL){printf("\n no records to delete ");return;}

Page 9: Ds Lab Manual

printf("enter data to delete");scanf("%d",&tdata);fflush(stdin);prev=head;curr=head;while((curr!=NULL)&&(curr->data!=tdata)){prev=curr;curr=curr->next;}if(curr==NULL){printf("data not found");return;}if(curr==head)head=head->next;{prev->next=curr->next;if(curr->next==NULL)temp=prev;}free(curr);}void display(){if(head==NULL){printf("no data to display");return;}printf("data");for(disp=head;disp!=NULL;disp=disp->next){printf("\t%d",disp->data);}}int size(){int count=0;if(head==NULL){return count;}

Page 10: Ds Lab Manual

for(disp=head;disp!=NULL;disp=disp->next)count++;return count;}void search(){int sitem,found=0;if(head==NULL){printf("nothing to search");return;}printf("enter no to search");scanf("%d",&sitem);for(disp=head;disp!=NULL&&found==0;disp=disp->next){if(disp->data==sitem)found=1;}if(found==0)printf("search no is not found in the list");elseprintf("search no is found in the list");return;}

Page 11: Ds Lab Manual

OUTPUT :

……………Singly Linked List………….

1- Add Record2- Delete Record3- Display Record4- Size5- Exit……………Singly Linked List………….

Enter your choice : 1

Enter data : 20

Want to proceed y/n y1- Add Record2- Delete Record3- Display Record4- Size5- Exit……………Singly Linked List………….

Enter data : 40

Want to proceed y/n n1- Add Record2- Delete Record3- Display Record4- Size5- Exit……………Singly Linked List………….

Enter your choice : 3

Data 20 40

1- Add Record2- Delete Record3- Display Record4- Size5- Exit

Page 12: Ds Lab Manual

Enter your choice : 4

The size of list is 2

1- Add Record2- Delete Record3- Display Record4- Size5- Exit……………Singly Linked List………….

1- Add Record2- Delete Record3- Display Record4- Size5- ExitEnter your choice : 2

Enter data to delete 20

……………Singly Linked List………….

1- Add Record2- Delete Record3- Display Record4- Size5- ExitEnter your choice : 5

RESULT: : Thus the given C program has been executed successfully and the output is verified.

Page 13: Ds Lab Manual

PROGRAM : 3 // CURSOR IMPLEMENTATION OF LIST ADT //

AIM : To write a C program to perform all possible operation in singly linked list .

ALGORITHM:

1. Start the program

2. Declare the struct variables.

3. Create list

i) initialize head = NULL.

ii) Get the new node and assign new node = get node

iii) Check the memory is free or not.

4. Insert first

i) Declare head, new node

ii) Assign head = * head ptr

iii) Get the new node

iv) Check the memory is free or not

5. Insert last

i) Declare head,last, new node

ii) Assign head = * head ptr

iii) Get the new node

iv) Check the memory is free or not

6. Insert middle

i) Declare head,last, new node, insdata

ii) Assign head = * head ptr

iii) Check the list is empty,so new node is head node.

7. Stop the program.

Page 14: Ds Lab Manual

PROGRAM :

#include<stdio.h>#include<conio.h>#define NULL 0typedef struct cursor{char element;int next;};struct cursor list[10];void create(struct cursor[]);void show();int n,pos;void main(){clrscr();printf("\n Cursor implementation:\n");create(list);printf("\nn");show(list);getch();}void create(struct cursor list[]){int i,p;list[0].element='0';printf("\n Enter the size of the list:\n");scanf("\n%d",&n);list[0].next=n;for(i=1;i<=n;i++){if(i==n){printf("\n Enter the element('@'as header):");list[pos].element=getche();}

Page 15: Ds Lab Manual

else{printf("\n Enter the element('@'as header);");list[pos].element=getche();p=pos;printf("\n\nEnter the position of the next elment:\n");scanf("\n%d",&pos);list[p].next=pos;}}}void show(struct cursor list[]){int i;printf("\nRecord \tElement\tposition\n");for(i=0;i<n;i++)printf("\n%4d%6c%8d\n",i+1,list[i].element,list[i].next);}

Page 16: Ds Lab Manual

OUTPUT :

Cursor implementation:

Enter the size of the list:3

Enter the element ('@'as header); 2

Enter the position of the next element:1

Enter the element ('@'as header); 2

Enter the position of the next element:2

Enter the element ('@'as header):3

Record Element position

1 2 1

2 2 2

3 3 0

RESULT: : Thus the given C program has been executed successfully and the output is verified.

Page 17: Ds Lab Manual

PROGRAM: 4 \\ARRAY IMPLEMENTATION OF STACK ADT\\

AIM : To write a C program for the array implementation of stack ADT.

ALGORITHM:

1. Start the program.

2. Declare an array stack of size N.

3. Assign top as a pointer to denote the top element in the stack.

4. Get the new element to add into the stack.

5. If top = size then display the stack as full.

6. Otherwise increment the top by 1 and place the new element in that position.

7. To delete top element from stack check if top = 0, then display stack is

empty otherwise decrement top by 1.

8. Display the elements present in the stack

9. Stop the program.

Page 18: Ds Lab Manual

PROGRAM:

#include<stdio.h>#include<conio.h>#define size 3void push();void pop();void display();int S[size],top=-1;void main(){int choice;clrscr();do{printf("\n.....stack operations...");printf("\n1-push\n2-pop\n3-display\n4-exit\n");printf("enter your choice");scanf("%d",&choice);switch(choice){case 1:push();break;case 2:pop();break;case 3:display();break;case 4:exit(0);}}while(choice<4);}void push(){int no;if(top==size-1)printf("stack is full");else{printf("enter the number");scanf("%d",&no);top++;

Page 19: Ds Lab Manual

S[top]=no;}}void pop(){int no;if(top==-1)printf("\nstack is empty");else{no=S[top];printf("The element%d is popped out from the stack",no);top--;}}void display(){int i;if(top==-1){printf("stack is empty");return;}printf("the element in the stack are\n");for(i=top;i>=0;i--)printf("%d\n",S[i]);}

Page 20: Ds Lab Manual

OUTPUT :

1. Push2. Pop3. Display4. ExitEnter your choice : 1Enter the numbers : 13

1. Push2. Pop3. Display4. ExitEnter your choice : 1Enter the numbers : 34

Enter your choice : 3The elements in stack are3413

1. Push2. Pop3. Display4. ExitEnter your choice : 1Enter the numbers : 56

Enter your choice : 3The elements in stack are563413

1. Push2. Pop3. Display4. ExitEnter your choice : 1

Stack is full

Page 21: Ds Lab Manual

1. Push2. Pop3. Display4. ExitEnter your choice : 256 is popped out from the stack

Enter your choice : 3The elements in stack are3413

1. Push2. Pop3. Display4. ExitEnter your choice : 234 is popped out from the stack

Enter your choice : 3The elements in stack are13

1. Push2. Pop3. Display4. ExitEnter your choice : 213 is popped out from the stack

Enter your choice : 3Stack is empty1. Push2. Pop3. Display4. ExitEnter your choice : 4

RESULT : Thus the given C program has been executed successfully and the output is verified.

Page 22: Ds Lab Manual

PROGRAM: 5 \\LINKED LIST IMPLEMENTATION OF STACK ADT\\

AIM : To write a C program to perform all possible operation in singly linked list .

ALGORITHM:

1. Start the program.

2. Declare the variables.

3. Create a singly linked list using struct.

4. To push the node

i) Define memory allocation process

ii) Check the if condition and assign newnode -> item =x, newnode ->link = top.

4. Assign last node to the new node.

5. To pop a new node delete the last node and set the previous node to the

last node link to NULL.

6. Declare stack elements and print the stack elements.

7. Stop the program.

Page 23: Ds Lab Manual

PROGRAM:

#include<stdio.h>#include<conio.h>#include<stdlib.h>struct stack{int no;struct stack *next;}*p,*q,*temp,*head=NULL;void main(){void push();void pop();void display();int ch;clrscr();do{printf("\n............Stack operator using linked list………");printf("\n1.push\n2.pop\n3.display\n4.exit\n");printf("enter your choice");scanf("%d",&ch);switch(ch){case 1:push();break;case 2:pop();break;case 3:display();break;case 4:exit(0);break;}}while(ch<=3);}void push(){

Page 24: Ds Lab Manual

temp=(struct stack*)malloc(sizeof(struct stack));printf("enter the number");scanf("%d",&temp->no);if(head==NULL){head=temp;temp->next=NULL;}else{temp->next=head;head=temp;}printf("\n element %d is pushed to the stack", temp->no);}void pop(){p=head;if(head==NULL)printf("stack is empty");else{head=head->next;printf("the element %d is popped out from the stack",p->no);free(p);}}void display(){if(head==NULL){printf("stack empty");return;}elsep=head;printf("the elements in the stack are...");while(p!=NULL){printf("%5d",p->no);p=p->next;}}

Page 25: Ds Lab Manual

OUTPUT :

............Stack operator using linked list………1. Push2. Pop3. Display4. ExitEnter your choice : 1Enter the numbers : 12

Element 12 is pushed in to the stack

............Stack operator using linked list………1. Push2. Pop3. Display4. ExitEnter your choice : 1Enter the numbers : 13

Element 13 is pushed in to the stack

............Stack operator using linked list………

1. Push2. Pop3. Display4. ExitEnter your choice : 2Element 13 is popped out from the stack............Stack operator using linked list………1. Push2. Pop3. Display4. ExitEnter your choice : 1The elements in stack are 12............Stack operator using linked list………1. Push2. Pop3. Display4. ExitEnter your choice : 4

RESULT : : Thus the given C program has been executed successfully and the output is verified.

Page 26: Ds Lab Manual

PROGRAM: 7

\\CHECKING THE BALANCE OF PARENTHSIS USING LINGED LIST IMPLEMENTATION OF STACK ADT\\

AIM : To write a C program to Check the balance of parenthesis using linked list implementation of stack ADT.

ALGORITHM:

1. Start the program.

2. Find the string length of that expressions.

3. Whenever a left parenthesis is encountered push to stack.

4. Whenever a right parenthesis is encountered check whether the stack is empty.

5. If so the expression is unbalanced with left paranthesis, Otherwise pop the

left parenthesis from the stack.

6. At the end of processing check whether the stack is empty if so then it is balanced.

7. Otherwise it is unbalanced with right parenthesis.

8. Stop the program.

Page 27: Ds Lab Manual

PROGRAM :

#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>#define null 0#include”linkstack.h”Void main(){Char expr[20]; Char ch;Int I,len;Clrscr();Printf(“enter the expression:\t”);Scanf(“%s”,&expr);Len=strlen(expr);For(i=0;i<len;i++){If(expr[i]==’(‘)Push(expr[i]);If(expr[i]==’)’)Pop();}If(top==NULL)Printf(“balanced expression”);ElsePrintf(“imbalanced right parenthesis”);Getch();}

HEADER FIL CREATION FOR BALANCED PARENTHSIS#define NULL 0Stuct node{int data;struct node *next;}*top=NULL;Void push(char);Void pop();

Page 28: Ds Lab Manual

Void push(char item){Struct node *newnode;Newnode=(struct node*)malloc(size of(struct node));Newnode->data=item;If(top==NULL){Newnode->next=NULL;Top=newnode;}Else{Newnode->next=top;Top=newnode;}}Void pop(){If(top==NULL){Printf(“imbalanced left paranthesis\n”);Exit(0);}ElseTop=top->next;Getch();}

Page 29: Ds Lab Manual

OUTPUT :

Enter the expression a+b)The left parenthesis is missing

Enter the expression (a+bThe right parenthesis is missing

Enter the expression (a+b)The expression is balanced.

RESULT: Thus the given C program has been executed successfully and the output is verified.

Page 30: Ds Lab Manual

PROGRAM : 6

\\CHECKING THE BALANCE OF PARENTHESIS USING ARRAY IMPLEMENTATION OF STACK ADT\\

AIM : To write a C program to check the balance of the parenthesis in an arithmetic expression using array implementation of stack ADT..

ALGORITHM:

1. Start the program.

2. Get the arithmetic expression with parenthesis in a string array.

3. Process the expression character by character till the end of the string is encountered.

4. Whenever a left parenthesis is encountered then push it into the stack.

5. Whenever a right parenthesis is encountered check whether the stack is empty

6. If,so the expression is unbalanced with left parenthesis then stop the further processing.

7. Otherwise pop the left parenthesis in from stack.

8. At the end of processing check the status of the stack.

9. If the stack is empty then the expression is balanced with parenthesis.

10. Otherwise the expression is unbalanced with right parenthesis missing.

11.Stop the program.

Page 31: Ds Lab Manual

PROGRAM : #include<stdio.h>#include<conio.h>#define size 10char stack[size];void push(char ch);void pop();int top=-1;void main(){char str[20];int i=0;clrscr();printf("\nenter the arithematic expression\n");gets(str);for(i=0;str[i]!='\0';i++){if(str[i]==('('))push('(');if(str[i]==(')'))pop();}if(top==-1)printf("\nthe expression is balanced\n");elseprintf("\nright parenthesis is missing\n");getch();}void push(char ch){top++;stack[top]=ch;}void pop(){if(top==-1){printf("\nleft parentesis missing\n");getch();exit(0);}top--;}

Page 32: Ds Lab Manual

OUTPUT :

Enter the expression

(a*b)+(c*d)

Expression Is balanced

Enter the expression

(a*b+c

Right parenthesis is missing

Enter the expression

a*b)+c

Left parenthesis is missing

RESULT : Thus the given C program has been executed successfully and the output is verified.

Page 33: Ds Lab Manual

PROGRAM : 8

\\EVALUATION OF POSTFIX EXPRESSION USING LINKED LIST IMPLEMENTATIONOF STACK ADT\\

AIM : To write a C program to implement the postfix expression using linked list.

ALGORITHM:

1. Start the program.

2. Declare the variable

3. Scan the expression from left to right.

4. If the operand encountered push it into the stack.

5. If an operator <op> is encountered ,

i) Remove ( pop ) the top element and also the element next to the top from the stack.

ii) Evaluate the operands using the operators.

iii) Push back the result again into stack.

6. Repeat step 4 and step 5 until the program ends.

7. Stop the program.

Page 34: Ds Lab Manual

PROGRAM :

#include<stdio.h>#include<conio.h>#include<ctype.h>int top;float stack[80];void main(){void push(char);float pop();float answer,val[10];float operand1,operand2;int i;char str[80],opr;top=-1;clrscr();puts("Enter the postfix expression");gets(str);for(i=0;str[i]!='\0';++i){if(isalpha(str[i])){printf("Enter the value for the variable %c",str[i]);scanf("%f",&val[i]);fflush(stdin);}}for(i=0;str[i]!='\0';++i){opr=str[i];if(isalpha(str[i]))push(val[i]);else{operand2=pop();operand1=pop();switch(opr){case'+':push(operand1+operand2);break;case'-':push(operand1-operand2);break;

Page 35: Ds Lab Manual

case'*':push(operand1*operand2);break;case'/':push(operand1/operand2);break;}}}answer=pop();printf("The answer for the given post fix expression is = %1f",answer);getch();}void push(char ch){stack[++top]=ch;}float pop(){float no;no=stack[top--];return no;}

Page 36: Ds Lab Manual

OUTPUT :

Enter the postfix expression : abc*+d

Enter the value for the variable a : 1Enter the value for the variable b : 2Enter the value for the variable c : 3Enter the value for the variable d : 4

The answer for the given post fix expression is : 3

RESULT: Thus the given C program has been executed successfully and the output is verified.

Page 37: Ds Lab Manual

PROGRAM : 9 \\ARRAY IMPLEMENTATION OF QUEUE ADT\\

AIM : To write a C program to implement Queue using array.

ALGORITHM:

1. Start the program.

2. Declare an array as queue with size N.

3. Declare front and rear pointers as variable to denote the front and rear position

in the queue.

4. Get the new element to be added into the queue.

5. If rear = size, then display ,” Queue is full”

6. Otherwise increment rear by 1 and place new element in that position.

7. To delete the element from the queue check if front = rear, then display

“ queue is empty “.

8. Otherwise increment front by 1 and display the elements.

7. Stop the program.

Page 38: Ds Lab Manual

PROGRAM :

#include<stdio.h>#include<conio.h>#define size 5void enqueue();void dequeue();void display();int q[size],front=-1,rear=-1;void main(){int choice;clrscr();do{printf("\n...........Queue implementation........\n");printf("\n1-add the element\n2-delete the element\n3-display\n4-exit\n");printf("Enter your choice");scanf("%d",&choice);switch(choice){case 1:enqueue();break;case 2:dequeue();break;case 3:display();break;case 4:exit(0);}}while(choice<4);}void enqueue(){int no;if(rear==size-1){printf("\nQueue is full");return;}

Page 39: Ds Lab Manual

else{printf("Enter the no to be inserted");scanf("%d",&no);rear++;q[rear]=no;}}void dequeue(){int no,i;if(front==rear)printf("\n Queue is empty");else{front++;no=q[front];printf("The no %d is removed from the queue \n",no);}}void display(){int i;if(front==rear)printf("\nqueue is empty\n");else{printf(" Element in the queue are\n");for(i=front+1;i<=rear;i++)printf("\n%d\t",q[i]);}}

Page 40: Ds Lab Manual

OUTPUT :

...........Queue implementation........

1. add element2. delete element3. display elements4. exit

Enter your choice : 1Enter the no to be inserted :11

1. add element2. delete element3. display elements4. exit

Enter your choice : 1Enter the no to be inserted :22

1. add element2. delete element3. display elements4. exit

Enter your choice : 1Enter the no to be inserted :33

1. add element2. delete element3. display elements4. exit

Enter your choice : 1

Queue is full

1. add element2. delete element3. display elements

Page 41: Ds Lab Manual

4. exit

Enter your choice : 1

Elements in the queue are 11 22 33

1. add element2. delete element3. display elements4. exit

Enter your choice : 2The no 11 is removed from the queue.

1. add element2. delete element3. display elements4. exit

Enter your choice : 2The no 22 is removed from the queue

1. add element2. delete element3. display elements4. exit

Enter your choice : 2The no 33 is removed from the queue

Enter your choice : 3

Queue is empty1. add element2. delete element3. display elements4. exit

Enter your choice : 4

RESULT: Thus the given C program has been executed successfully and the output is verified.

Page 42: Ds Lab Manual

PROGRAM   :1 0 /* IMPLEMENTATION OF BINARY SEARCH TREE */

AIM : To write a C program to implement binary search tree operation.

ALGORITHM:

1. Start the program.

2. Declare the struct variables.

3. Create new node to be inserted with value of vertex.

4. Check for empty tree

i) Check if (tree == NULL) then Print tree is empty.

ii) Insert new node as root node

Assign tree -> = V

Assign tree -> left = NULL

Assign tree -> right = NULL

else tree not empty

5. Compare new node value V with root value

i) Check if (V < tree -> value )

tree-> left = insert ( tree-> left, V)

else if( V> tree -> value ) then

tree -> right = insert (tree -> right , V)

else if( V == tree -> value) then

Print duplicate nodes.

6. Stop the program.

Page 43: Ds Lab Manual

PROGRAM :

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

typedef struct bst

{

int data;

struct bst *left,*right;

}node;

void insert(node *,node *);

void inorder(node *);

node *search(node *,int,node **);

void del(node *,int);

void main()

{

int choice;

char ans='n';

int key;

node *newn,*root,*tmp,*parent;

node *get_node();

root=NULL;

clrscr();

printf("\n\t program for binary search tree");

do

{

printf("\n1.create\n2.search\n3.delete\n4.display\n5.exit");

printf("\n\nenter your choice");

scanf("%d",&choice);

switch(choice)

Page 44: Ds Lab Manual

{

case 1:

do

{

newn=get_node();

printf("\nEnter the element!");

scanf("%d",&newn->data);

if(root==NULL)

root=newn;else

insert(root,newn);

printf("\nDo u want to enter more elements?Y/n)");

ans=getch();;

}while(ans=='y');

break;

case 2:

printf("\nEnter the element to search!");

scanf("%d",&key);

tmp=search(root,key,&parent);

printf("\nParent of node %d is %d",tmp->data,parent->data);

break;

case 3:

printf("\nEnter the element to delete!");

scanf("%d",&key);

del(root,key);

break;

case 4:

if(root==NULL)

printf("\nTree is not created");

else

{

Page 45: Ds Lab Manual

printf("\nThe tree is:");

inorder(root);

}

break;

}

}while(choice!=5);

}

node *get_node()

{

node *temp;

temp=(node*)malloc(sizeof(node));

temp->left=NULL;

temp->right=NULL;

return temp;

}

void insert(node *root,node *newn)

{

if(newn->data<root->data)

{

if(root->left==NULL)

root->left=newn;

else

insert(root->left,newn);

}

if(newn->data>root->data)

{

if(root->right==NULL)

root->right=newn;

else

insert(root->right,newn);

}

Page 46: Ds Lab Manual

}

node *search(node *root,int key,node **parent)

{

node *temp;

temp=root;

while(temp!=NULL)

{

if(temp->data==key)

{

printf("\nThe %d element is parent",temp->data);

return temp;

}

*parent=temp;

if(temp->data>key)

temp=temp->left;

else

temp=temp->right;

}

return NULL;

}

void del(node *root,int key)

{

node *temp,*parent,*temp_succ;

temp=search(root,key,&parent);

if(temp->left!=NULL&&temp->right!=NULL)

{

parent=temp;

temp_succ=temp->right;

while(temp_succ->left!=NULL)

{

parent=temp_succ;

Page 47: Ds Lab Manual

temp_succ=temp_succ->left;

}

temp->data=temp_succ->data;

parent->left=NULL;

printf("now deleted it!");

return;

}

if(temp->left!=NULL&&temp->right==NULL)

{

if(parent->left==temp)

parent->left=temp->left;

else

parent->right=temp->left;

temp=NULL;

free(temp);

printf("now deleted it!");

return;

}

if(temp->left==NULL&&temp->right!=NULL)

{

if(parent->left==temp)

parent->left=temp->right;

else

parent->right=temp->right;

temp=NULL;

free(temp);

printf("now deleted it!");

return;

}

if(temp->left==NULL&&temp->right==NULL)

{

Page 48: Ds Lab Manual

if(parent->left==temp)

parent->left=NULL;

else

parent->right=NULL;

printf("now deleted it!");

return;

}

}

void inorder(node *temp)

{

if(temp!=NULL)

{

inorder(temp->left);

printf("%d->",temp->data);

inorder(temp->right);

}

}

Page 49: Ds Lab Manual

OUTPUT:

program for binary search tree

1.create

2.search

3.delete

4.display

5.exit

enter your choice1

Enter the element!4

Do u want to enter more elements?Y/n)

Enter the element!7

Do u want to enter more elements?Y/n)

Enter the element!2

Do u want to enter more elements?Y/n)

Enter the element!5

Do u want to enter more elements?Y/n)

Enter the element!8

Do u want to enter more elements?Y/n)

1.create

2.search

3.delete

4.display

Page 50: Ds Lab Manual

5.exit

enter your choice4

The tree is:2->4->5->7->8->

1.create

2.search

3.delete

4.display

5.exit

enter your choice2

Enter the element to search!8

The 8 element is parent

Parent of node 8 is 7

1.create

2.search

3.delete

4.display

5.exit

enter your choice3

Enter the element to delete!4

The 4 element is parent now deleted it!

1.create

2.search

3.delete

4.display

5.exit

enter your choice4

The tree is:2->5->7->8->

Page 51: Ds Lab Manual

1.create

2.search

3.delete

4.display

5.exit

enter your choice5

RESULT: : Thus the given C program has been executed successfully and the output is verified.

Page 52: Ds Lab Manual

PROGRAM : 11 \\IMPLEMENTATION TO HEAP SORT\\

AIM : To write a C program to implement heap sort.

ALGORITHM:

1. Start the program.

2. Declare the variables and build the heap.

3. Repeat through step k=2,3 upto n.

4.Initialize I = k and heap = x[k].

5. Obtain the parent element and place the new element in the existing key.

6. Repeat through step 8 while (i<1) and (temp < x[i]).

7. Interchange the element

8. Obtain next parent.

9. Copy new element value into its proper place and return.

10. Sort the element using heap sort.

11. Call build heap and repeat the steps k= n,n=1 to2.

12. Exchange elements, construct new key, interchange elements, obtain the left child.

13.Stop the program.

Page 53: Ds Lab Manual

PROGRAM:

#include<stdio.h>#include<conio.h>int x[100],size,i;void main(){void buildheap();void heapsort();clrscr();printf("\n Enter the no of elements \n");scanf("%d",&size);printf("Enter %d elements",size);for(i=1;i<=size;++i)scanf("%d",&x[i]);buildheap();heapsort();printf("\nSorted elements are:\n");for(i=1;i<=size;++i)printf("%d",x[i]);getch();}void buildheap(){int j,k,temp;for(k=2;k<size;++k){i=k;temp=x[k];j=i/2;while((i>1)&&(temp>x[j])){x[i]=x[j];i=j;j=i/2;if(j<1)j=1;}}x[i]=temp;}void heapsort()

Page 54: Ds Lab Manual

{int j,k,temp,value;for(k=size;k>=2;--k){temp=x[1];x[1]=x[k];x[k]=temp;i=1;value=x[1];j=2;if((j+1)<k)if(x[j+1]>x[j])j++;while((j<=(k-1))&(x[j]>value)){x[i]=x[j];i=j;j=2*i;if(j<k)if(x[j+1]>x[j])j++;elseif(j>size)j=size;x[i]=value;}}}

Page 55: Ds Lab Manual

OUTPUT:

Enter the no of elements3Enter 3 elements126530

Sorted elements are:12 30 65

RESULT: Thus the given C program has been executed successfully and the output is verified.

Page 56: Ds Lab Manual

PROGRAM : 12 \\QUICK SORT IMPLEMENTATION\\

AIM : To write a C program to implement Quick sort.

ALGORITHM:

1. Start the program.

2. Declare the variables.

3. Get the number of elements and the elements to be sorted.

4. If first < last, assign first as pivot.

5. To the left of pivot, check elements which are lesser than pivot.

6. To the right of pivot, check elements which are greater than pivot.

7. Take the left segment as a separate array and repeat the steps 3 to 5.

8. Take the right segment as a separate array and repeat the steps 3 to 5.

9. Display the sorted array.

10. Stop the program.

Page 57: Ds Lab Manual

PROGRAM :

#include<stdio.h>#include<conio.h>int x[100],items,i;void main(){clrscr();printf("\n....QUICK SORT.......\n");printf("\n Enter the number of elements you want to sort");scanf("%d",&items);printf("\n Enter the %d elements",items);for(i=0;i<items;i++)scanf("%d",&x[i]);sort(0,items-1);display();getch();}display(){printf("\n Elements sorted by quick sort are \n");for(i=0;i<items;++i)printf("%d\n",x[i]);return(0);}sort(int first,int last){int temp,pivot,i,j;if(first<last){pivot=x[first];i=first;j=last;while(i<j){while(x[i]<=pivot&&i<last)i++;while(x[j]>=pivot&&j>first)j--;

Page 58: Ds Lab Manual

if(i<j){temp=x[i];x[i]=x[j];x[j]=temp;}}temp=x[first];x[first]=x[j];x[j]=temp;sort(first,j-1);sort(j+1,last);}return(0);}

Page 59: Ds Lab Manual

OUTPUT :

....QUICK SORT.......

Enter the number of elements you want to sort 4

Enter 4 elements

4556178

Elements sorted by quick sort are

1455678

RESULT: Thus the given C program has been executed successfully and the output is verified.