Ex.

No: 1

IMPLEMENTATION OF SINGLY LINKED LIST

Program: /* IMPLEMENTATION OF SINGLY LINKED LIST */ #include<stdio.h> #include<conio.h> #define NULL 0 struct info { int data; struct info * next; }; struct info * head, *temp, *disp; void additem(); void delitem(); void display(); int size(); void search(); void main() { int choice; clrscr(); while(1) { printf("\n menu \n **** \n 1.add \n 2.delete \n 3.display \n 4.count \n 5.search \n 6.exit \n"); printf("Enter your choice"); scanf("%d",&choice); switch(choice) { case 1: additem(); break; case 2: delitem(); break; case 3: display(); break; case 4: printf("\n The size of the list is:%d",size()); 1

break; case 5: search(); break; case 6: exit(); break; default: printf("\n Invalid choice"); } } } void additem() { struct info*add; { add=(struct info*)malloc(sizeof(struct info)); printf("\n Enter the data:"); scanf("%d",&add->data); printf("\n%d is added in the list.",add->data); if(head==NULL) { head=add; add->next=NULL; temp=add; } else { temp->next=add; add->next=NULL; temp=add; } } } void delitem() { struct info *curr,*prev; int tdata; if(head==NULL) { printf("\n No records to delete"); return; } printf("\n Enter data to delete"); scanf("%d",&tdata); prev=curr=head; 2

while((curr!=NULL)&&(curr->data!=tdata)) { prev=curr; curr=curr->next; } if(curr==NULL) { printf("\nData is not found"); return; } if(curr==head) head=head->next; else { prev->next=curr->next; if(curr->next==NULL) temp=prev; } free(curr); printf("\n%d is deleted",tdata); } void display() { if(head==NULL) { printf("\nData is not found"); return; } printf("\n The nodes present in the list is:"); for(disp=head;disp!=NULL;disp=disp->next) { printf("%d\t",disp->data); } } int size() { int count=0; if(head==NULL) { return count; } for(disp=head;disp!=NULL;disp=disp->next) count++; return count; } void search() 3

