Upcoming SlideShare
×

# Final ds record

1,119 views

Published on

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,119
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
59
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Final ds record

1. 1. Ex no –1(A) IMPLEMENTATION OF SINGLY LINKED LISTAIM: To write a c program to implement singly linked list.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Create a structure named node and declare variables to denote a node for singly linked list. Step4: In the create () method, check whether the head is NULL or not. If it is NULL, then get the number of nodes from user and get data for each node that are created. Step5: In the insert () method, check whether the head is NULL or not. If it is NOT NULL, then get the position from the user and then insert into list. Step6: In the delete () method, check whether the head is NULL or not. If it is NOT NULL, then get the position to be deleted from the user. Step7: Insert and delete according to the position. Step8: In display () method, print the elements of singly linked list. Step9: In main () method, use switch case statement to invoke the methods according to the user choice entered. Step10: End of the program. 1
2. 2. PROGRAM:#include<conio.h>#include<stdio.h>void creation(int);void display();void insertion(int,int);void deletion(int);struct list{ int num; struct list *next;}*head, *l;void main(){ int n1,ch,pos,val; clrscr(); do { printf("n1.creationn2.insertionn3.deletionn4.displayn5.exitn"); printf("nenter ur choice : "); scanf("%d",&ch); switch(ch) { case 1: creation(n1); break; case 2: printf("nnenter the postion in which to be inserted : "); scanf("%d",&pos); printf("nnenter the value : "); scanf("%d",&val); insertion(pos,val); break; case 3: printf("nnenter the position to be deleted : "); scanf("%d",&pos); deletion(pos); break; case 4: display(); getch(); break; case 5: exit(0); } }while(ch!=5); 2
3. 3. getch();}void creation(int n1){ int i,n; head=((struct list *)malloc(sizeof(struct list))); l=head; printf("nEnter the no of nodes:"); scanf("%d",&n1); for(i=0;i<n1;i++) { printf("enter the %d node : ",i+1); scanf("%d",&n); l->num=n; l->next=((struct list *)malloc(sizeof(struct list))); l=l->next; } l->next=0;}void display(){ l=head; printf("nthe nodes entered are : "); while(l->next>0) { printf("%dt",l->num); l=l->next; } printf("null");}void insertion(pos,val){ int i; struct list *x,*y; l=head; i=2; if(pos==1) { x=((struct list *)malloc(sizeof(struct list))); x->num=val; x->next=l; head=x; } else { while(l->next>0) 3
4. 4. { if(pos==i-1) { x=((struct list *)malloc(sizeof(struct list))); x->num=val; x->next=l; y->next=x; } y=l; l=l->next; i++; } }}void deletion(pos){ int i; struct list *y; l=head; i=1; if(pos==1) { head=l->next; } else { while(l->next>0) { if(pos==i) { l=l->next; y->next=l; break; } y=l; l=l->next; i++; } }} 4
5. 5. INPUT & OUTPUT:1.creation2.insertion3.deletion4.display5.exitenter ur choice : 1Enter the no of nodes:2enter the 1 node : 1enter the 2 node : 21.creation2.insertion3.deletion4.display5.exitenter ur choice : 2enter the postion in which to be inserted : 1enter the value : 61.creation2.insertion3.deletion4.display5.exitenter ur choice : 4the nodes entered are : 6 1 2 null1.creation2.insertion3.deletion4.display5.exitenter ur choice : 2enter the postion in which to be inserted : 2enter the value : 41.creation2.insertion3.deletion4.display5.exitenter ur choice :2enter the postion in which to be inserted : 2 5
6. 6. enter the value : 41.creation2.insertion3.deletion4.display5.exitenter ur choice : 3enter the position to be deleted : 21.creation2.insertion3.deletion4.display5.exitenter ur choice : 4the nodes entered are : 6 1 2 null1.creation2.insertion3.deletion4.display5.exitenter ur choice : 5<Exiting>RESULT: Thus the program for program for implementation of singly linked list has beencompleted successfully and output verified.Ex no –1(B) IMPLEMENTATION OF DOUBLY 6
7. 7. LINKED LISTAIM: To write a c program to implement doubly linked list.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Create a structure named node and declare variables to denote a node for doubly linked list. Step4: In the create () method, get the number of nodes and iterate the loop to get data for each node that are created. Step5: In the insert () method, get the position from the user. If it is valid position, get the data for a node and give forward link and backward link. Step6: In the delete () method, get the position to be deleted. If it is valid position, by rearranging forward link and backward link to delete the node from the list. Step7: Insert and delete according to the valid position. Step8: In display () method, print the elements of doubly linked list. Step9: In main () method, use switch case statement to invoke the methods according to the user choice entered. Step10: End of the program.PROGRAM:#include<stdio.h> 7
12. 12. AIM: To write a c program represent a polynomial as a linked list and write functionsfor polynomial addition.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the needed variables and initialize them. Step4: Get the value of co – efficient and exponent value of two polynomial expressions Step5: Compare the exponent of two polynomial expressions. Step6: If the exponent values are same add the polynomial co – efficient. Step7: If the exponent values are different add the biggest exponent’s co – efficient value in to the result polynomial. Step8: Print the result polynomial expression. Step9: End of the program.PROGRAM:#include<stdio.h>#include<conio.h> 12
13. 13. typedef struct poly{int coeff;int expo;}p;p p1[10],p2[10],p3[10];void main(){int t1,t2,t3;int read(p p1[10]);int add(p p1[10],p p2[10],int t1,int t2,p p3[10]);void print(p p2[10],int t2);void printo(p pp[10],int t2);t1=read(p1);print(p1,t1);t2=read(p2);print(p2,t2);t3=add(p1,p2,t1,t2,p3);printo(p3,t3);getch();}int read(p p[10]){int t1,i;printf("nEnter the total no of terms in polynomial:");scanf("%d",&t1);printf("Enter the coefficient and exponent in descending order");for(i=0;i<t1;i++)scanf("%d%d",&p[i].coeff,&p[i].expo);return(t1);}int add(p p1[10],p p2[10],int t1,int t2,p p3[10]){int i=0,j=0,k=0,t3;while(i<t1&&j<t2){if(p1[i].expo==p2[j].expo){p3[k].coeff=p1[i].coeff+p2[j].coeff;p3[k].expo=p1[i].expo;i++;j++;k++;}else if(p1[i].expo>p2[j].expo){p3[k].coeff=p1[i].coeff;p3[k].expo=p1[i].expo; 13
14. 14. i++;k++;}else{p3[k].coeff=p2[j].coeff;p3[k].expo=p2[j].expo;j++;k++;}}while(i<t1){p3[k].coeff=p1[i].coeff;p3[k].expo=p1[i].expo;i++;k++;}while(j<t2){p3[k].coeff=p2[j].coeff;p3[k].expo=p2[j].expo;j++;k++;}t3=k;return(t3);}void print(p pp[10],int term){int k;printf("Printing the polynomial");for(k=0;k<term-1;k++)printf("%dx^%d+",pp[k].coeff,pp[k].expo);printf("%dx^%d",pp[k].coeff,pp[k].expo);}void printo(p pp[10],int term){int k;printf("nAddition of polynomial");for(k=0;k<term-1;k++)printf("%dx^%d+",pp[k].coeff,pp[k].expo);printf("%dx^%d",pp[k].coeff,pp[k].expo);}INPUT & OUTPUT: 14
15. 15. Enter the total no of terms in polynomial:3Enter the coefficient and exponent in descending order332210Printing the polynomial3x^3+2x^2+1x^0Enter the total no of terms in polynomial:3Enter the coefficient and exponent in descending order423150Printing the polynomial4x^2+3x^1+5x^0Addition of polynomial3x^3+6x^2+3x^1+6x^0RESULT: Thus the program for program for polynomial addition has beencompleted successfully and output verified.Ex no – 3 IMPLEMENTATION OF STACK 15
16. 16. AIM: To write a c program to implement stack operations using array.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the needed variables and initialize them. Step4: In push () operation, check whether the stack is full or not. If the stack is not full, insert the element into the stack by incrementing top value. Step5: In pop () operation, check whether the stack is empty or not. If the stack is not empty, delete the element from the stack by decrementing top value. Step6: In display () method, print the stack elements using for loop. Step7: In main () method, using switch case statement to invoke the methods according to the user choice entered. Step8: End of the program.PROGRAM:#include<stdio.h> 16
17. 17. #include<conio.h>#define size 10int stack[size],top=-1;void push();void pop();void display();void main(){int c;clrscr();printf("nn1.pushn2.popn3.displayn4.Exit");do{printf("nnEnter your choice::");scanf("%d",&c);switch(c){case 1:push();break;case 2:pop();break;case 3:printf("nnContents of stack ist");display();break;default:printf("nInvalid Choice");exit(0);}}while(c<4);getch();}void push(){int b;if(top>size-1){printf("nstack over flow");return;}else{printf("nEnter the number to push into the stack:");scanf("%d",&b); 17
18. 18. top++;stack[top]=b;return;}}void pop(){int res;if(top==0){printf("nStack Underflow");}else{res=stack[top];top--;printf("nDeleted element is %d",res);return;}}void display(){int i;if(top==-1){printf("nStack Under flow");return;}else{for(i=top;i>=0;i--)printf("%d",stack[i]);}}INPUT & OUTPUT:1.push 18
19. 19. 2.pop3.display4.ExitEnter your choice::1Enter the number to push into the stack:1Enter your choice::1Enter the number to push into the stack:2Enter your choice::1Enter the number to push into the stack:3Enter your choice::3Contents of stack is 3 2 1Enter your choice::2Deleted element is 3Enter your choice::3Contents of stack is 2 1Enter your choice:: 4<Exiting>RESULT: Thus the program for program for implementation of stack using array has beencompleted successfully and output verified.Ex.No:4 INFIX TO POSTFIX CONVERSION 19
20. 20. AIM: Write a C program to Implement stack and use it to convert infix to postfixexpressionALGORITHM: Step1: Start the program. Step2: Initialize the stack. Step3: Read the given infix expression into string called infix. Step4: If the character is an operand, place it on the output. Step5: If the character is an operator, push it on to the stack. if the stack operator has a higher or equal priority than input operator then pop that operator from the stack and place it onto the output. Step6: If the character is a left parenthesis, push it onto the stack. Step7: If the character is a right parenthesis, pop all operators from the stack till it encounters left parenthesis, discard both the parenthesis in the output. Step8: Display the postfix expression for the given infix expression. Step9: End the program.PROGRAM:#include<stdio.h> 20
21. 21. #include<conio.h>#include<stdlib.h>char inf[40],post[40];int top=0,st[20];void postfix();void push(int);char pop();void main(){clrscr():printf(“Enter the infix expression:”);scanf(“%s”,&inf);postfix();getch();}void postfix(){int i,j=0;for(i=0;inf[i]!=’0’;i++) {switch(inf[i]) {case ‘+’:while(st[top>=1)post[j++]=pop();push(1);break;case ‘-’:while(st[top>=1)post[j++]=pop();push(2);break;case ‘*’:while(st[top>=3)post[j++]=pop();push(3);break;case ‘/’:while(st[top>=3)post[j++]=pop();push(4);break;case ‘^’:while(st[top>=4)post[j++]=pop();push(5);break;case ‘(’: 21
22. 22. push(0);break;case ‘)’:while(st[top!=0)post[j++]=pop();top--;break;default: post[j++]=inf[i];} }while(top>0)post[j++]=pop();printf(“nPostfix expression is %s”,post);}void push(int ele) {top++;st[top]=ele;}char pop(){int e1;char e;e1=st[top];top--;switch(e1) {case 1:e=’+’;break;case 2:e=’-’;break;case 3:e=’*’;break;case 4:e=’/’;break;case 5:e=’^’;break;}return(e);}INPUT & OUTPUT:Enter the infix expression: a+b 22
23. 23. Postfix expression is ab+RESULT: Thus the program for program for infix to postfix expression conversion has beencompleted successfully and output verified.Ex no – 5 IMPLEMENTATION OF QUEUE 23
24. 24. AIM: To write a c program to implement queue operations using array.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the needed variables and initialize them. Step4: In insert () operation, check whether the queue is full or not. If the queue is not full, insert the element into the queue by incrementing rear value. Step5: In delete () operation, check whether the queue is empty or not. If the queue is not empty, delete the element from the queue by incrementing front value. Step6: In display () method, print the queue elements using for loop. Step7: In main () method, using switch case statement to invoke the methods according to the user choice entered. Step8: End of the program.PROGRAM:#include <stdio.h> 24
25. 25. #include<conio.h>#define MAXSIZE 2int q[MAXSIZE];int front=-1,rear=-1,ch;void main(){void insert();int del();void display();clrscr();do{printf("nMAIN MENU:1.Insert 2.Delete 3.Display 4.ExitnEnter ur choice:");scanf("%d",&ch);switch(ch){case 1:insert(); break;case 2: del(); break; case 3: display(); break; case 4: exit(0);default: printf("Invalid Choice ... ");}} while(ch!=4);}void insert(){int num;if(rear==(MAXSIZE-1)) { printf("QUEUE FULL"); return; }else { printf("Enter the no:"); scanf("%d",&num); rear=rear+1; q[rear]=num;if(front==-1)front++; 25
26. 26. }return;}int del(){ int num;if(front==-1) { printf("QUEUE EMPTY"); return 0; }else { num=q[front]; printf("nDeleted element is %d",q[front]); front++; } return(num); }void display(){int i;if(front==-1){printf("Queue empty");return;}else{printf("nQueue elements are:");for(i=front;i<=rear;i++)printf("%dt",q[i]);}}INPUT & OUTPUT: 26
27. 27. MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:3Queue emptyMAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:1Enter the no:1MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:1Enter the no:2MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:1QUEUE FULLMAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:2Deleted element is 1MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:2Deleted element is 2MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:3Queue elements are:MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:4<Exiting>RESULT: Thus the program for program for implementation of queue using array has beencompleted successfully and output verified.Ex no – 6 IMPLEMENTATION OF CIRCULAR 27
28. 28. QUEUEAIM: To write a c program to implement circular queue using array.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the needed variables and initialize them. Step4: In insert () operation, check whether the queue is full or not. If the queue is not full, check the front value if no element is there, insert the element into the queue in front position. Step5: In delete () operation, check whether the queue is empty or not. If the queue is not empty, delete the element from the queue by incrementing front value. Step6: In display () method, print the queue elements using for loop. Step7: In main () method, using switch case statement to invoke the methods according to the user choice entered. Step8: End of the program. 28
29. 29. PROGRAM:#include<stdio.h>#include<conio.h>#define max 5int q[max];int front=-1;int rear=-1;void main(){int ch;clrscr();while(1){printf("enter your choice");scanf("%d",&ch);switch(ch){case 1:insert();break;case 2:del();break;case 3:display();break;case 4:exit(1);default:printf("in wrong choice");}}}insert(){int item;if((front==0&&rear==max-1)||(front==rear+1)){printf("n queue overflow");return;}if (front==-1){front=0;rear=0;} 29
30. 30. elseif(rear==max-1)rear=0;elserear=rear+1;printf("enter the element");scanf("%d",&item);q[rear]=item;}del(){if(front==-1){printf("queue underflown");return;}printf("element deleted from queue is %d:n",q[front]);if(front==rear){rear=-1;front=-1;}elseif(front==max-1)front=0;elsefront=front+1;}display(){int fpos=front,rpos=rear;if(front==-1){printf("queue is emptyn");return;}printf("queue elements:n");if(fpos<=rpos)while(fpos<=rpos){printf("%dn",q[fpos]);fpos++;}else{ 30
31. 31. while(fpos<=max-1){printf("%dn",q[fpos]);fpos++;}fpos=0;while(fpos<=rpos){printf("%d",q[fpos]);fpos++;}}printf("n");} 31
32. 32. INPUT & OUTPUT:1.insert 2.delete 3.display 4.quitenter your choice1enter the element11.insert 2.delete 3.display 4.quitenter your choice1enter the element21.insert 2.delete 3.display 4.quitenter your choice3queue elements:121.insert 2.delete 3.display 4.quitenter your choice2element deleted from queue is 1:1.insert 2.delete 3.display 4.quitenter your choice3queue elements:21.insert 2.delete 3.display 4.quitenter your choice4<Exiting>RESULT: Thus the program for implementation of circular queue has been completedsuccessfully and output verified. 32
33. 33. Ex no – 7 IMPLEMENTATION OF EXPRESSION TREEAIM: To write a c program to implement an expression tree. Produce its pre-order, in-order, and post order traversals.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the function inorder(), postorder(), preorder(). Step4: Read the input in the form of postfix expression. Step5: In inorder () function, traversal will be from left child-->root-->right child. Step6: In preorder () function, traversal will be from root-->left child--> right child. Step7: In postorder () function, traversal will be from left child--> right child-->root. Step8: In main () method, using switch case statement to invoke the methods according to the user choice entered. Step9: End of the program. 33
34. 34. PROGRAM:#include<stdio.h>#include<conio.h>#include<stdlib.h>#define size 20typedef struct node{char data;struct node *left;struct node *right;}btree;btree *stack[size];int top;void main(){btree *root;char exp[80];btree *create(char exp[80]);void inorder(btree *root);void preorder(btree *root);void postorder(btree *root);clrscr();printf(“nEnter the postfix expression:”);scanf(“%s”,&exp);top=-1;root=create(exp);printf(“nInorder traversal:”);inorder(root);printf(“nPreorder traversal:”);preorder(root);printf(“nPostorder traversal:”);postorder(root);getch();}btree *create(char exp[]){btree *temp;int pos;char ch;void push(btree *);btree *pop();pos=0;ch=exp[pos];while(ch!=’0’){temp=(btree *)malloc(sizeof(btree)); 34
35. 35. temp->left=temp->right=NULL;temp->data=ch;if(isalpha(ch))push(temp);else if(ch==’+’||ch==’-‘||ch==’*’||ch==’/’) {temp->right=pop();temp->left=pop();push(temp); }elseprintf(“nInvalid character in expression”);pos++;ch=exp[pos];}temp=pop();return(temp); }void push(btree *node) {if(top+1>=size)printf(“Error:Stack is full”);top++;stack[top]=node;}btree *pop(){btree *node;if(top==-1)printf(“Error:Stack empty”);node=stack[top];top--;return(node);}void inorder(btree *root){btree *temp;temp=root;if(temp!=NULL){inorder(temp->left);printf(“%c”,temp->data);inorder(temp->right);}}void preorder(btree *root) { 35
36. 36. btree *temp;temp=root;if(temp!=NULL){printf(“%c”,temp->data);preorder(temp->left);preorder(temp->right);}}void postorder(btree *root){btree *temp;temp=root;if(temp!=NULL) {postorder(temp->left);postorder(temp->right);printf(“%c”,temp->data);}} 36
37. 37. INPUT & OUTPUT:Enter the postfix expression:ab+cd-*Inorder traversal:a+b*c-dPreorder traversal:*+ab-cdPostorder traversal:ab+cd-*RESULT: Thus the program for implementation of expression tree and its traversal order hasbeen completed successfully and output verified. 37
38. 38. Ex no – 8 IMPLEMENTATION OF BINARY SEARCH TREEAIM: To write a c program to implement binary search tree.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the function add(),search(), findmin(),findmax() and display(). Step4: In main () method, using switch case statement to invoke the methods according to the user choice entered. Step5: In add () function, get the number from user and insert it into tree. Step6: In search () function, the number given for search will be traversed and result of the searching will be displayed. Step7: In findmin () function, display the minimum element in the tree Step8: In findmax () function, display the maximum element in the tree Step8: In display() function, the binary search tree will be displayed. Step9: End of the program. 38
39. 39. PROGRAM:#include<stdio.h>#include<conio.h>#include<stdlib.h>typedef struct node{int data;struct node *left, *right;}*nptr;nptr root,t,p,q;void add();void search();void findmin();void findmax();void disp(nptr,int,int,int);void main(){int ch;root=NULL;while(1){printf(“1.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitnEnter ur choice:”);scanf(“%d”,&ch);switch(ch){case 1:add();break;case 2:search();break;case 3:clrscr();disp(root,40,7,16);break;case 4:findmin();break;case 5:findmax();break;case 6:exit(0);}}} 39
40. 40. void add(){int x;t=(nptr)malloc(sizeof(struct node));printf(“nEnter data:”);scanf(“%d”,&x);t->data=x;t->left=NULL;t->right=NULL;if (root==NULL)root=t;else{p=q=root;while(q!=NULL&&x!=p->data){p=q;if(x<p->data)q=p->left;elseq=p->right;}if(x==p->data)printf(“%d is duplicate no n”,x);else if(x<p->data)p->left=t;elsep->right=t;}}void search(){int x;printf(“Enter the element to search:”);scanf(“%d”,&x);p=q=root;while(q!=NULL&&x!=p->data){p=q;if(x<p->data)q=p->left;elseq=p->right;}if(x==p->data)printf(“Element is present”); 40
41. 41. elseprintf(“Element not present”);}void disp(nptr root,int col,int row,int wid){gotoxy(col,row);if(root!=NULL){printf(“%d”,root->data);disp(root->left,col-wid,row+2,wid/2);disp(root->right,col+wid,row+2,wid/2);}}void findmax(){t=root;while(t->right!=NULL)t=t->right;printf(Maximum value in BST is %d”,t->data);}void findmin(){t=root;while(t->left!=NULL)t=t->left;printf(Minimum value in BST is %d”,t->data);} 41
42. 42. INPUT & OUTPUT:1.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:1Enter data:101.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:1Enter data:91.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:1Enter data:81.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:1Enter data:111.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:1Enter data:121.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:3 10 9 11 8 121.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:2Enter the element to search:8Element is present1.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:2Enter the element to search:7Element not present1.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:4Minimum value in BST is 81.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:5Maximum value in BST is 121.Create 2.Search 3.Display 4.Findmin 5.Findmax 6.ExitEnter ur choice:6 <Exiting…..>RESULT: Thus the program for implementation of binary search tree has been completedsuccessfully and output verified. 42
43. 43. Ex no – 9 IMPLEMENTATION OF PRIORITY QUEUE USING HEAPSAIM: To write a c program to implement priority queue using heap.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the needed variables and initialize them. Step4: By defining the priority queue, we can perform operations on heap. Step5: Insert an item arbitrarily at anywhere in the priority queue. Step6: Delete an item that has highest priority i.e. maximum value from the priority queue. This is called max heap. Step7: Delete an item that has lowest priority i.e. minimum value from the priority queue. This is called min heap. Step8: Print the contents of heap using for loop. Step9: End of the program. 43
44. 44. PROGRAM:#include<stdio.h>#include<conio.h>#define size 5void main(void){int rear,front,que[size],choice;int qfull(int rear),qempty(int rear,int front);int insert(int que[size],int rear,int front);int delet(int que[size],int front);void display(int que[size],int rear,int front);clrscr();front=0;rear=-1;do{printf("nMainmenu1:insert 2:delete 3:display 4.Exitnenter ur choice: ");scanf("%d",&choice);switch(choice){case 1:if(qfull(rear))printf("nqueue is full");elserear=insert(que,rear,front);break;case 2:if(qempty(rear,front))printf("ncannot delete element");elsefront=delet(que,front);break;case 3:if(qempty(rear,front))printf("nqueue is empty");elsedisplay(que,rear,front);break;case 4:exit(0);default:printf("nwrong choice");break;}}while(choice!=4);getch();} 44
45. 45. int insert(int que[size],int rear,int front){int item,j;printf("nenter the element : ");scanf("%d",&item);if(front==-1)front++;j=rear;while(j>=0&&item<que[j]){que[j+1]=que[j];j--;}que[j+1]=item;rear=rear+1;return rear;}int qfull(int rear){if(rear==size-1)return 1;elsereturn 0;}int delet(int que[size],int front){int item;item=que[front];printf("nthe item deleted is %d",item);front++;return front;}qempty(int rear,int front){if((front==-1)||(front>rear))return 1;elsereturn 0;}void display(int que[size],int rear,int front){int i;printf("nthe queue is :");for(i=front;i<=rear;i++)printf(" %d",que[i]);} 45
46. 46. INPUT & OUTPUT:Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 1enter the element : 1Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 1enter the element : 2Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 1enter the element : 3Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 3the queue is : 1 2 3Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2the item deleted is 1Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2the item deleted is 2Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2the item deleted is 3Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2cannot delete elementMainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 3queue is emptyMainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 4<Exiting>RESULT: Thus the program for implementation of priority queue using heap has beencompleted successfully and output verified. 46
47. 47. Ex no – 10 IMPLEMENTATION OF HASHING TECHINQUEAIM: To write a c program to implement hashing by linear probing.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare and initialize needed variables. Step4: Get the size of the hash table from the user and makes the indices sequentially based on size. Step5: Then get the integer number from the user . And compute the hash function for that number using the given formula. Hash function= (int) e % size of the hash table Step6: Finally, store the number in corresponding location in the hash table. Step7: Check whether the hash table is empty or full during insertion and deletion function. Step8: Print the contents of hash table whenever the user needs to view. Step9: End of the program. 47
48. 48. PROGRAM:#include<stdio.h>#include<conio.h>#include<stdlib.h>#define MAX 10void main(){int a[MAX],num,key,i;char ans;int create(int);void linear_prob(int [],int,int),display(int []);clrscr();printf("nCollision handling by linear probing");for(i=0;i<MAX;i++)a[i]=-1;do{printf("nEnter the number");scanf("%d",&num);key=create(num);linear_prob(a,key,num);printf("ndo you wish to continue? (y/n)");ans=getche();}while(ans==y);display(a);getch();}int create(int num){int key;key=num%10;return key;}void linear_prob(int a[MAX],int key,int num){int flag,i,count=0;void display(int a[]);flag=0;if(a[key]==-1)a[key]=num;else{i=0;while(i<MAX){if(a[i]!=-1) 48
49. 49. count++;i++;}if(count==MAX){printf("nhash table is full");display(a);getch();exit(1);}for(i=key+1;i<MAX;i++)if(a[i]==-1){a[i]=num;flag=1;break;}for(i=0;i<key&&flag==0;i++)if(a[i]==-1){a[i]=num;flag=1;break;}}}void display(int a[MAX]){int i;printf("nthe hash table");for(i=0;i<MAX;i++)printf("n%dt%d",i,a[i]);} 49
50. 50. INPUT & OUTPUT:Collision handling by linear probingEnter the number33do you wish to continue? (y/n)yEnter the number83do you wish to continue? (y/n)yEnter the number35do you wish to continue? (y/n)yEnter the number42do you wish to continue? (y/n)yEnter the number74do you wish to continue? (y/n)nthe hash table0 -11 -12 423 334 835 356 747 -18 -19 -1RESULT: Thus the program for implementation of hashing by linear probing has beencompleted successfully and output verified. 50
51. 51. Ex no – 11 IMPLEMENTATION OF TOPOLOGICAL SORTINGAIM: To write a c program to implement topological sorting.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the function push(), pop(), ts(). Step4: In push () function, check whether the stack is full or not. If the stack is not full, insert the vertex into the stack by incrementing top value. Step5: In pop () operation, check whether the stack is empty or not. If the stack is not empty, get the vertex from the stack by decrementing top value. Step6: In ts () function, traversal order for the given graph is found. Step7: In main () method, get the adjacency matrix for given number of vertices from user and call the required function. Step8: End of the program. 51
52. 52. PROGRAM:#include<stdio.h>#include<conio.h>#define max 30int stack[max],top=-1;int push(int v);int pop(int *v);int ts(int mat[max][max],int n,int order[max]);void main(){int i,j,n,mat[max][max],order[max],succ;clrscr();printf("nenter the no. of vertices : ");scanf("%d",&n);printf("nenter adjacency matrix :n");for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&mat[i][j]);succ=ts(mat,n,order);if(succ==1){printf("nnthe directed graph is acyclic");printf("nnthe topological sorted order isn");for(i=0;i<n;i++)printf("%dt",order[i]+1);}elseprintf("nthe directed graph is not acyclic");getch();}int push(int v){if(top==max-1)return 0;elsestack[++top]=v;return -1;}int pop(int *v){if(top==-1)return 0;else*v=stack[top--];return -1;}int ts(int mat[max][max],int n,int order[max]) 52
53. 53. {int indegree[max],v,i,k,m;for(i=0;i<n;i++){indegree[i]=0;for(k=0;k<n;k++)if(mat[k][i]==1)indegree[i]++;if(indegree[i]==0)push(i);}m=0;while(pop(&v)){order[m++]=v;for(k=0;k<n;k++)if(mat[v][k]==1&&indegree[k]>0){indegree[k]--;if(indegree[k]==0)push(k);}}return (i==n);} 53
54. 54. INPUT & OUTPUT:Enter the no. of vertices : 7Enter adjacency matrix:0111000000110000000100010011000100100000000000010The directed graph is acyclicThe topological sorted order is1 2 5 4 7 3 6RESULT: Thus the program for implementation of topological sorting has been completedsuccessfully and output verified. 54
55. 55. Ex no – 12 IMPLEMENTATION OF DIJKSTRA’S ALGORITHMAIM: To write a c program to implement dijkstra’s algorithm.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Get the number of vertices from the user and get cost for each vertex. Step4: The general algorithm for this process is as follows, o select a starting node o build the initial fringe from nodes connected to the starting node o while we are not at the destination node do • choose the fringe node with the shortest path to the starting node • add that node and its edge to the tree • update the fringe by:  adding nodes to the fringe connected to the new node  for each node in the fringe do update its edge one connected to the tree on the shortest path to the starting node  end for o end while Step5: Print the shortest path. Step6: End of the program. 55
56. 56. PROGRAM:#include<stdio.h>#include<conio.h>#define INFINITY 1000int a[10][10],b[10][10];int i,j,k,n;void input();void initialize();void spath();void display();void input(){printf("nt *** DIJKSTRA’S ALGORITHM ***");printf("n enter the no of vertices:");scanf("%d",&n);for(i=1;i<=n;i++)for(j=1;j<=n;j++){if(i!=j){printf("cost between %d to %d",i,j);scanf("%d",&a[i][j]);}}}void initialize(){for(i=1;i<=n;i++)a[i][j]=0;for(i=1;i<=n;i++)for(j=1;j<=n;j++){b[i][j]=a[i][j];if(!a[i][j] && (i!=j)){b[i][j]=INFINITY;}}}void spath(){for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)if((b[i][k] && b[k][j]) && (b[i][k]+b[k][j]<b[i][j])){ 56
57. 57. b[i][j]=b[i][k]+b[k][j];}}void display(){i=1;if(i<n){for(j=2;j<=n;j++)printf("Minimum cost FROM source vertex 1 TO %d is : %dn",j,b[i][j]);}}void main(){clrscr();input();initialize();spath();display();getch();} 57
58. 58. INPUT & OUTPUT: *** DIJKSTRA’S ALGORITHM ***enter the no of vertices: 5cost between1—2: 2cost between1—3: 1cost between1—4: 0cost between1—5: 0cost between2—1: 2cost between2—3: 5cost between2—4: 4cost between2—5: 0cost between3—1: 1cost between3—2: 5cost between3—4: 3cost between3—5: 2cost between4—1: 0cost between4—2: 4cost between4—3: 3cost between4—5: 6cost between5—1: 0cost between5—2: 0cost between5—3: 2cost between5—4: 6minimum cost FROM 1 TO 2 is : 2minimum cost FROM 1 TO 3 is : 1minimum cost FROM 1 TO 4 is : 4minimum cost FROM 1 TO 5 is : 3RESULT: Thus the program for implementation of dijkstra’s algorithm has been completedsuccessfully and output verified. 58
59. 59. Ex no – 13(A) IMPLEMENTATION OF PRIM’S ALGORITHMAIM: To write a c program to implement prim’s algorithm.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Get the number of nodes from the user and get weight for each vertex. Step4: The general algorithm for this process is as follows, o select a starting node. o build the initial fringe from nodes connected to the starting node o while there are nodes left do  choose the edge to the fringe of the smallest weight  add the associated node to the tree  update the fringe by: • adding nodes to the fringe connected to the new node • updating the edges to the fringe so that they are the smallest o end while Step5: Print the minimal spanning tree. Step6: End of the program. 59
60. 60. PROGRAM:#include<stdio.h>#include<conio.h>void main(){int lowcost[20],min;int n,noe,i,j,k,v,closest[20],u,cost[50][50];clrscr();printf("ENTER THE NO OF NODES:");scanf("%d",&n);printf("nENTER NO OF EDGES:");scanf("%d",&noe);for(i=1;i<=n;i++){for(j=1;j<=n;j++){cost[i][j]=1000;}}for(i=1;i<=noe;i++){printf("ENTER THE EDGE: ");scanf("%d%d",&u,&v);printf("ENTER COST OF EDGE:");scanf("%d",&cost[u][v]);cost[v][u]=cost[u][v];}printf("nThe output of minimum spanning tree will be...n");for(i=2;i<=n;i++){lowcost[i]=cost[1][i];closest[i]=1;}for(i=2;i<=n;i++){min=lowcost[2];k=2;for(j=3;j<=n;j++){if(lowcost[j]<min){min=lowcost[j];k=j;} 60
61. 61. }printf("%d ->%d:cost %dn",k,closest[k],lowcost[k]);lowcost[k]=2000;for(j=2;j<=n;j++)if((cost[k][j]<lowcost[j]) && (lowcost[j]<2000)){lowcost[j]=cost[k][j];closest[j]=k;}}getch();} 61
62. 62. INPUT & OUTPUT:ENTER THE NO OF NODES:4ENTER NO OF EDGES:5ENTER THE EDGE: 12ENTER COST OF EDGE:9ENTER THE EDGE: 13ENTER COST OF EDGE:3ENTER THE EDGE: 14ENTER COST OF EDGE:2ENTER THE EDGE: 24ENTER COST OF EDGE:1ENTER THE EDGE: 34ENTER COST OF EDGE:6The output of minimum spanning tree will be...4 ->1:cost 22 ->4:cost 13 ->1:cost 3RESULT: Thus the program for implementation of prim’s algorithm has been completedsuccessfully and output verified. 62
63. 63. Ex no – 13(B) IMPLEMENTATION OF KRUSKAL’S ALGORITHMAIM: To write a c program to implement kruskal’s algorithm.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Get the number of nodes from the user and get weight for each node. Step4: Sort the edges in nondecreasing order by weight and initialize partition structure for finding the edges to be included in spanning tree. Step5: Print the minimum spanning tree. Step6: End of the program. 63
64. 64. PROGRAM:#include<stdio.h>#include<conio.h>typedef struct edge{int node1,node2,wt;}edge;void sortedge(edge a[],int n){int i,j;edge temp;for(i=0;i<n;i++)for(j=i+1;j<n;++j)if(a[i].wt>a[j].wt){temp=a[i];a[i]=a[j];a[j]=temp;}}int check(int p[],int i,int j){int v1,v2;v1=i;v2=j;while(p[i]>-1)i=p[i];while(p[j]>-1)j=p[j];if(i!=j){p[j]=i;printf("%d->%dn",v1,v2);return 1;}return 0;}void main(){edge e[100];int r[100],n,i,j,k=1,m,cost=0;clrscr();printf("Kruskal algorithmn");printf("Enter the no of nodes:");scanf("%d",&n);for(i=0;i<n;i++) 64
65. 65. r[i]=-1;i=0;printf("nEnter no of edges:");scanf("%d",&m);for(i=0;i<m;i++){printf("nENter the edge and cost of the edge:");scanf("%d%d%d",&e[i].node1,&e[i].node2,&e[i].wt);}sortedge(e,m);printf("nEdges of the MSTn");i=0;while(k<n){if(check(r,e[i].node1,e[i].node2)){k++;cost=cost+e[i].wt;i++;}}printf("Minimum cost:%d",cost);getch();} 65
66. 66. INPUT & OUTPUT:Kruskal algorithmEnter the no of nodes:4Enter no of edges:4Enter the edge and cost of the edge:121Enter the edge and cost of the edge:133Enter the edge and cost of the edge:232Enter the edge and cost of the edge:241Edges of the MST1->22->42->3Minimum cost:4RESULT: 66
67. 67. Thus the program for implementation of kruskal’s algorithm has been completedsuccessfully and output verified.Ex no – 14 IMPLEMENTATION OF BACKTRACKING ALGORITHMAIM: To write a c program to implement backtracking algorithm for Knapsack problem.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the function knap(), bound(). Step4: Get the input number of items and capacity of knapsack from user. Step5: Get the weight and profit for each item from user. Step6: In knap () function, the maximum capacity and profit are found Step7: In bound () function, the maximum capacity for inserting the item is checked. Step8: In main () method, the values of knapsack are printed in descending order. Step9: End of the program. 67
68. 68. PROGRAM:#include<stdio.h>#include<conio.h>int n,m,i,j,x[10],y[10];float w[10],t,t1,t2,p[10],c1[10],b,c,fp=-1.0,fw;void knap(int,float,float);float bound(float,float,int);void main(){clrscr();printf("nenter the no. of items : ");scanf("%d",&n);printf("nenter the maximum capacity of knapsack : ");scanf("%d",&m);for(i=1;i<=n;i++){printf("nnenter the weight & profit : n");scanf("%f%f",&w[i],&p[i]);}for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){if(c1[i]>=c1[j])t1=w[i];w[i]=w[j];w[j]=t1;t2=p[i];p[i]=p[j];p[j]=t2;t=c1[i];c1[i]=c1[j];c1[j]=t;} }printf("nnweightttprofitn");for(i=1;i<=n;i++){printf("n%ft%fn",w[i],p[i]);knap(1,0,0);printf("nnthe selected objects: ");for(i=1;i<=n;i++){printf("%d",x[i]); 68
69. 69. }printf(“nFinal Weight=%0.2f”,fw);printf(“nFinal Profit=%0.2f”,fp);getch();}void knap(int k,float cp,float cw){if(cw+w[k]<=m){y[k]=1;if(k<n)knap(k+1,cp+p[k],cw+w[k]);if((cp+p[k]>fp)&&(k==n)){fp=cp+p[k];fw=cw+w[k];for(j=1;j<=n;j++)x[j]=y[j];}if(bound(cp,cw,k)>=fp){y[k]=0;if(k<n)knap(k+1,cp,cw);if((cp>fp)&&(k==n)){fp=cp;fw=cw;for(j=1;j<=n;j++)x[j]=y[j];}}}}float bound(float cp,float cw,int k){float b=cp,c=cw;for(i=k+1;i<=n;i++){c=c+w[i];if(c<m)b=b+p[i];elsereturn(b+(1-(c-m))/w[i]*p[i]);}return b;} 69
70. 70. INPUT & OUTPUT:Enter the no. of items : 5Enter the maximum capacity of knapsack : 20Enter the weight & profit ; 3 12Enter the weight & profit ; 2 4Enter the weight & profit ; 4 8Enter the weight & profit ; 5 10Enter the weight & profit ; 4 12Weight profit4.000000 12.0000005.000000 10.0000004.000000 8.00000002.000000 4.00000003.000000 12.000000The selected objects are: 11111Final Weight=18.00Final Profit=46.00RESULT: 70
71. 71. Thus the program for implementation of backtracking algorithm for knapsackproblem has been completed successfully and output verified.Ex no – 15 IMPLEMENTATION OF BRANCH AND BOUND ALGORITHMAIM: To write a c program to implement branch and bound algorithm for travellingsalesman problem.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the function tsp(), display(). Step4: In main() method, get the number of cities and distance for each city from user. Step5: In tsp() function, finds the minimum distance from source. Step6: In display() function, minimum cost and path is displayed. Step7: End of the program. 71
72. 72. PROGRAM:#include<stdio.h>#include<conio.h>#define max 10typedef struct{int nodes[max];int vertex;int min;}path;path tsp(int src,path list,int ele[][max],int mcities){int i,j;path nlist,npath,nmin;if(list.vertex==0){nmin.min=ele[src][1];nmin.nodes[mcities-1]=src;nmin.vertex=mcities;return nmin;}for(i=0;i<list.vertex;i++){nlist.vertex=0;for(j=0;j<list.vertex;j++)if(i!=j)nlist.nodes[nlist.vertex++]=list.nodes[j];npath=tsp(list.nodes[i],nlist,ele,mcities);npath.min=ele[src][list.nodes[i]]+npath.min;npath.nodes[mcities-list.vertex-1]=src;if(i==0)nmin=npath;elseif(npath.min<nmin.min)nmin=npath;}return nmin;}void display(path path1){int i;printf("nnthe minimum cost is %dn",path1.min);printf("nthe path is....n"); 72
73. 73. for(i=0;i<path1.vertex;i++)printf("%d--",path1.nodes[i]);printf("%d",path1.nodes[0]);}void main(){int i,j,ele[max][max],mcities;path graph,path1;clrscr();printf("nenter number of cities : ");scanf("%d",&mcities);if(mcities==0){printf("error : there is no city for proceeding the TSP");}else{for(i=1;i<=mcities;i++){for(j=1;j<=mcities;j++)if(i==j)ele[i][i]=0;else{printf("enter distance from city %d to %d [if no path put 999]: ",i,j);scanf("%d",&ele[i][j]);}if(i>1)graph.nodes[i-2]=i;}graph.vertex=mcities-1;path1=tsp(1,graph,ele,mcities);display(path1);}getch();} 73
74. 74. INPUT & OUTPUT:Enter number of cities : 4Enter distance from city 1 to 2 [if no path put 999]: 1Enter distance from city 1 to 3 [if no path put 999]: 999Enter distance from city 1 to 4 [if no path put 999]: 999Enter distance from city 2 to 1 [if no path put 999]: 999Enter distance from city 2 to 3 [if no path put 999]: 5Enter distance from city 2 to 4 [if no path put 999]: 1Enter distance from city 3 to 1 [if no path put 999]: 1Enter distance from city 3 to 2 [if no path put 999]: 999Enter distance from city 3 to 4 [if no path put 999]: 3Enter distance from city 4 to 1 [if no path put 999]: 4Enter distance from city 4 to 2 [if no path put 999]: 999Enter distance from city 4 to 3 [if no path put 999]: 1The minimum cost is 4The path is…..1---2---4---3---1RESULT: 74
75. 75. Thus the program for implementation of branch and bound algorithm fortravelling salesman problem has been completed successfully and output verified.Ex no –16 IMPLEMENTATION OF RANDOMIZED ALGORITHMAIM: To write a c program to implement randomized algorithm- to find repeatedelements in the array.ALGORITHM: Step1: Start the program. Step2: Include the required header files at the top of the program. Step3: Declare the function repetition(). Step4: In main () method, used to print the repeated elements in the given array. Step5: Repetition () function, checks whether the declared array has repeated elements. Step6: End of the program. 75
76. 76. PROGRAM:#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<time.h>int main(void){int a[10]={10,20,30,40,11,12,13,10,30,30};int n=10;void repetition(int a[],int n);clrscr();printf("ntRANDOMIZED ALGORITHM - TO FIND REPEATED ELEMENTSn");repetition(a,n);getch();return 0;}void repetition(int a[],int n){int i,j,count;time_t t;srand((unsigned)time(&t));count=1;while(count<=100){i=rand()%(n+1);j=rand()%(n+1);if((i!=j)&&(a[i]==a[j]))printf("nthe repeated element is present at index %d",i);count++;}} 76
77. 77. INPUT & OUTPUT:RANDOMIZED ALGORITHM – TO FIND REPEATED ELEMENTSThe repeated element is present at index 9The repeated element is present at index 9The repeated element is present at index 2The repeated element is present at index 0The repeated element is present at index 0The repeated element is present at index 7The repeated element is present at index 7The repeated element is present at index 9The repeated element is present at index 7The repeated element is present at index 9RESULT: 77
78. 78. Thus the program for implementation of randomized algorithm to find repeatedelements in the array has been completed successfully and output verified. 78