Final ds record

100
Ex no –1(A) IMPLEMENTATION OF SINGLY LINKED LIST AIM: 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. 1
  • date post

    21-Oct-2014
  • Category

    Education

  • view

    1.189
  • download

    5

description

 

Transcript of Final ds record

Page 1: Final ds record

Ex no –1(A) IMPLEMENTATION OF SINGLY LINKED LIST

AIM:

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

Page 2: Final ds record

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.creation\n2.insertion\n3.deletion\n4.display\n5.exit\n"); printf("\nenter ur choice : "); scanf("%d",&ch); switch(ch) {

case 1:creation(n1);break;

case 2:printf("\n\nenter the postion in which to be inserted : ");scanf("%d",&pos);printf("\n\nenter the value : ");scanf("%d",&val);insertion(pos,val);break;

case 3:printf("\n\nenter the position to be deleted : ");scanf("%d",&pos);deletion(pos);break;

case 4:display();getch();break;

case 5:exit(0);

} }while(ch!=5);

2

Page 3: Final ds record

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("%d\t",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

Page 4: Final ds record

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

INPUT & OUTPUT:

4

Page 5: Final ds record

1.creation2.insertion3.deletion4.display5.exitenter ur choice : 1Enter the no of nodes:2enter the 1 node : 1enter the 2 node : 2

1.creation2.insertion3.deletion4.display5.exitenter ur choice : 2enter the postion in which to be inserted : 1enter the value : 6

1.creation2.insertion3.deletion4.display5.exitenter ur choice : 4the nodes entered are : 6 1 2 null

1.creation2.insertion3.deletion4.display5.exitenter ur choice : 2enter the postion in which to be inserted : 2enter the value : 4

1.creation2.insertion3.deletion4.display5.exitenter ur choice :2

enter the postion in which to be inserted : 2enter the value : 4

5

Page 6: Final ds record

1.creation2.insertion3.deletion4.display5.exitenter ur choice : 3enter the position to be deleted : 2

1.creation2.insertion3.deletion4.display5.exitenter ur choice : 4the nodes entered are : 6 1 2 null

1.creation2.insertion3.deletion4.display5.exitenter ur choice : 5<Exiting>

RESULT:

Thus the program for program for implementation of singly linked list has been completed successfully and output verified.

Ex no –1(B) IMPLEMENTATION OF DOUBLY LINKED LIST

6

Page 7: Final ds record

AIM:

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>#include<conio.h>

7

Page 8: Final ds record

struct list{int data;struct list *llink,*rlink;};typedef struct list node;node *head=NULL, *tail=NULL;void ins(node *head){node *p,*q;int d,t;q=(node*)malloc( sizeof(node) );printf("enter the element into the list :");scanf("%d" ,&d);q->data=d;q->rlink=q-> llink=NULL;p=head;do{if(p->rlink->llink==NULL){ q->llink=head;head->rlink= q;q->rlink=tail;tail->llink= q;t=1;}elseif(p->rlink->data>d){p->rlink->llink= q;q->llink=p;q->rlink=p-> rlink;p->rlink=q;t=1;}elsep=p->rlink;}while(t!=1);while(p->llink!=NULL)p=p->llink;head=p;}void delete(node *head){int d,t;

8

Page 9: Final ds record

node *p, *fntptr;printf("enter the deleted element :");scanf("%d",& d);p=head;do{fntptr=p->rlink;if(p->rlink->data==d){fntptr=fntptr->rlink;p->rlink=fntptr;fntptr=p;t=1;}elseif(d>p->rlink->data)p=fntptr;else{puts("\n data not found \n");t=1;}}while(t!=1);while(p->llink!=NULL)p=p->llink;head=p;}void dis(node *head){node *p;p=head->rlink;while(p->rlink!=NULL){printf("%d-> ",p->data) ;p=p->rlink;}while(p->llink!=NULL)p=p->rlink;head=p;printf("NULL\n");}void main(){int ch;clrscr();

9

Page 10: Final ds record

printf(" Doubly linked list operations\n" );printf("1.insert 2.delete 3.display 4.exit \n");head=(node*) malloc(sizeof( node));tail=(node*) malloc(sizeof( node));head->data=0;head->llink= NULL;tail->data=1000;tail->rlink= NULL;head->rlink= tail;tail->llink= head;do{printf("enter your choice :");scanf("%d",& ch);switch(ch){case 1:ins(head);break;case 2:delete(head) ;break;case 3:dis(head);getch();}}while(ch!=4) ;free(head);free(tail);}

INPUT & OUTPUT:

Doubly linked list operations

10

Page 11: Final ds record

1.insert 2.delete 3.display 4.exit

enter your choice :1enter the element into the list :1

enter your choice :1enter the element into the list :2

enter your choice :31-> 2-> NULL

enter your choice :2enter the deleted element :4data not found

enter your choice :2enter the deleted element :1

enter your choice :32-> NULL

enter your choice :4<Exiting>

RESULT:

Thus the program for program for implementation of doubly linked list has been completed successfully and output verified.

Ex no – 2 POLYNOMIAL ADDITION

AIM:

11

Page 12: Final ds record

To write a c program represent a polynomial as a linked list and write functions

for 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>typedef struct poly{

12

Page 13: Final ds record

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;i++;k++;}

13

Page 14: Final ds record

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:

Enter the total no of terms in polynomial:3Enter the coefficient and exponent in descending order3

14

Page 15: Final ds record

32210Printing the polynomial3x^3+2x^2+1x^0

Enter the total no of terms in polynomial:3Enter the coefficient and exponent in descending order423150Printing the polynomial4x^2+3x^1+5x^0

Addition of polynomial3x^3+6x^2+3x^1+6x^0

RESULT:

Thus the program for program for polynomial addition has been

completed successfully and output verified.

Ex no – 3 IMPLEMENTATION OF STACK

AIM:

To write a c program to implement stack operations using array.

15

Page 16: Final ds record

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>#include<conio.h>#define size 10int stack[size],top=-1;void push();

16

Page 17: Final ds record

void pop();void display();void main(){int c;clrscr();printf("\n\n1.push\n2.pop\n3.display\n4.Exit");do{printf("\n\nEnter your choice::");scanf("%d",&c);switch(c){case 1:push();break;case 2:pop();break;case 3:printf("\n\nContents of stack is\t");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);top++;stack[top]=b;return;}

17

Page 18: Final ds record

}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.push2.pop3.display4.Exit

18

Page 19: Final ds record

Enter your choice::1Enter the number to push into the stack:1

Enter your choice::1Enter the number to push into the stack:2

Enter your choice::1Enter the number to push into the stack:3

Enter your choice::3Contents of stack is 3 2 1

Enter your choice::2Deleted element is 3

Enter your choice::3Contents of stack is 2 1

Enter your choice:: 4<Exiting>

RESULT:

Thus the program for program for implementation of stack using array has been completed successfully and output verified.

Ex.No:4 INFIX TO POSTFIX CONVERSION

AIM:

19

Page 20: Final ds record

Write a C program to Implement stack and use it to convert infix to postfix

expression

ALGORITHM:

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>#include<conio.h>#include<stdlib.h>char inf[40],post[40];

20

Page 21: Final ds record

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 ‘(’:push(0);break;case ‘)’:

21

Page 22: Final ds record

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+bPostfix expression is ab+

22

Page 23: Final ds record

RESULT:

Thus the program for program for infix to postfix expression conversion has been completed successfully and output verified.

Ex no – 5 IMPLEMENTATION OF QUEUE

AIM:

To write a c program to implement queue operations using array.

23

Page 24: Final ds record

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>#include<conio.h>#define MAXSIZE 2int q[MAXSIZE];int front=-1,rear=-1,ch;void main(){

24

Page 25: Final ds record

void insert();int del();void display();clrscr();do{printf("\nMAIN MENU:1.Insert 2.Delete 3.Display 4.Exit\nEnter 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++;}return;}int del(){ int num;if(front==-1)

25

Page 26: Final ds record

{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("%d\t",q[i]);}}

INPUT & OUTPUT:

MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:3Queue empty

MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:1

26

Page 27: Final ds record

Enter the no:1

MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:1Enter the no:2

MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:1QUEUE FULL

MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:2Deleted element is 1

MAIN MENU:1.Insert 2.Delete 3.Display 4.ExitEnter ur choice:2Deleted element is 2

MAIN 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 been completed successfully and output verified.

Ex no – 6 IMPLEMENTATION OF CIRCULAR QUEUE

AIM:

To write a c program to implement circular queue using array.

27

Page 28: Final ds record

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.

PROGRAM:#include<stdio.h>#include<conio.h>#define max 5int q[max];int front=-1;int rear=-1;void main()

28

Page 29: Final ds record

{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;}elseif(rear==max-1)rear=0;elserear=rear+1;printf("enter the element");scanf("%d",&item);q[rear]=item;

29

Page 30: Final ds record

}del(){if(front==-1)

{printf("queue underflow\n");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 empty\n");return;}printf("queue elements:\n");if(fpos<=rpos)while(fpos<=rpos){printf("%d\n",q[fpos]);fpos++;}else{while(fpos<=max-1){printf("%d\n",q[fpos]);fpos++;}fpos=0;while(fpos<=rpos){

30

Page 31: Final ds record

printf("%d",q[fpos]);fpos++;}}printf("\n");}