disp!=NULL&&found==0. } if(found==0) printf("\n%d is not present in the list".{ int titem.&titem). for(disp=head. else { printf("\n%d is present in the list". return.disp=disp->next) { if(disp->data==titem) found=1. to search").titem).titem). } } 4 . return.found=0. } printf("\n Enter the no. scanf("%d". if(head==NULL) { printf("\nNo data in the list").

delete 3.delete 3.count 5.search 6. menu **** 1.search 6.add 2.delete 3.exit Enter your choice1 Enter the data:12 12 is added in the list.exit Enter your choice1 Enter the data:34 34 is added in the list.search 6.add 2.delete 3.display 4.add 2.add 2.exit Enter your choice1 Enter the data:23 23 is added in the list.count 5.Output: menu **** 1.display 4. menu **** 1.display 4.count 5. menu **** 1.display 5 .

exit Enter your choice4 The size of the list is:2 menu **** 1.delete 3.add 2.display 4.add 2.delete 3.exit Enter your choice5 Enter the no.delete 3.search 6.search 6. to search12 12 is present in the list Enter your choice6 23 6 .exit Enter your choice3 The nodes present in the list is:12 menu **** 1.search 6.count 5.count 5.display 4.search 6.display 4.add 2.exit Enter your choice2 Enter data to delete34 34 is deleted menu **** 1.count 5.4.count 5.

Ex.No: 2

IMPLEMENTATION OF DOUBLY LINKED LIST

Program: /* IMPLEMENTATION OF DOUBLY LINKED LIST */ #include<stdio.h> #include<conio.h> #define NULL 0 struct info { int data; struct info*next; struct info*prev; }; struct info*head,*temp,*disp; void additem(); void display(); void delitem(); int size(); void search(); void main() { int choice; clrscr(); while(1) { printf("\nMENU\n******\n1.ADD\n2.DISPLAY\n3.DELETE\n4.COUNT\n5.SEARCH\n6.EXI T\nEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: additem(); break; case 2: display() ; break; case 3: delitem(); break; 7

case 4: printf("\n the size of the list %d",size()); break; case 5: search(); break; case 6: exit(); break; defult: printf("\ninvalid choice."); } } } void additem() { struct info*add; { add=(struct info*)malloc(sizeof(struct info)); printf("enter the data:"); scanf("%d",&add->data); printf("\nthe element %d is added",add->data); if(head==NULL) { head=add; add->next=NULL; add->prev=NULL; temp=add; } else { temp->next=add; add->prev=temp; add->next=NULL; temp=add; } } } void delitem() { int x; struct info*p; if(head==NULL) { printf("\nNo items in the list."); return; 8

} printf("\nEnter the value to remove:"); scanf("%d",&x); p=(struct info*)malloc(sizeof(struct info)); p=head->next; if(head->data==x) { head=head->next; printf("\nThe no is removed."); return; } while(p) { if(p->data==x) { p->prev->next=p->next; if(p->next!=NULL) p->next->prev=p->prev; else temp=p->prev; return; } else { p=p->next; } } printf("\nThe no is not available in the list."); } void display() { if(head==NULL) { printf("\nNo records to display."); return; } printf("\nFrom forward direction\n"); for(disp=head;disp!=NULL;disp=disp->next) { printf("\nData %d",disp->data); } printf("\nFrom backward direction\n"); for(disp=temp;disp!=NULL;disp=disp->prev) { printf("\nData %d",disp->data); } 9

for(disp=head. } if(found==1) printf("\nSearch element is present in the list. } 10 . else printf("\nSearch element is not present in the list.disp=disp->next) { if(disp->data==tdata) found=1.disp!=NULL&&found==0.&tdata). } void search() { int tdata.disp!=NULL. } printf("\nEnter search element:").} int size() { int count=0.")."). return count.").disp=disp->next) count++. return.found=0. scanf("%d". for(disp=head. if(head==NULL) { printf("\nNo items in the list. if(head==NULL) return count.

COUNT 5.SEARCH 6.ADD 2.COUNT 5.DISPLAY 3.EXIT Enter your choice:1 enter the data:54 the element 54 is added MENU ****** 1.DELETE 4.EXIT Enter your choice:1 enter the data:12 the element 12 is added MENU ****** 1.Output: MENU ****** 1.SEARCH 6.COUNT 5.ADD 2.ADD 2.EXIT Enter your choice:1 enter the data:34 the element 34 is added MENU ****** 1.DISPLAY 3.DELETE 4.DELETE 4.DISPLAY 3.DELETE 4.COUNT 5.SEARCH 6.SEARCH 6.ADD 2.EXIT 11 .DISPLAY 3.

ADD 2.SEARCH 6.COUNT 5.DELETE 4.DELETE 4.EXIT Enter your choice:3 Enter the value to remove:34 MENU ****** 1.DISPLAY 3.DELETE 4.Enter your choice:2 From forward direction Data 12 Data 34 Data 54 From backward direction Data 54 Data 34 Data 12 MENU ****** 1.ADD 2.EXIT Enter your choice:4 the size of the list 2 MENU ****** 1.EXIT Enter your choice:5 Enter search element:54 12 .DISPLAY 3.DISPLAY 3.SEARCH 6.SEARCH 6.COUNT 5.COUNT 5.ADD 2.

DISPLAY 3.DISPLAY 3.DELETE 4.ADD 2.COUNT 5.EXIT Enter your choice:5 Enter search element:78 Search element is not present in the list.COUNT 5.EXIT Enter your choice:6 13 . MENU ****** 1.SEARCH 6.SEARCH 6.Search element is present in the list. MENU ****** 1.ADD 2.DELETE 4.

*poly=NULL. int pow. } void show(struct link*node) { while(node->next!=NULL) { printf("%dX^%d".No: 3 POLYNOMIAL ADDITION USING LINKED LIST Program: /* POLYNOMIAL ADDITION USING LINKED LIST */ #include<stdio. }. } while(ch=='y'||ch=='Y').h> #include<conio. scanf("%d". node=node->next.node->pow). node->next=(struct link*)malloc(sizeof(struct link)). void create(struct link *node) { char ch. node->next=NULL.h> #include<malloc. ch=getch(). printf("\n enter power:"). } } 14 . if(node->next!=NULL) printf("+").node->coeff.*poly2=NULL.&node->coeff). struct link *poly1=NULL. struct link *next.h> struct link { int coeff. printf("\n continue(Y/n):"). node=node->next.Ex. scanf("%d".&node->pow). do { printf("\n enter coeff:").

poly1=poly1->next. } poly->next=(struct link*)malloc(sizeof(struct link)). } else { poly->pow=poly1->pow.void polyadd(struct link *poly1. poly2=poly2->next. } else if(poly1->pow<poly2->pow) { poly->pow=poly2->pow. poly=poly->next.struct link *poly) { while(poly1->next&&poly2->next) { if(poly1->pow>poly2->pow) { poly->pow=poly1->pow. poly1=poly1->next. poly->coeff=poly1->coeff. poly2=poly2->next. } poly->next=(struct link*)malloc(sizeof(struct link)). poly->next=NULL.struct link *poly2. poly->next=NULL. } if(poly2->next) { poly->pow=poly2->pow. poly->coeff=poly1->coeff. poly1=poly1->next. } while(poly1->next||poly2->next) { if(poly1->next) { poly->pow=poly1->pow. poly->coeff=poly2->coeff. poly->coeff=poly2->coeff. poly2=poly2->next. } } 15 . poly=poly->next. poly->coeff=poly1->coeff+poly2->coeff.

show(poly1).void main() { char ch. } 16 . create(poly2). printf("\n added polynomial:").poly). } while(ch=='y'||ch=='Y'). do { poly1=(struct link*)malloc(sizeof(struct link)). poly2=(struct link*)malloc(sizeof(struct link)). show(poly). printf("\n second number:"). printf("\n Add two more numbers:"). create(poly1). printf("\n Enter first number:"). polyadd(poly1. printf("\n Enter second number:"). printf("\n first number:"). show(poly2).poly2. ch=getch().

Output: Enter first number: enter coeff:2 enter power:2 continue(Y/n): enter coeff: 3 enter power:1 continue(Y/n): Enter second number: enter coeff:6 enter power:2 continue(Y/n): enter coeff:4 enter power:1 continue(Y/n): first number:2X^2+3X^1 second number:6X^2+4X^1 added polynomial:8X^2+7X^1 Add two more numbers: 17 .

Ex. 18 .h> #include<string. return. printf("Enter the infix expression").h> #include<conio. char pop(). if(top==-1) { printf("stack is empty"). in_to_post(infix). int top=-1. } else { top=top+1. getch().h> char stack[50]. getch(). } } char pop() { char item. void in_to_post(char infix[]). void push(char). clrscr(). void main() { char infix[25]. gets(infix). stack[top]=symb.No: 4 INFIX TO POSTFIX EXPRESSION Program: /* INFIX TO POSTFIX EXPRESSION */ #include<stdio. } void push(char symb) { if(top>=49) { printf("stack overflow").

push('#'). } return(item). switch(symbol) { case'(':push(symbol). } else if(ch==42) { return(4). break. } void in_to_post(char infix[]) { int length.temp.getch(). } else { item=stack[top].pos=0. } else return(2). char postfix[40]. case')':temp=pop(). } else if(ch==43) { return(3). while(index<length) { symbol=infix[index]. length=strlen(infix). top--. static int index=0. char symbol. } int preced(char ch) { if(ch==47) { return(5). while(temp!='(') { 19 . return(0).

pos++. default:postfix[pos++]=symbol. break. } postfix[pos++]='\0'. } push(symbol). } Output: Enter the infix expression a+b*c+(d*e+f)*g The postfix expression is abc*+de*f+g*+ 20 . break.postfix[pos]=temp. temp=pop(). postfix[pos++]=temp. } break. pos++. postfix[pos]=temp. return. } while(top>0) { temp=pop(). case'+': case'-': case'*': case'/': case'^': while(preced(stack[top])>=preced(symbol)) { temp=pop(). puts(postfix). printf("The postfix expression is"). } index++.

h> #include<stdlib. 21 . } } void delete() { int item. else { if(front==-1) front=rear=0. int qfull() { if(front==(rear+1)%size) return 1. else return 0. int rear=0. else return 0. else rear=(rear+1)%size. } void add(int item) { if(qfull()) printf("\n the circular queue is full").h> #include<conio.h> #define size 5 int queue[size]. } int qempty() { if(front==-1) return 1. int front=-1. No: 5 IMPLEMENTATION OF DOUBLE ENDED QUEUE Program: /* IMPLEMENTATION OF DOUBLE ENDED QUEUE */ #include<stdio.Ex. queue[rear]=item.

i=(i+1)%size. } i=front. printf("\n the deleted item is %d".delete\n3.insert\n2.queue[i]).display"). printf("\n 1. if(qempty()) { printf("\n the queue is empty").item). printf("\n enter your choice").item. return. } } void display() { int i.&item). do { printf("\n main menu"). while(i!=rear) { printf("%d". char ans. 22 . } void main() { int choice. } else front=(front+1)%size. switch(choice) { case 1: printf("\n enter the element"). } printf("%d\t". scanf("%d".if(qempty()) printf("\n queue is empty").queue[i]). else { item=queue[front]. clrscr(). if(front==rear) { front=rear=-1. scanf("%d".&choice).

getch(). } printf("\n do u want to continue(y/n)"). } while(ans=='Y'||ans=='y'). case 3: display(). break. case 2: delete().add(item). } 23 . break. ans=getch(). default: exit(0). break.

insert 2.delete 3.display enter your choice 1 enter the element 45 do u want to continue(y/n) main menu 1.Output: main menu 1.display enter your choice 2 the deleted item is 12 do u want to continue(y/n) enter your choice 3 45 67 do u want to continue(y/n) 24 .insert 2.delete 3.delete 3.insert 2.display enter your choice 1 enter the element 67 do u want to continue(y/n) main menu 1.insert 2.display enter your choice 1 enter the element 12 do u want to continue(y/n) main menu 1.delete 3.

printf("\n 4.int row.h> #define NULL0 typedef struct btreenode { struct btreenode*leftchild. btree*root. buildtree(n).Enter your choice:").int num). scanf("%d".h> #include<conio. void traverse(). printf("Enter the element to insert:").Ex.n. void disp(btree*sr. clrscr(). void postorder(btree*sr). do { printf("\n 1. void main() { int opt. void display().Quit").&n).h> #include<malloc. void insert(btree**sr. void buildtree(int num). scanf("%d".Traverse"). printf("\n 3. void preorder(btree*sr).Insert"). void inorder(btree*sr). 25 . struct btreenode*rightchild.int col). printf("\n 2. } btree.&opt). switch(opt) { case 1: clrscr(). int data.No: 6 IMPLEMENTATION OF EXPRESSION TREE Program: /* IMPLEMENTATION OF EXPRESSION TREE */ #include<stdio.

} } void traverse() { printf("\n Inorder traversal:"). } void inorder(btree*sr) 26 . break. preorder(root). inorder(root). } else { if(num<(*sr)->data) insert(&(*sr)->leftchild. } void insert(btree**sr. printf("\n Preorder traversal:"). (*sr)->leftchild=NULL.num). case 2: clrscr(). (*sr)->data=num. postorder(root). traverse(). else if(num>=(*sr)->data) insert(&(*sr)->rightchild. } } while(opt!=3). (*sr)->rightchild=NULL. default: break. printf("\n Postorder traversal:").num). } void buildtree(int num) { insert(&root.num).int num) { if((*sr)==NULL) { (*sr)=(btree*)malloc(sizeof(btree)). display(). getch(). break.printf("\n Tree after insertion").

col).sr->data). printf("%d\t".int row.row+3.sr->data). printf("%d\t". } } 27 . preorder(sr->leftchild).{ if(sr!=NULL) { inorder(sr->leftchild). } void disp(btree*sr.10).row-3. postorder(sr->rightchild).col+2). } } void preorder(btree*sr) { if(sr!=NULL) { printf("%d\t".sr->data).40. inorder(sr->rightchild). } } void display() { disp(root. printf("%d".col+2). } } void postorder(btree*sr) { if(sr!=NULL) { postorder(sr->leftchild).sr->data). preorder(sr->rightchild). } if(sr->rightchild) { disp(sr->rightchild.int col) { if(sr->data!=NULL) { gotoxy(row. } if(sr->leftchild) { disp(sr->leftchild.

Enter your choice:1 Enter the element to insert:11 Tree after insertion 12 11 1.Quit 4.Insert 2.Quit 4.Insert 2.Traverse 3.Traverse 3.Insert 2.Enter your choice:1 Enter the element to insert:12 Tree after insertion 12 1.Traverse 3.Quit 4.Insert 2.Traverse 3.Insert 2.Traverse 3.Enter your choice:1 Enter the element to insert:23 Tree after insertion 12 23 1.Output: 1.Quit 4.Quit 23 23 23 12 28 .Enter your choice:2 Inorder traversal:11 12 Preorder traversal:12 11 Postorder traversal:11 23 1.

int).*temp. scanf("%d". printf("\n enter your choice:").&x). scanf("%d". struct tree *find(struct tree*. int choice.int).No: 7 IMPLEMENTATION OF BINARY SEARCH TREE Program: /* IMPLEMENTATION OF BINARY SEARCH TREE */ #include<stdio. struct tree *insert(struct tree*. case 2: printf("\n enter the data to be insert:").exit"). void main() { struct tree *create(struct tree*.x). struct tree *findmax(struct tree*).Ex.h> #include<conio.&choice).h> #include<process. struct tree *left.h> struct tree { int data.&x). struct tree *findmin(struct tree*).create\n2.delete\n4. 29 . } *t. break.h> #include<alloc. scanf("%d". clrscr().find\n5. do { printf("\n1. t=create(t.int).findmax\n7.*right.insert\n3. struct tree *del(struct tree*. switch(choice) { case 1: printf("\n enter the element:").findmin\n6.int). int x.

else 30 .&x).temp). if(temp->data==x) printf("\n element %d is at %d". getch().x)). case 5: temp=findmin(t). } } while(choice<=7). case 7: getch().t=insert(t. case 6: temp=findmax(t).x)).temp->data).x).x). scanf("%d". break. scanf("%d". t->left=t->right=NULL. else printf("\n element not found").x).temp->data). printf("\n max element=%d".x. exit(0). temp=find(t. } struct tree *create(struct tree *t.int x) { if(t==NULL) if(x<t->data) return(find (t->left. break. return t. if(x<t->data) return(find(t->right. break. break. printf("\n min element=%d".&x). case 3: printf("\n enter the data to be deleted:"). case 4: printf("\n enter the data to be search:"). t=del(t. } struct tree *find(struct tree *t. t->data=y.int y) { t=(struct tree*)malloc(sizeof(struct tree)).

t->left=t->right=NULL. } return t. } return t. } } 31 .x). } struct tree *insert(struct tree *t. } else if(x>t->data) { t->right=insert(t->right. } else { if(x<t->data) { t->left=insert(t->left. else return(findmin(t->left)). } else if(x==t->data) { printf("\n element already present"). t->data=x. else if(t->left==NULL) return t. return t.return t.x). } struct tree *findmax(struct tree *t) { if(t!=NULL) { while(t->right!=NULL) t=t->right.int x) { if(t==NULL) { t=(struct tree*)malloc(sizeof(struct tree)). } struct tree*findmin(struct tree *t) { if(t==NULL) return NULL.

if(t->left==NULL) t=t->right. else if(x<t->data) t->left=del(t->left. t->data=temp->data. else if(t->right==NULL) t=t->left.t->data).x). free(temp). } return t.struct tree *del(struct tree *t.x). else if(t->left&&t->right) { temp=findmin(t->right).int x) { if(t==NULL) printf("\n element not found"). } 32 . t->right=del(t->right. else if(x>t->data) t->right=del(t->right. } else { temp=t.

insert 2. menu 1.find 3. menu 1.insert 2.exit Enter your choice:1 Enter the item to be inserted:12 the item is inserted.insert 2.find 3.display 4.exit Enter your choice:2 Enter the element to be found:34 The item is found. 33 .find 3. menu 1.Output: menu 1.exit Enter your choice:2 Enter the element to be found:556 The item is not found.find 3.display 4.exit Enter your choice:1 Enter the item to be inserted:23 the item is inserted.find 3.insert 2.exit Enter your choice:1 Enter the item to be inserted:34 the item is inserted.display 4. menu 1.display 4.display 4.insert 2.

inorder 2.postordertraversal 3.find 3.preordertraversal 2 The tree is 12 34 23 menu 1.display 4.preordertraversal 1 The tree is: 12 23 34 menu 1.find 3.exit Enter your choice:3 Enter the traversal: 1.insert 2.inorder 2.display 4.exit Enter your choice:3 Enter the traversal: 1.exit Enter your choice:3 Enter the traversal: 1.insert 2.find 3.postordertraversal 3.display 4.insert 2.menu 1.preordertraversal 34 .postordertraversal 3.inorder 2.

insert 2.find 3.display 4.3 The tree is 23 12 34 menu 1.exit Enter your choice:4 35 .

Ex. position findmax(avltree t).avltree t). avltree makeempty (avltree t). else return 0. avltree right. } avltree makeempty(avltree t) { 36 */ . avltree left. typedef struct treenode ptrnode. int height(position p) { if(p!=NULL) return p->height. int height. }. avltree insert(elementtype x. position findmin(avltree t).h> struct treenode.int h2) { if(h1>h2) return h1.avltree t).No:8 IMPLEMENTATION OF AVL TREES Program: /* IMPLEMENTATION OF AVL TREE #include<stdio. else return h2. elementtype retrieve(position p). avltree delet(elementtype x. typedef int elementtype.avltree t). typedef struct treenode *position. struct treenode { elementtype element. } int max(int h1. typedef struct treenode *avltree . position find(elementtype x.

return k1. k2=k1->right. } avltree insert(elementtype x. k1=k2->left.avltree t) { if(t==NULL) { t=(ptrnode*)malloc(sizeof(struct treenode)). else 37 . if(t==NULL) printf("out of space"). } position singlerightleftrotation(position k1) { position k2.height(k1->right))+1. k2->height=max(height(k2->left).if(t!=NULL) { makeempty(t->left).height(k2->right))+1. return singlerightrotation(k3). k1->right=k2. k1->height=max(height(k1->left). free(t). makeempty(t->right). k2->left=k2->right. k2->height=max(height(k2->left). k2->left=k1. } position doublerightrotation(position k3) { k3->left=singlerightrotation(k3->left). k1->height=max(height(k1->left). } position singlerightrotation(position k2) { position k1. k1->right=k2->left.height(k2->right))+1. return singlerightleftrotation(k3).height(k1->right))+1. } position doubleleftrightrotation(position k3) { k3->right=singlerightleftrotation(k3->right). } return NULL. return k2.

} else if(x>t->element) { return find(x.t->right).t->right).height(t->right))+1. if(height(t->left)-height(t->right)==2) if(x<t->left->element) t=singlerightrotation(t). t->right=NULL. return t. } position find(elementtype x.t->left). if(height(t->right)-height(t->left)==2) if(x>t->right->element) t=singlerightleftrotation(t). t->left=NULL. } void inorder(avltree t) { 38 . else t=doublerightrotation(t). else if(x<t->element) { return find(x.{ t->element=x. else t= doublerightrotation(t). } } else if(x<t->element) { t->left=insert(x. } else if(x>t->element) { t->right=insert(x. t->height=0. avltree t) { if(t==NULL) return NULL. } t->height=max(height(t->left).t->right). } else return t.

t->element).display \n4.flag. switch(opt) { 39 . else { printf("\n%d". } } int main() { int opt. printf("\n%d".&opt). printf("\n Enter your choice:"). postorder(t->right). printf("\n%d". printf("\nmenu \n1. else { postorder(t->left).insert \n2.find \n3.exit \n").t->element). } } void postorder(avltree t) { if(t==NULL) return. t=NULL. preorder(t->right). inorder(t->right). preorder(t->left). do { flag=0. avltree t.item. } } void preorder(avltree t) { if(t==NULL) return.if(t==NULL) return. scanf("%d".choice. position p. else { inorder(t->left). clrscr().t->element).

t=insert(item. scanf("%d".t).").case 1: printf("Enter the item to be inserted:"). case 2: if(t) { printf("The tree is"). case 3: while(flag==0) { printf("\nEnter the traversal:"). scanf("%d". break. case 3: 40 .preordertraversal\n"). printf("the item is inserted.t).inorder\n2.&item). if(p) printf("The item is found.postordertraversal\n3. p=find(item. break."). else printf("The item is not found.&choice)."). printf("\n1. scanf("%d". } else { printf("the tree is empty.").").&item). case 2: printf("Enter the element to be found:"). inorder(t). } else { printf("The tree is empty. switch(choice) { case 1: if(t) { printf("The tree is:"). postorder(t). } break. } break.

case 4: exit(1). continue. } break. default:flag=1. printf("\n invalid"). } else { printf("The tree is empty. } 41 . } break. preorder(t).").if(t) { printf("The tree is"). } } } while(1).

exit Enter your choice:2 Enter the element to be found:34 The item is found.display 4.display 4.exit Enter your choice:2 Enter the element to be found:556 The item is not found.exit Enter your choice:1 Enter the item to be inserted:23 the item is inserted.insert 2.insert 2.exit Enter your choice:1 Enter the item to be inserted:34 the item is inserted.find 3. menu 1.insert 2. menu 1.insert 2.exit Enter your choice:1 Enter the item to be inserted:12 the item is inserted.display 4.find 3.find 3.find 3.insert 2.display 4. menu 1.display 4.Output: menu 1. menu 1. 42 .find 3.

preordertraversal 43 .inorder 2.exit Enter your choice:3 Enter the traversal: 1.find 3.insert 2.preordertraversal 1 The tree is: 12 23 34 menu 1.postordertraversal 3.insert 2.preordertraversal 2 The tree is 12 34 23 menu 1.exit Enter your choice:3 Enter the traversal: 1.menu 1.postordertraversal 3.find 3.insert 2.find 3.postordertraversal 3.display 4.display 4.display 4.inorder 2.inorder 2.exit Enter your choice:3 Enter the traversal: 1.

find 3.exit Enter your choice:4 44 .insert 2.display 4.3 The tree is 23 12 34 menu 1.

struct node { int info. printf("\nEnter your choice:"). case 3: del().Ex. typedef struct node N. clrscr().DISPLAY\n5.No: 9 IMPLEMENTATION OF HASHING ALGORITHM Program: /* IMPLEMENTATION OF HASHING ALGORITHM */ #include<stdio. break. int *next. }*start[10]. do { p1: printf("\n\tMENU\n1.EXIT").key.INSERT\n2.SEARCH\n3.h> void insert(). void display().&ch). 45 . break. main() { int ch. switch(ch) { case 1: insert().g. case 4: display(). break.DELETE\n4. void search(). void del(). scanf("%d". case 2: search(). int i.

key++) { if(start[key]==NULL) printf("\n. } loc->next=p.. p=(N*)malloc(sizeof(N)). for(key=0. if(start[key]==NULL) start[key]=p. loc=(N*)malloc(sizeof(N)). } void insert() { N *p. p->next=NULL. default: printf("\nInvalid choice entry!!\n").. case 5: break. } } void display() { N *p. goto p1. else { while(loc->next!=NULL) { loc=loc->next.. while(p!=NULL) { 46 . p=(N*)malloc(sizeof(N)). } } while(ch!=5). int item. else { p=start[key]...break. scanf("%d". printf("\nEnter the item to be inserted:"). p->info=item. key=item%10.!").&item).*loc.key<10. loc=start[key].

} } if(g==1) { while(start[key]!=NULL&&f!=1) { if(start[key]->info==item) { f=1.&item).g=0. for(i=0. while(p->next!=NULL) { p=p->next. scanf("%d". key=(item%10).f=0.i++) { if(start[i]!=NULL) { g=1. break. } } 47 .p->info). p=p->next.p->info). printf("\nEnter the element to be searched:"). } else { p=start[key]. } } printf("\n"). printf("\t%d". break. f=1.start[key]->info). } } void search() { N *p. int item. break. p=(N*)malloc(sizeof(N)). if(p->info==item) { printf("%d".i<10.printf("%d\t".

} } if(f==1) printf("\n\tThe item searched was found!").g=0. int item. key=(item%10). printf("\nThe item searched was not found in the hash table"). f=1. } } if(g==1) { if(start[key]!=NULL) { if(start[key]->info==item) { p=start[key].i++) { if(start[i]!=NULL) { g=1. for(i=0. } } else printf("\nHashtable is empty!!\n"). t=(N*)malloc(sizeof(N)). } p=start[key]. break.*t.i<10. scanf("%d". p=(N*)malloc(sizeof(N)). start[key]=t. } void del() { N *p. else { key=-1.break.&item). t=p->next. printf("\nEnter the element to be deleted"). free(p). if(start[key]==NULL) goto q.f=0. 48 .

f=1. printf("\nThe item searched was not found in the hash table"). } } } q: if(f==1) printf("\nThe item searched was deleted"). } } else printf("\nHash table is empty!!\n"). free(p). } 49 .while(p->next!=NULL) { t=p. p=t. p=p->next. if(p->info==item) { t->next=p->next. else { key=-1.

EXIT Enter your choice:1 Enter the item to be inserted:23 MENU 1.SEARCH 3.EXIT Enter your choice:2 Enter the element to be searched:23 23 The item searched was found! MENU 1.Output: MENU 1.SEARCH 3.DELETE 4.INSERT 2.EXIT Enter your choice:3 Enter the element to be deleted23 The item searched was deleted MENU 50 .SEARCH 3.INSERT 2.DELETE 4.DISPLAY 5.DISPLAY 5.INSERT 2.EXIT Enter your choice:1 Enter the item to be inserted:12 MENU 1.SEARCH 3.INSERT 2.DISPLAY 5.DISPLAY 5.DELETE 4.DELETE 4.

SEARCH 3.1.INSERT 2.DELETE 4.INSERT 2.SEARCH 3.DELETE 4.EXIT Enter your choice:4 12 MENU 1.DISPLAY 5.EXIT Enter your choice:5 51 .DISPLAY 5.

No: 10 IMPLEMENTATION OF DIJKSTRAS ALGORITHM Program: /* IMPLEMENTATION OF DIJKSTRAS ALGORITHM */ #include<stdio. scanf("%d".j++) { b[i][j]=a[i][j]. printf("\n Shortest Path Using Dijstra Algorithm ").i.k.i<=n.n.i++) { for(j=1. printf("\n Enter the no of needs: ").j<=n.&a[i][j]).j++) { if(a[i][j]==0) { a[i][j]=INF. } } } printf("\n The weight of the graph is :\n").h> #define INF 99 void main() { int a[20][20].i<=n.i++) { for(j=1.a[i][j]).j++) { printf("Enter the weight of the node %d to %d :". printf("\t%d".Ex.i++) { for(j=1. printf("\n ***************************************\n").i<=n.i. for(i=1. } } for(i=1.j<=n.h> #include<conio. for(i=1. scanf("%d". 52 .&n).j.j<=n.j).b[20][20]. clrscr(). printf("\n Enter the adjnodes weight").

k<=n.j<=n. for(k=1. } printf("\n").i++) { for(j=1. } Output: Shortest Path Using Dijstra Algorithm *************************************** Enter the no of nodeds: 2 Enter the adjnodes weightEnter the weight of the node 1 to 1 :34 Enter the weight of the node 1 to 2 :45 Enter the weight of the node 2 to 1 :67 Enter the weight of the node 2 to 2 :78 The weight of the graph is : 34 45 67 78 The shortest path is: 34 45 67 78 53 .i<=n.i<=n.} printf("\n").j++) { printf("\t%d".b[i][j]).j<=n. for(i=1. } printf("\n The shortest path is: \n"). } getch().k++) for(i=1.j++) b[i][j]=(b[i][j]<(b[i][k]+b[k][j]))?b[i][j]:(b[i][k]+b[k][j]).i++) for(j=1.

scanf("%d%d%d". printf("\n Enter the number of vertices:"). for(i=1.mincost. void creategraph() { int i.b and weight w:").i.a.&w).visited[20]. current=1. while(totalvisited!=v) { 54 . } } void prim() { int current.w. g[a][b]=g[b][a]=w. for(i=1.e.Ex. totalvisited=1. d[i]=32767.&v). scanf("%d".h> int g[20][20].i++) { p[i]=visited[i]=0. scanf("%d".&b.&e).j.No: 11 IMPLEMENTATION OF PRIMS ALGORITHM Program: /* IMPLEMENTATION OF PRIMS ALGORITHM */ #include<stdio.p[20].i<=v.i++) { printf("Enter edge a.totalvisited. } for(i=1.i++) for(j=1.i<=e.min. int v.j++) g[i][j]=0.d[20].j<=v.i<=v.&a. d[current]=0. visited[current]=1.b.h> #include<conio. printf("\n Enter the num of edges:").

} void main() { int i.i++) printf("vertex %d is connected to %d \n".i<=v. for(i=2. printf("minimum span tree is \n").for(i=1.i++) mincost+=d[i].i++) { if(g[current][i]!=0) if(visited[i]==0) if(d[i]>g[current][i]) { d[i]=g[current][i]. } } min=32767. totalvisited++. clrscr(). } 55 . p[i]=current. for(i=1. } } visited[current]=1.i. creategraph(). for(i=1.i<=v.mincost). prim(). } mincost=0. current=i. printf("minimum cost=%d\n".p[i]).i<=v.i++) { if(visited[i]==0) if(d[i]<min) { min=d[i].i<=v. getch().

b and weight w:35 456 67 minimum cost=-2 minimum span tree is vertex 2 is connected to 0 vertex 3 is connected to 0 56 .Output: Enter the number of vertices:3 Enter the num of edges:3 Enter edge a.b and weight w:43 54 665 Enter edge a.b and weight w:23 32 43 Enter edge a.

time_t t. getch().No: 12 RANDOMIZED ALGORITHM Program: /* Randomized algorithm */ #include<stdio.int n) { int i.30.40. } } 57 .h> #include<time.i).20.30}. printf("\n\t Program finds the repeated element \n").j. while(count<=100) { i=rand()%(n+1).int n).11.count.n).h> #include<conio. return 0.13. } void repetition(int a[]. if((i!=j)&&(a[i]==a[j])) printf("\n The repeated element is present at index %d". clrscr(). count++.h> #include<stdlib.12. j=rand()%(n+1). void repetition(int a[].30. int n=10.h> int main(void) { int a[10]={10.10. count=1. repetition(a.Ex. srand((unsigned)time(&t)).

Output: The repeated element is present at index 7 The repeated element is present at index 2 The repeated element is present at index 2 The repeated element is present at index 9 The repeated element is present at index 7 The repeated element is present at index 8 The repeated element is present at index 7 The repeated element is present at index 2 The repeated element is present at index 9 The repeated element is present at index 8 The repeated element is present at index 7 The repeated element is present at index 0 58 .

n1=pop(&s).top=-1.float).i++) { ch=postfix[i]. printf("Enter the postfix expression:\n\n").Ex. }s. float value.i<=j-1.h> #include<conio.ch-'0').final_result. float pop(struct stack*). 59 .h> #include<string. printf("\n Evaluation of postfix expression \n\n").h> #include<math. float items[stacksize]. for(i=0. clrscr(). } else { n2=pop(&s). char ch.result.&value).n1. scanf("%f". gets(postfix).n2. s.No: 13 IMPLEMENTATION OF POSTFIX EVALUATION Program: /* postfix evaluation */ #include<stdio.j. if((ch>='0')&&(ch<='9')) push(&s. push(&s. j=strlen(postfix). int i. else if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))) { printf("\n Enter the value of %c :".postfix[25].ch). struct stack s. void main() { void push(struct stack*.value).h> #define stacksize 20 struct stack { int top.

getch(). break. return. getch(). getch(). break. break. } else sptr->items[++(sptr->top)]=ele.float ele) { if(sptr->top==stacksize-1) { printf("\n stack overflow. } push(&s.final_result). printf("\n Value of the postfix expression is %f". return. } void push(struct stack *sptr.n2). exit(). break. break. case '/': result=n1/n2. } float pop(struct stack*sptr) { if(empty(sptr)) { 60 . case '*': result=n1*n2. case '$': result=pow(n1. case '-': result=n1-n2. } } final_result=pop(&s).result). exit().switch(ch) { case '+': result=n1+n2.Elementcannot be pushed"). default: printf("\n\n Invalid postfix expression").

} Output: Evaluation of postfix expression Enter the postfix expression: ab*cde/-+ Enter the value of a :4 Enter the value of b :5 Enter the value of c :5 Enter the value of d :8 Enter the value of e :2 Value of the postfix expression is 21. return(0). getch(). else return(0). } else return(sptr->items[sptr->top--]). exit(1). } int empty(struct stack*sptr) { if(sptr->top==-1) return(1).000000 61 .printf("\n Invalid postfix experssion ").

h> #define stacksize 25 void main() { int top=-1.h> #include<conio.option. do { printf("\n the stsck operations are:"). else { popitem=stack[top]. printf("\n 1. case 2: if(top==-1) printf("\n stack is empty"). int pushitem. 62 . printf("\n item successfully pushed"). } else { printf("\n enter the item to push"). switch(option) { case 1: if(top==stacksize-1) { printf("\n STACK IS FULL"). PUSH \n 2. stack[top]=pushitem.stack[stacksize]. POP \n 3. DISPLAY"). } break. int i.&pushitem). printf("\n 4. ISFULL \n 5. clrscr(). ISEMPTY \n 6. printf("\n ENTER YOUR OPTION"). EXIT").&option). scanf("%d".Ex. scanf("%d".No: 14 IMPLEMENTATION OF STACK Program: /* Implementation of Stack*/ #include<stdio.popitem. top++.

i++) printf("\t %d". else printf("\n stack is not full"). case 3: if(top==-1) printf("\n stack is empty"). } break. case 4: if(top==stacksize-1) printf("\n stack is full").stack[i]). else { printf("\n the items are "). for(i=0. break. case 5: if(top==-1) printf("\n stack is empty").popitem).top--. } } while(option!=6). } break.i<=top. break. default: printf("\n enter a valid option"). printf("\n poped item is %d". break. else printf("\n stack is not empty"). } 63 .