INPUT & OUTPUT:

1.insert 2.delete 3.display 4.quitenter your choice1enter the element1

1.insert 2.delete 3.display 4.quitenter your choice1enter the element2

31

Page 32: Final ds record

1.insert 2.delete 3.display 4.quitenter your choice3queue elements:12

1.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:2

1.insert 2.delete 3.display 4.quitenter your choice4<Exiting>

RESULT:

Thus the program for implementation of circular queue has been completed

successfully and output verified.

Ex no – 7 IMPLEMENTATION OF EXPRESSION TREE

AIM:

To write a c program to implement an expression tree. Produce its pre-order, in-

order, and post order traversals.

32

Page 33: Final ds record

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.

PROGRAM:#include<stdio.h>#include<conio.h>#include<stdlib.h>#define size 20typedef struct node{char data;struct node *left;struct node *right;

33

Page 34: Final ds record

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

34

Page 35: Final ds record

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) {btree *temp;temp=root;if(temp!=NULL) {printf(“%c”,temp->data);preorder(temp->left);preorder(temp->right);} }void postorder(btree *root)

35

Page 36: Final ds record

{btree *temp;temp=root;if(temp!=NULL) {postorder(temp->left);postorder(temp->right);printf(“%c”,temp->data);} }

INPUT & OUTPUT:

Enter the postfix expression:ab+cd-*

Inorder traversal:a+b*c-dPreorder traversal:*+ab-cdPostorder traversal:ab+cd-*

36

Page 37: Final ds record

RESULT:

Thus the program for implementation of expression tree and its traversal order has

been completed successfully and output verified.

Ex no – 8 IMPLEMENTATION OF BINARY SEARCH TREE

AIM:

To write a c program to implement binary search tree.

ALGORITHM:

Step1: Start the program.

37

Page 38: Final ds record

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.

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

38

Page 39: Final ds record

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.Exit\nEnter 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);}}}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

39

Page 40: Final ds record

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

40

Page 41: Final ds record

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

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:11

41

Page 42: Final ds record

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

1.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 completed

successfully and output verified.

Ex no – 9 IMPLEMENTATION OF PRIORITY QUEUE USING HEAPS

AIM:

To write a c program to implement priority queue using heap.

ALGORITHM:

Step1: Start the program.

42

Page 43: Final ds record

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.

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;

43

Page 44: Final ds record

do{printf("\nMainmenu1:insert 2:delete 3:display 4.Exit\nenter 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();}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;

44

Page 45: Final ds record

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

INPUT & OUTPUT:

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 1enter the element : 1

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 1enter the element : 2

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 1enter the element : 3

45

Page 46: Final ds record

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 3the queue is : 1 2 3

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2the item deleted is 1

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2the item deleted is 2

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2the item deleted is 3

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 2cannot delete element

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 3queue is empty

Mainmenu1:insert 2:delete 3:display 4.Exitenter ur choice: 4<Exiting>

RESULT:

Thus the program for implementation of priority queue using heap has been completed successfully and output verified.

Ex no – 10 IMPLEMENTATION OF HASHING TECHINQUE

AIM:

To write a c program to implement hashing by linear probing.

ALGORITHM:

Step1: Start the program.

46

Page 47: Final ds record

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.

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;

47

Page 48: Final ds record

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

48

Page 49: Final ds record

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%d\t%d",i,a[i]);}

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 -1

49

Page 50: Final ds record

2 423 334 835 356 747 -18 -19 -1

RESULT:

Thus the program for implementation of hashing by linear probing has been completed successfully and output verified.

Ex no – 11 IMPLEMENTATION OF TOPOLOGICAL SORTING

AIM:

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

50

Page 51: Final ds record

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.

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

51

Page 52: Final ds record

for(j=0;j<n;j++)scanf("%d",&mat[i][j]);succ=ts(mat,n,order);if(succ==1){printf("\n\nthe directed graph is acyclic");printf("\n\nthe topological sorted order is\n");for(i=0;i<n;i++)printf("%d\t",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]){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++)

52

Page 53: Final ds record

if(mat[v][k]==1&&indegree[k]>0){indegree[k]--;if(indegree[k]==0)push(k);}}return (i==n);}

INPUT & OUTPUT:Enter the no. of vertices : 7Enter adjacency matrix:0 1 1 1 0 0 00 0 0 1 1 0 0 0 0 0 0 0 1 00 0 1 0 0 1 1 0 0 0 1 0 0 10 0 0 0 0 0 00 0 0 0 0 1 0

The directed graph is acyclic

The topological sorted order is

1 2 5 4 7 3 6

53

Page 54: Final ds record

RESULT:

Thus the program for implementation of topological sorting has been completed

successfully and output verified.

Ex no – 12 IMPLEMENTATION OF DIJKSTRA’S ALGORITHM

AIM:

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,

54