DISPLAY 4. ISEMPTY 6. ISFULL 5. EXIT ENTER YOUR OPTION1 enter the item to push34 item successfully pushed enter the item to push23 item successfully pushed 64 . ISFULL 5.Output: the stsck operations are: 1. POP 3. EXIT ENTER YOUR OPTION1 the stsck operations are: 1. ISEMPTY 6. POP 3. PUSH 2. EXIT ENTER YOUR OPTION1 enter the item to push23 item successfully pushed the stsck operations are: 1. DISPLAY 4. PUSH 2. POP 3. ISEMPTY 6. DISPLAY 4. ISFULL 5. PUSH 2.

*p.*deleteptr. printf("\n 2.QUIT "). break.No: 15 IMPLEMENTATION OF QUEUE Program: /* Implementation of queue */ #include<stdlib.h> #include<conio. printf("\nEnter your choice").*temp. } } 65 .ADD ELEMENT "). clrscr().&choice). DISPLAY ").Ex. printf("\n 3. scanf("%d".DELETE ELEMENT "). while(1) { printf("\n 1.h> struct queue { int no. void main() { int choice.*rearptr. break. struct queue *next. switch(choice) { case 1: enqueue().h> #include<ctype. frontptr=rearptr=NULL. default: exit(0). struct queue *frontptr. case 3: display().h> #include<stdio. break. }. printf("\n 4. case 2: dequeue().