Page 55: Final ds record

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.

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("\n\t *** 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++){

55

Page 56: Final ds record

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])){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 : %d\n",j,b[i][j]);}}void main(){clrscr();input();initialize();spath();

56

Page 57: Final ds record

display();getch();}

INPUT & OUTPUT:

*** DIJKSTRA’S ALGORITHM *** enter the no of vertices: 5

cost between1—2: 2 cost between1—3: 1cost between1—4: 0

57

Page 58: Final ds record

cost 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 : 3

RESULT:

Thus the program for implementation of dijkstra’s algorithm has been completed successfully and output verified.

Ex no – 13(A) IMPLEMENTATION OF PRIM’S ALGORITHM

AIM:

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.

58

Page 59: Final ds record

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.

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

59

Page 60: Final ds record

}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;}}printf("%d ->%d:cost %d\n",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();}

60

Page 61: Final ds record

INPUT & OUTPUT:

ENTER THE NO OF NODES:4

ENTER 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:6

61

Page 62: Final ds record

The output of minimum spanning tree will be...4 ->1:cost 22 ->4:cost 13 ->1:cost 3

RESULT:

Thus the program for implementation of prim’s algorithm has been completed successfully and output verified.

Ex no – 13(B) IMPLEMENTATION OF KRUSKAL’S ALGORITHM

AIM:

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.

62

Page 63: Final ds record

Step6: End of the program.

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

63

Page 64: Final ds record

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->%d\n",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 algorithm\n");printf("Enter the no of nodes:");scanf("%d",&n);for(i=0;i<n;i++)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 MST\n");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);

64

Page 65: Final ds record

getch();}

INPUT & OUTPUT:

Kruskal algorithmEnter the no of nodes:4

Enter no of edges:4

Enter the edge and cost of the edge:121

Enter the edge and cost of the edge:133

Enter the edge and cost of the edge:232

Enter the edge and cost of the edge:2

65

Page 66: Final ds record

41

Edges of the MST1->22->42->3Minimum cost:4

RESULT:

Thus the program for implementation of kruskal’s algorithm has been completed successfully and output verified.

Ex no – 14 IMPLEMENTATION OF BACKTRACKING ALGORITHM

AIM:

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.

66

Page 67: Final ds record

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.

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("\n\nenter the weight & profit : \n");scanf("%f%f",&w[i],&p[i]);}for(i=1;i<=n;i++){

67

Page 68: Final ds record

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("\n\nweight\t\tprofit\n");for(i=1;i<=n;i++){printf("\n%f\t%f\n",w[i],p[i]);knap(1,0,0);printf("\n\nthe selected objects: ");for(i=1;i<=n;i++){printf("%d",x[i]);}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);

68

Page 69: Final ds record

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

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 12

Weight profit4.000000 12.0000005.000000 10.0000004.000000 8.00000002.000000 4.00000003.000000 12.000000

The selected objects are: 11111

Final Weight=18.00Final Profit=46.00

69

Page 70: Final ds record

RESULT:

Thus the program for implementation of backtracking algorithm for knapsack

problem has been completed successfully and output verified.

Ex no – 15 IMPLEMENTATION OF BRANCH AND BOUND ALGORITHM

AIM:

To write a c program to implement branch and bound algorithm for travelling

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

70

Page 71: Final ds record

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.

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

71

Page 72: Final ds record

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("\n\nthe minimum cost is %d\n",path1.min);printf("\nthe path is....\n");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)

72

Page 73: Final ds record

graph.nodes[i-2]=i;}graph.vertex=mcities-1;path1=tsp(1,graph,ele,mcities);display(path1);}getch();}

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]: 1

The minimum cost is 4

The path is…..1---2---4---3---1

73

Page 74: Final ds record

RESULT:

Thus the program for implementation of branch and bound algorithm for

travelling salesman problem has been completed successfully and output verified.

Ex no –16 IMPLEMENTATION OF RANDOMIZED ALGORITHM

AIM:

To write a c program to implement randomized algorithm- to find repeated

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

74

Page 75: Final ds record

Step6: End of the program.

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("\n\tRANDOMIZED ALGORITHM - TO FIND REPEATED ELEMENTS\n");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){

75

Page 76: Final ds record

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

INPUT & OUTPUT:

RANDOMIZED ALGORITHM – TO FIND REPEATED ELEMENTS

The repeated element is present at index 9

The repeated element is present at index 9

The repeated element is present at index 2

The repeated element is present at index 0

The repeated element is present at index 0

The repeated element is present at index 7

The repeated element is present at index 7

The repeated element is present at index 9

The repeated element is present at index 7

The repeated element is present at index 9

76

Page 77: Final ds record

RESULT:

Thus the program for implementation of randomized algorithm to find repeated

elements in the array has been completed successfully and output verified.

77