if(p==NULL) { printf(" Queue is empty"). return. free(deleteptr). temp->next=NULL. rearptr=rearptr->next.} enqueue() { temp=malloc(sizeof(struct queue)). } deleteptr=frontptr. } return. return."). if(frontptr==NULL) { frontptr=temp. } else { rearptr->next=temp. } display() { p=frontptr. frontptr=frontptr->next. while(p!=NULL) 66 . } printf("\n The elements present in the queue: \n"). scanf("%d". if(deleteptr!=NULL) { printf("\n The deleted element is %d". rearptr=temp. } return. } dequeue() { if(frontptr==NULL) { printf("\n queue is empty").deleteptr->no). temp=frontptr. printf("\n enter no.&temp->no).

DISPLAY 4.67 1. } return.QUIT Enter your choice1 enter no.DELETE ELEMENT 3.QUIT Enter your choice1 enter no.ADD ELEMENT 2. DISPLAY 4.QUIT Enter your choice3 The elements present in the queue: 67 1.QUIT 67 .DELETE ELEMENT 3. DISPLAY 4.5 1.ADD ELEMENT 2.DELETE ELEMENT 3.ADD ELEMENT 2. DISPLAY 4.ADD ELEMENT 2. } Output: 1. DISPLAY 4.DELETE ELEMENT 3.QUIT Enter your choice2 The deleted element is 5 1.ADD ELEMENT 2.{ printf("%d\t". p=p->next.p->no).DELETE ELEMENT 3.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.