You are on page 1of 53

SINGLY LINKED LIST USING ARRAY AIM: To implement a singly linked list using array.

ALGORITHM: 1) Declare a structure with an element field and the last field to represent the last element in the list. 2) Initialize the value of last field to 1(end). 3) To insert an element into list perform the following steps. a) Get the position of insertion b) Get the element to be inserted. c) Using for loop push the elements forward by 1 to make a room for the element to be inserted. 4) To delete an element from list perform the followings. a) Get the position to be deleted. b) Push the element in P+1th position to position P to close the gap and decrement the last field value 5) To display the contents of the list using a for loop and continue until the last element of the list is displayed. /* SINGLY LINKED LIST USING ARRAY*/ #include<iostream.h> #include<conio.h> class list { struct node { int ele[10]; int last; }l; public: int pos,n; list(); void create(); void dis(); void insert(int,int); void del(int); }; list::list() { l.last=-1; } void list::create() { cout<<"no of elements"<<endl; cin>>n; cout<<"enter the elements"<<endl; for(int i=0;i<n;i++) { cin>>l.ele[i]; } l.last=i; } void list::insert(int x,int p) { cout<<"INSERTION"<<endl; cout<<"ENTER THE ELEMENT"<<endl; cin>>x; cout<<"ENTER THE POSITION"<<endl; cin>>p; int j=n; if(l.last>n) { cout<<"LIST IS FULL"<<endl; } else if((p>l.last)||(p<0)) { cout<<"POSITION DOES NOT EXIST"<<endl; }

else { while(j>p) { l.ele[j+1]=l.ele[j]; j=j-1; } l.ele[p]=x; l.last=l.last+1; } } void list::del(int pos) { cout<<endl<<"DELETION"<<endl; cout<<endl<<"ENTER THE POSITION"<<endl; cin>>pos; if((pos>l.last)||(pos<0)) { cout<<"POSITION DOES NOT EXIST"<<endl; } else { l.last=l.last-1; for(int i=pos;i<=l.last-1;i++) { l.ele[i]=l.ele[i+1]; } } } void list::dis() { cout<<endl<<"THE ELEMENTS OF LIST"<<endl; for(int i=0;i<l.last;i++) { cout<<endl<<l.ele[i]; } } void main() { list l1; int p,po,x1; clrscr(); l1.create(); l1.insert(p,x1); l1.dis(); l1.del(po); l1.dis(); getch(); } OUTPUT : NO. OF ELEMENTS : 3 ENTER THE ELEMENTS : 2 3 4 INSERTION ENTER THE ELEMENT : 5 ENTER THE POSITION : 4 POSITION DOES NOT EXIT THE ELEMENT OF LIST 2 3 4 DELETION ENTER THE POSITION : 0

THE ELEMENT OF LIST 3 4 RESULT : Thus the given singly linked list implementation using array was implemented and output is verified

SINGLY LINKED LIST USING POINTER AIM: To implement a Singly Linked List using Pointers. ALGORITHM: 1) Declare a structure of list such that it contains element field and a pointer field. 2) Make the node as NULL to indicate no element is inserted. 3) Add some elements to the list by making pointer to point to the next element. 4) Make the last nodes pointer field as NULL to indicate the end. 5) To insert an element perform the following a) Create a new node. b) Place the contents into the new nodes element part. c) Change the pointer assignment. 6) To delete an element from the list perform the following a) Get the position to be deleted. b) If the node to be deleted is head change the 1st node as head and delete the head. c) If the node to be deleted is middle one changes the pointer assignment. d) If the last node is to be deleted, make the previous node pointer field as NULL. Delete the last node. 7) Display the contents of the list. /*SINGLY LINKED LIST USING POINTER*/ #include<iostream.h> #include<conio.h> struct s { int data; struct s *next; }node; class link { struct s*p,*head,*node,*last,*temp; public: void create(void); void insert(void); void display(void); void del(void); }; void link::create(void) { p=new s; head=p; cout<<"ENTER THE DATA AND 0 FOR EXIST"<<endl; cin>>p->data; while(p->data!=0) { p->next=new s; p=p->next; cin>>p->data; } last=p; p->next=NULL;

} void link::display(void) { cout<<"THE LIST AFTER PROCESSING"<<endl; p=head; while(p->data!=0) { cout<<p->data<<"\t"; p=p->next; } } void link::insert(void) { link a; int option; int pos; node=new s; p=node; cout<<endl<<"ENTER DATA TO BE INSERTED"<<endl; cin>>p->data; cout<<"ENTER OPTION 1->FIRST 0R 2->LAST 0R 3->MIDDLE"<<endl; cin>>option; switch(option) { case 1: { node->next=head; head=node; break; } case 2: { p=head; while(p->next->next!=NULL) { p=p->next; } node->next=p->next; p->next=node; break; } case 3: { cout<<"ENTER THE POSITION TO BE INSERTED"<<endl; cin>>pos; p=head; while(p->data!=pos) { p=p->next; node->next=p->next; } p->next=node; // while(p->next=NULL && p->data!=pos) // cout<<endl<<"THE ELEMENT YOU ENTERED IS NOT IN THE LIST"; break; } } } void link::del(void) { int item; int option; cout<<endl<<"ENTER DATA TO BE DELETED"<<endl; cin>>item; p=head; cout<<"ENTER THE OPTION 1->FIRST OR 2->LAST OR 3>MIDDLE"<<endl;

cin>>option; switch(option) { case 1: { p=head; head=p->next; delete p; break; } case 2: { while(p->next->next!=NULL) { p=p->next; } p->next=NULL; } case 3: { p=head; while(p->next->data!=item) { p=p->next; } temp=p->next; p->next=temp->next; delete temp; break; } } } void main() { link a; clrscr(); a.create(); a.display(); a.insert(); a.display(); a.del(); a.display(); getch(); } OUTPUT: ENTER THE DATA AND 0 FOR EXIST 10 20 30 0 THE LIST AFTER PROCESSING 10 20 30 ENTER DATA TO BE INSERTED 40 ENTER OPTION 1 FIRST OR 2 LAST OR 3 MIDDLE 2 THE LIST AFTER PROCESSING 10 20 30 40 ENTER DATA TO BE DELETED 30 THE LIST AFTER PROCESSING 10 20 40 RESULT : Thus the above program singly linked list using pointer was developed and executed

DOUBLY LINKED LIST AIM: To implement a Doubly linked list. ALGORITHM: 1) Create a node with an element field and two pointer fields (Prev, next) 2) Make the previous pointer as NULL. 3) Create required amount of nodes until no value is pressed. 4) Make the next pointer of last nodes to be NULL. 5) To insert an element perform the following steps. a) Create a temporary node. b) Get the position of insertion. c) Insert the element into the element field of temporary node. d) Insert the node by making appropriate change in the pointer assignments. 6) To delete an element from the list perform the following steps. a) Get the position of deletion. b) Change the pointer assignments. c) Delete the free node. 7) Display the contents of the list. /*DOUBLY LINKED LIST USING POINTER*/ #include<iostream.h> #include<conio.h> class dlist { public: struct node { int data; struct node *f1,*b1; }*j,*r,*r1,*u,*st,*p2,*p,*q,*t,*p1,*p4; dlist(); void create(int); void insert(int,int); void del(int); void prlis(); }; dlist::dlist() { st=NULL; } void dlist::create(int num) { if(st==NULL) { p=new node; p->data=num; p->f1=NULL; p->b1=NULL; st=p; } else { p=st; while(p!=NULL) { j=p; p=p->f1; } t=new node; t->data=num; t->f1=NULL; t->b1=j; j->f1=t; } } void dlist::insert(int num,int pos)

{ int c=1; if(pos==1) { t=new node; t->data=num; t->f1=st; st->b1=t; t->b1=NULL; st=t; } else { p1=st; while(c<pos) { p4=p1; p1=p1->f1; c++; } t=new node; t->data=num; t->f1=p4->f1; t->b1=p4; (p4->f1)->b1=t; p4->f1=t; } r=st; while(r!=NULL) { cout<<r->data<<"<-->"; r=r->f1; } cout<<"NULL"; } void dlist::del(int pos) { cout<<"ENTER THE CHOICE POSITION(1 OR 2 OR 3)"<<endl; cin>>pos; switch(pos) { case 1: { p2=st; st=p2->f1; delete p2; break; } case 2: { if(p2->f1==NULL) { p2=p2->b1; p2->f1=NULL; } break; } case 3: { (t->f1)->b1=t->b1; (t->b1)->f1=t->f1; delete t; break; } } } void dlist:: prlis() {

u=st; while(u!=NULL) { cout<<u->data<<"<-->"; u=u->f1; } cout<<"NULL"; } void main() { dlist d; int x,x1,po,pos; char s='y'; char x2; clrscr(); while(s=='y') { cout<<endl<<"ENTER THE DATA TO BE ADDED"<<endl; cin>>x; d.create(x); cout<<"DO YOU WANT TO CONTINUE"<<endl; cin>>s; } d.prlis(); cout<<endl<<"DO YOU WANT TO INSERT OR DELETE"<<endl; cin>>x2; if(x2=='i') { cout<<"ENTER THE POSITION"<<endl; cin>>po; cout<<"ENTER THE ELEMENT TO BE INSERTED"<<endl; cin>>x1; d.insert(x1,po); } else { d.del(pos); d.prlis(); } getch(); } OUTPUT : ENTER THE DATA TO BE ADDED 10 DO YOU WANT TO CONTINUE Y ENTER THE DATA TO BE ADDED 20 DO YOU WANT TO CONTINUE Y ENTER THE DATA TO BE ADDED 30 DO YOU WANT TO CONTINUE N 10 <- - > 20 <- - > 30 <- - > NULL DO YOU WANT TO INSERT OR DELETE I ENTER THE POSITION 3 ENTER THE ELEMENT TO BE INSERTED 25 10 <- - > 20 <- - > 25 <- - >30 <- - > NULL DO YOU WANT TO INSERT OR DELETE D ENTER THE CHOICE POSITION ( 1 OR 2 OR 3 ) 1 20 <- - > 25 <- - >30 <- - > NULL

RESULT : Thus the program doubly linked list was implemented and executed successfully. CIRCULAR LINKED LIST AIM: To implement a circular linked list and to perform basic operations like insertion and deletion. ALGORITHM: 1) Declare a node with one element field and pointer field (previous and next). 2) Create required number of nodes. 3) Make the last node to point to first node and first node to point to the last node. 4) No NULL pointer is assigned. 5) To insert an element a) Get the position of insertion. b) Create a new node. c) Insert the elements into the new nodes element field. d) Make the assignment of pointers respectively. 6) To delete an element a) Get the position of deletion. b) Change the pointer assignment. c) Delete the free node. /*CIRCULAR LINKED LIST USING POINTERS*/ #include<iostream.h> #include<conio.h> struct s { int data; struct s *next,*prev; }node; class link { struct s *p,*head,*temp,*node,*x; public: void create(); void dis(); void insert(); void del(); }; void link::create() { p=new s; head=p; cout<<"ENTER ELEMENTS OF THE LIST"<<endl; cin>>p->data; while(p->data!=0) { x=p; p->next=new s; p=p->next; p->prev=x; cin>>p->data; } x->next=head; head->prev=x; } void link::insert() { int item; node=new s; cout<<endl<<"ENTER AFTER WHICH ELEMENT YOU WANT TO INSERT "<<endl; cin>>item; cout<<"ENTER THE ELEMENT TO BE INSERTED"<<endl; cin>>node->data; p=head; while(p->data!=item && p->next!=head)

{ p=p->next; } x=p->next; node->next=p->next; node->prev=p; x->prev=node; p->next=node; } void link::del() { cout<<endl<<"DELETION"<<"\n\n"; int item; int choice; cout<<"ENTER THE CHOICE(1 OR 2)"<<endl; cin>>choice; cout<<"ENTER THE ELEMENT TO BE DELETED"<<endl; cin>>item; p=head; temp=head; switch(choice) { case 1: { while(p->next!=head) { p=p->next; } x=head->next; p->next=x; x->prev=p; head=x; delete temp; break; } case 2: { while(p->data!=item && p->next!=head) { x=p; p=p->next; } temp=p; x->next=p->next; p->next->prev=x; break; } } } void link::dis() { cout<<endl<<"THE LIST AFTER PROCESSING"<<endl; p=head; while(p->next!=head) { cout<<p->data<<"\t"; p=p->next; } cout<<p->data<<"\t"; } void main() { link a; clrscr(); a.create(); a.dis(); a.insert(); a.dis();

a.del(); a.dis(); getch(); }

OUTPUT : ENTER ELEMENTS OF THE LIST 10 20 30 0 THE LIST AFTER PROCESSING 10 20 30 ENTER AFTER WHICH ELEMENT YOU WANT TO INSERT 20 ENTER THE ELEMENT TO BE INSERTED 60 THE LIST AFTER PROCESSING 10 20 60 30 1 INSERTION 2 DELETION ENTER THE CHOICE ( 1 OR 2 ) 2 ENTER THE ELEMENT TO BE DELETED 60 THE LIST AFTER PROCESSING 10 20 30 RESULT : Thus the circular linked list was implemented and executed successfully

STACK USING ARRAY AIM: To implement a stack data structure using array. ALGORITHM: 1) Get the size of the stack. 2) Read the choice of operation. 3) If the choice is 1push the items into the stack 4) To push the elements perform the following. a) If the top of stack is equal to size of stack elements cant be pushed. b) Read item to be pushed. c) Set or increment the top by one. d) Assign the item to the stack [top]. e) Repeat the steps until it is required. 5) If choice is 2 pop the items from the stack a) Check for empty stack. b) Decrement the top by one. c) Return the popped item. 6) Display the contents of Stack.

/*STACK USING ARRAY*/ #include<iostream.h> #include<conio.h> const max=5; class stack { int top,a[max]; public: stack(); void push(); void pop(); void display(); }; stack::stack() { top=-1; } void stack::push() { int data; char ch; do { if(top==max-1) { cout<<"stack is full"<<endl; break; } else { top=top+1; cout<<"enter the data"<<endl; cin>>data; a[top]=data; } cout<<"do you want to push anymore"<<endl; cin>>ch; } while(ch=='y'); } void stack::pop() { char ch; do { cout<<"do you want to pop any data"<<endl; cin>>ch; if(top<0) { cout<<"can't pop"<<endl; break; } if(ch=='y') { top=top-1; } } while(ch=='y'); } void stack::display() { int i; cout<<endl<<"the elements of the stack"<<endl; for(i=top;i>=0;i--) { cout<<a[i]<<endl;

} } void main() { stack s; int cho; clrscr(); do { cout<<"1.for push"<<endl; cout<<"2.for pop"<<endl; cout<<"3.for exit"<<endl; cout<<"4.enter the choice"<<endl; cin>>cho; switch(cho) { case 1: s.push(); s.display(); break; case 2: s.pop(); s.display(); break; } } while(cho!=3); getch(); } OUTPUT : 1. FOR PUSH 2. FOR POP 3. FOR EXIT ENTER THE CHOICE 1 ENTER THE DATA 22 DO YOU WANT TO PUSH ANYMORE Y ENTER THE DATA 43 DO YOU WANT TO PUSH ANYMORE N THE ELEMENTS OF THE STACK 43 34 22 1. FOR PUSH 2. FOR POP 3. FOR EXIT ENTER THE CHOICE 2 DO YOU WANT TO POP ANY DATA Y DO YOU WANT TO POP ANY DATA N THE ELEMENTS OF THE STACK 34 22 1. FOR PUSH 2. FOR POP 3. FOR EXIT ENTER THE CHOICE 3 RESULT : Thus the program stack using array was implemented and executed

successfully. STACK USING POINTER AIM: To implement a stack using Pointer. ALGORITHM: 1) Create a node with an implement field and a pointer field 2) Read the choice of operation. 3) If choice is 1 push the elements into the stack 4) To push elements into the stack perform the following. a) Create a temporary node. b) Enter the element into the element of field of temporary node. c) Assign the temporary node as top. d) Continue these steps until required elements are being pushed. 5) To pop the elements perform the following steps. a) Check for empty stack. b) Assign the nodes element part to a variable. c) Pop the element is variable through return statement. d) Continue these steps until the pop operation is required. 6) Display the contents of stack. /*STACK USING POINTER*/ #include<iostream.h> #include<conio.h> class stptr { public: struct node { int ele; struct node *next; }; node *new1,*new2,*top; public: stptr(); void push(int); int pop(); void show(); } stptr::stptr() { top=NULL; } void stptr::push(int item) { new1=new node; new1->ele=item; new1->next=top; top=new1; } int stptr::pop() { int k; k=top->ele; top=top->next; if(top==NULL) { cout<<"stack is empty"<<endl; } return(k); } void stptr::show() { while(top!=NULL) { cout<<"\n"<<top->ele<<"\n"; top=top->next; } }

void main() { stptr s1; int c,r; clrscr(); do { cout<<"\nenter \n1.push\n 2.pop\n 3.display\n 4.exit\n"<<endl; cin>>c; switch(c) { case 1: { cout<<"enter value to be pushed"<<endl; cin>>r; s1.push(r); break; } case 2: { r=s1.pop(); cout<<"popped element"<<endl; cout<<r; break; } case 3: { s1.show(); cout<<"NULL"; break; case 4: break; } } } while(c!=4); getch(); } OUTPUT: Enter 1. push 2. pop 3. display 1 Enter value to be pushed Enter 1. push 2. pop 3. display 1 Enter value to be pushed Enter 1. push 2. pop 3. display 1 Enter value to be pushed Enter 1. push 2. pop 3. display 2 Popped element 40 Enter 1. push 3 30 20 NULL 2. pop 3. display 4. exit

4. exit 20 4. exit 30 4. exit 40 4. exit

Enter 1. push 4

2. pop

3. display 4. exit

RESULT : Thus the program stack using pointer was implemented and executed successfully. QUEUE USING ARRAY AIM: To create and implement queue using array. ALGORITHM: 1. nitialize the queue with rear and front equal to zero. 2. If the count of number of elements in queue is equal to maximum capacity, then queue is full. 3. Else enter the data to be inserted in the queue. 4. Check whether the rear is not equal to maximum capacity of queue. 5. If it is not maximum then increment the count. 6. To delete the element from queue, check for the count. a] If the count value is equal to zero the queue is empty. b] Else display the deleted element. 7. Display the contents of the queue. /* QUEUE USING ARRAY*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> #define max 10 class que { private: int qe[max],rr,frt,data,ct,q,r; public: que() { frt=rr=ct=0; } void add() { if(ct==(max-1)) { cout<<"queue is full"<<endl; } else { cout<<"enter data"<<endl; cin>>data; qe[rr]=data; if(rr!=(max-1)) rr++; ct++; } } void del() { clrscr(); if(ct==0) cout<<"can't delete"<<endl; else { cout<<"element removed:"<<qe[frt]<<endl; cout<<"element count"<<--ct<<endl; if(frt!=(max-1)) frt++; }

} void disp() { clrscr(); q=ct; r=frt; if(q==0) cout<<"can't display"<<endl; else { cout<<"element count"<<ct<<endl; cout<<"element in queue"<<endl; for(q=0;q<ct;q++) { cout<<qe[r]<<"\n"; if(r!=(max-1)) r++; } } } void menu() { cout<<endl<<"menu"; cout<<"\t1.add"<<endl; cout<<"\t2.delete\n"; cout<<"\t3.display\n"; cout<<"\t4.exit\n"; } }; void main() { int c; que o; clrscr(); do { o.menu(); cin>>c; switch(c) { case 1: o.add(); break; case 2: o.del(); break; case 3: o.disp(); break; } } while(c!=4); getch(); }

OUTPUT : menu 1. add 2. delete 1 Enter data 2 menu 1. add 2. delete 1 Enter data 3

3.display

4.exit

3.display

4.exit

menu 1. add 2. delete 1 Enter data 4 menu 1. add 2. delete 3 Element count 3 Element in queue 2 3 4 menu 1. add 2. delete 2 Element removed : 2 Element count : 2 menu 1. add 2. delete 3 Element count 2 Element in queue 3 4 menu 1. add 2. delete 4

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

RESULT : Thus the program queue using array was implemented and executed successfully QUEUE USING POINTER AIM: To create and implement queue using POINTER. ALGORITHM: 1. Declare the structure of the queue. 2. To insert an element into the queue a) Enter the data b) If fronts link is equal to null fronts next pointer will point to the new node. c) Else rears next pointer will point to the new node. d) Increment the count of the number of elements of queue. 3. To delete an element from the queue a) If the count is equal to zero the queue will be empty and deletion is not possible. b) Else the element will be removed. c) Count is decremented. 4. Display the contents of queue. /* QUEUE USING POINTER*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> #define NULL 0 class que { private: struct node { int data; struct node *link; }; struct node *q,*rr,*frt; int x,ct; public: que()

{ frt=new node; frt=NULL; rr=frt; } void add() { cout<<"enter data\n"; cin>>x; q=new node; q->data=x; q->link=frt; if(frt->link==NULL) frt->link=q; else rr->link=q; rr=q; ct++; } void del() { clrscr(); if(ct==0) cout<<"can't delete\n"; else { cout<<"element removed"<<frt->link->data<<"\n"; cout<<"element count\n"<<--ct; frt->link=frt->link->link; } } void disp() { clrscr(); q=frt; if(q->link==NULL) cout<<"can't display\n"; else { cout<<"element count\t"<<ct; cout<<"\nelements in queue\n"; for(q=q->link;q->data!=NULL;q=q->link) cout<<q->data<<"\n"; } } void menu() { cout<<"\n menu"; cout<<"\t1.add\n"; cout<<"\t2.delete\n"; cout<<"\t3.display\n"; cout<<"\t4.exit\n"; } }; void main() { int c; que o; clrscr(); do { o.menu(); cin>>c; switch(c) { case 1: o.add(); break;

case 2: o.del(); break; case 3: o.disp(); break; } } while(c!=4); getch(); } OUTPUT : menu 1. add 2. delete 1 Enter data 2 menu 1. add 2. delete 1 Enter data 3 menu 1. add 2. delete 1 Enter data 4 menu 1. add 2. delete 3 Element count 3 Element in queue 2 3 4 menu 1. add 2. delete 2 Element removed : 2 Element count : 2 menu 1. add 2. delete 3 Element count 2 Element in queue 3 4 menu 1. add 2. delete 4

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

RESULT : Thus the program queue using linked list was implemented and executed successfully BINARY SEARCH METHOD AIM: To search an element in an array using binary search algorithm. ALGORITHM: 1) Get the number of elements to be in an array. 2) Read the elements of array. 3) Sort the elements of the array if unsorted. 4) Assign low as zero. 5) Assign high as maximum size minus one. 6) While low is less then or equal to high repeat the steps. a) Assign median as [low + high]/2. b) If element to be searched is equal to the element in an array display that the item is found.

c) Else assign low as zero and high as median-1. Else assign low as median+1. 7) If element is not found display the error message. /*BINARY SEARCH METHOD*/ #include<iostream.h> #include<conio.h> void main() { int a[10],i,j,s=0,n,d=1,t=0; int l,m,h; clrscr(); cout<<"enter the number of elements\n"; cin>>n; cout<<"enter the elements\n"; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } cout<<"the elements are\n"; for(i=0;i<n;i++) { cout<<"\n"<<a[i]; } cout<<"\n enter the elements to search\n"; cin>>s; l=0; h=n-1; while(l<=h) { m=(l+h)/2; if(s==a[m]) { d=0; cout<<"element\t"<<s<<"is present\n"; break; } else if(s<a[m]) { l=0; h=m-1; } else if(s>a[m]) { l=m+1; } } if(d!=0) { cout<<"element not found\n"; } getch(); }

OUTPUT : Enter the number of elements 5 Enter the elements 2 3 4 5 6 The elements are 2 3 4 5 6 Enter the element to search 4 Element 4 is present Enter the element to search 1 Element 1 is not present RESULT : Thus the program binary search method was developed and executed successfully CIRCULAR QUEUE USING ARRAY AIM: To create and implement circular queue using array. ALGORITHM: 1) Initialize the circular queue with rear and front equal to zero. 2) If the count of number of elements in queue is equal to maximum capacity the queue is full. 3) Else enter the data to be inserted in the queue. 4) Check whether the rear is not equal to maximum capacity of queue. 5) Increment the count. 6) To delete the element from queue, check for the count. a) If count is equal to zero the queue is empty. b) Else display the deleted element. 7) Display the contents of the queue. /*CIRCULAR QUEUE USING ARRAY*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> #define max 7 class cir { private: int cq[max],rr,frt,data,ct,q,r; public: cir() { frt=rr=ct=0; } void add() { if(ct==(max-1)) { cout<<"queue is full"<<endl; }

else { cout<<"enter data"<<endl; cin>>data; cq[rr]=data; if(rr!=(max-1)) rr++; else rr=0; ct++; } } void del() { clrscr(); if(ct==0) cout<<"can't delete"<<endl; else { cout<<"element removed:"<<cq[frt]<<endl; cout<<"element count"<<--ct<<endl; if(frt!=(max-1)) frt++; else frt=0; } } void disp() { clrscr(); q=ct; r=frt; if(q==0) cout<<"can't display"<<endl; else { cout<<"element count"<<ct<<endl; cout<<"element in queue"<<endl; for(q=0;q<ct;q++) { cout<<cq[r]<<"\n"; if(r!=(max-1)) r++; else r=0; } } } void menu() { cout<<endl<<"menu"; cout<<"\t1.add"<<endl; cout<<"\t2.delete\n"; cout<<"\t3.display\n"; cout<<"\t4.exit\n"; } }; void main() { int c; cir o; clrscr(); do { o.menu(); cin>>c; switch(c) {

case 1: o.add(); break; case 2: o.del(); break; case 3: o.disp(); break; } } while(c!=4); getch(); } OUTPUT : menu 1. add 2. delete 1 Enter data 2 menu 1. add 2. delete 1 Enter data 3 menu 1. add 2. delete 1 Enter data 4 menu 1. add 2. delete 3 The elements are 2 3 4 menu 1. add 2. delete 2 Element removed : 2 Element count : 2 menu 1. add 2. delete 3 The elements are 3 4 RESULT : Thus the program circular queue using array was implemented and executed successfully

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

CIRCULAR QUEUE USING LINKED LIST AIM: To create and implement circular queue using linked list. ALGORITHM: 1) Declare the structure of the circular queue. 2) To insert an element into the circular queue. a) Enter the data. b) If fronts links is equal to NULL fronts next pointer will point to the new node c) Else rears next pointer will point to the new node. d) Increment the count of the number of elements of circular queue. 3) To delete an element from the circular queue. a) If the count is equal to zero the queue will be empty and an element cant be deleted b) Or otherwise the element will be removed.

c) Count is decremented. 4) Display the contents of circular queue. /*CIRCULAR QUEUE USING POINTER*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> #define NULL 0 class cirque { private: struct node { int data; struct node *link; }; struct node *q,*rr,*frt; int x,ct; public: cirque() { frt=new node; frt=NULL; rr=frt; } void add() { cout<<"enter data\n"; cin>>x; q=new node; q->data=x; q->link=frt; if(frt->link==NULL) frt->link=q; else rr->link=q; rr=q; ct++; } void del() { clrscr(); if(ct==0) cout<<"can't delete\n"; else { cout<<"element removed"<<frt->link->data<<"\n"; cout<<"element count\n"<<--ct; frt->link=frt->link->link; } } void disp() { clrscr(); q=frt; if(q->link==NULL) cout<<"can't display\n"; else { cout<<"element count\t"<<ct; cout<<"\nelements in queue\n"; for(q=q->link;q->data!=NULL;q=q->link) cout<<q->data<<"\n"; } } void menu() {

cout<<"\n menu"; cout<<"\t1.add\n"; cout<<"\t2.delete\n"; cout<<"\t3.display\n"; cout<<"\t4.exit\n"; } }; void main() { int c; cirque o; clrscr(); do { o.menu(); cin>>c; switch(c) { case 1: o.add(); break; case 2: o.del(); break; case 3: o.disp(); break; } } while(c!=4); getch(); } OUTPUT : menu 1. add 2. delete 1 Enter data 2 menu 1. add 2. delete 1 Enter data 3 menu 1. add 2. delete 1 Enter data 4 menu 1. add 2. delete 3 The elements are 2 3 4 menu 1. add 2. delete 2 Element removed : 2 Element count : 2 menu 1. add 2. delete 3 The elements are 3 4 RESULT : Thus the program circular queue using linked list was implemented and executed successfully

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

3.display

4.exit

QUICK SORT AIM: To sort an unsorted array using quick sort algorithm. ALGORITHM: 1) Read the number of element to be in an array. 2) Read the input array. 3) Find the median value by either of three steps. a) Test left and middle elements. b) Test left and right elements. c) Test middle and right elements. 4) Find correct position of pivot element by using median value. 5) Examine median left and more right looking for an element that belongs on the right of pivot 6) Examine median right and mire left liking for an element that belongs on the left of pivot 7) When found exchange two elements. 8) Repeat steps (5), (6), (7) recursively until all elements are sorted. 9) Display the sorted array elements. /*QUICK SORT*/ #include<iostream.h> #include<conio.h> class quick { public: int i,j,k,p,temp,n,n1,a[100]; void getdata(int); void sort(int,int); void dis(int); }; void quick::getdata(int n) { cout<<"Enter the elements\n"; for(k=0;k<n;k++) { cin>>a[k]; } } void quick::sort(int n,int m) { if(m>n) { i=n; j=m; p=a[n]; n1=0; while(!n1) { do { i++; } while((a[i]<=p)&&(i<=m)); while((a[j]>=p)&&(j>n)) { j--; } if(j<i) { n1=1; } else { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[n];

a[n]=a[j]; a[j]=temp; sort(n,j-1); sort(i,m); } } void quick::dis(int n) { cout<<"The sorted list\n"; cout<<"----------------\n"; for(i=0;i<n;i++) { cout<<endl<<a[i]; } } void main() { quick q; int n,l,r; clrscr(); cout<<"Enter the no of elements\n"; cout<<"------------------------\n"; cin>>n; l=0; r=n-1; q.getdata(n); q.sort(l,r); q.dis(n); getch(); } OUTPUT : Enter the no of elements 5 Enter the elements 43 23 56 2 67 The sorted list 2 23 43 56 67 RESULT : Thus the quick sort was developed and executed successfully HEAP SORT AIM: To sort an unsorted array element using heap sort algorithm. ALGORITHM: 1) Enter the number of element in an array. 2) Read the array elements. 3) Repeat the following steps. a) Select an element that is largest in the unsorted array. b) Replace the last element with the largest element. c) Increment the walker value. d) Continue the process a), b), c) until all the elements are sorted. 4) Display the sorted list of array elements. /*HEAP SORT*/ #include<iostream.h> #include<conio.h> class heap

{ private: int a[100],i,temp; public: void getdata(int); void dis(int); void sort(int); }; void heap::getdata(int n) { cout<<"Enter the elements to be sorted\n"; cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; for(i=1;i<=n;i++) { cin>>a[i]; } } void heap::sort(int n) { int p,i,j; i=0; p=1; while(p<=n) { p=(p*3)+1; } do { p=p/3; i=p+1; while(i<=n) { temp=a[i]; j=i; while(a[j-p]>temp) { a[j]=a[j-p]; j=j-p; if(j<=p) { break; } } a[j]=temp; i++; } } while(p!=1); } void heap::dis(int n) { cout<<"The sorted list\n"; cout<<"~~~~~~~~~~~~~~~\n"; for(i=1;i<=n;i++) { cout<<endl<<a[i]; } } void main() { heap h; clrscr(); int n; cout<<"Enter the no of elements\n"; cout<<"~~~~~~~~~~~~~~~~~~~~~~~~\n"; cin>>n; h.getdata(n); h.sort(n);

h.dis(n); getch(); } OUTPUT : Enter the no of elements 5 Enter the elements 43 23 56 2 67 The sorted list 2 23 43 56 67 RESULT : Thus the heap sort was developed and executed successfully PRIMS ALGORITHM AIM: To obtain a minimum cost spanning tree using prims algorithm. ALGORITHM: 1. Read the number of vertices. 2. Read the input weighted matrix. 3. Get an edge with minimum cost. 4. Using a loop statement add the edges one by one such that i is a vertex already in tree j is a vertex not yet included. 5. Determine the cost of each and every time. 6. At the end of all examination find the minimum cost spanning tree. 7. Print the tree details. /*PRIMS ALGORITHM*/ #include<iostream.h> #include<conio.h> void main() { float lcost[8],a[8][8]; int clost[8],i,j,k,min,n; clrscr(); cout<<"Enter the number of vertices"<<endl; cin>>n; cout<<"Input Weighted matrix"<<endl; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cin>>a[i][j]; } } for(i=2;i<=n;++i) { lcost[i]=a[1][i]; clost[i]=1; } cout<<"Minimum cost spanning tree edges are :"<<endl; for(i=2;i<=n;++i) { min=lcost[2]; k=2; for(j=3;j<=n;++j) if(lcost[j]<min) {

min=lcost[j]; k=j; } cout<<endl<<k<<"-->"<<clost[k]; lcost[k]=200; for(j=2;j<=n;++j) { if((a[k][j] < lcost[j]) && (lcost[k] < 200)) { lcost[j]=a[k][j]; clost[j]=k; } } } getch(); } OUTPUT : Enter the no of vertices : 4 Input weighted matrix : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Minimum cost spanning tree edges are : 2-->1 3-->1 4-->1 RESULT : Thus the program Prims algorithm was developed and the output was verified. BREADTH FIRST SEARCH AIM:To implement the graph using adjacency matrix and the nodes to be visited systematically. ALGORITHM: 1. Construct the adjacency matrix a] it has a value 1 if there exists a direct path. b] it has a value 0 if there is no path. 2. Initialize all the nodes in the graph as unvisited. 3. Start from the first node or any node considering it as source Vertex. 4. Find any adjacent node. 5. Place the node inside the queue. 6. Mark the node as visited. 7. Continue steps (4),(5),(6) until all the nodes are visited. 8. Select the nodes from queue and find adjacent nodes until the Queue is empty. 9. Display the reached vertices in their order being visited. /*BREADTH FIRST SEARCH*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> class que { public: int q[10],size,data; int rear,front,x; que() { rear=front=0; } que insert(int v) { if(rear==size) {

cout<<"can't insert\n"; } else { q[rear]=v; rear++; } return *this; } int d() { if(front==rear) { cout<<"can't delete\n"; } else { x=q[front]; q[front]=0; front++; } return x; } }; class graph { public: int a[100][100],i,j,v,e,reach[100],label; void add(); void del(); void dis(); void bfs(); void menu() { cout<<"1.ADD\n2.DEL\n3.DIS\n4.BFS\n5.EXIT\n"; } graph() { e=0; cout<<"Enter no of vertices\n"; cin>>v; for(i=1;i<=v;i++) for(j=1;j<=v;j++) a[i][j]=0; for(int i=1;i<=v;i++) reach[i]=0; for(i=1;i<=v;i++) for(j=1;j<=v;j++) a[i][j]=0; label=1; } }; void graph::add() { cout<<"Enter the row and column value\n"; cin>>i>>j; if(i<1||j>v+1||i>v+1||j<1||a[i][j]==1) cout<<"Can't insert\n"; else { a[i][j]=1; e++; cout<<"No of edges\t"<<e<<"\n"; } } void graph::del() { cout<<"Enter the row and column value\n";

cin>>i>>j; if(i<1||j>v||i>v||j<1||a[i][j]==0) cout<<"Can't delete\n"; else { a[i][j]=0; e--; cout<<"No of edges\t"<<e<<"\n"; } } void graph::dis() { for(i=1;i<=v;i++) { cout<<endl; for(j=1;j<=v;j++) cout<<a[i][j]<<"\t"; } cout<<endl; } void graph::bfs() { int s; cout<<"Enter the source vertex\n"; cin>>s; reach[s]=label; que q; q.insert(s); while(q.front!=q.rear) { int w; w=q.d(); for(int u=1;u<=v;u++) { if(a[w][u]!=0&&reach[u]!=1) { q.insert(u); reach[u]=label; cout<<"Reached vertices are"<<u<<endl; } } } } void main() { clrscr(); graph o; int c,v; a:o.menu(); cout<<"Enter the choice\n"; cin>>c; switch(c) { case 1: o.add(); goto a; break; case 2: o.del(); goto a; break; case 3: o.dis(); goto a; break; case 4: o.bfs(); goto a;

break; case 5: exit(0); } getch(); } OUTPUT : Enter no of vertices : 4 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 1 4 No of edges 2 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 1 2 No of edges 3 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 2 3 No of edges 4 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 3 4 No of edges 5 1.ADD 2.DEL Enter the choice : 3 0 1 0 0 0 0 0 0 3.DIS 0 1 0 0 1 0 1 0 3.DIS 4.BFS 5.EXIT 4.BFS 5.EXIT

4.BFS

5.EXIT

4.BFS

5.EXIT

4.BFS

5.EXIT

4.BFS

5.EXIT

1.ADD 2.DEL Enter the choice : 4 Enter the source vertex : 1 Reached vertex are 2 Reached vertex are 4 1.ADD 2.DEL Enter the choice : 5

3.DIS

4.BFS

5.EXIT

RESULT : Thus the breadth first search was developed and executed successfully.

DEPTH FIRST SEARCH AIM:To implement the graph using adjacency matrix and to visit all the nodes in the graph systematically using depth first search method. ALGORITHM: 1. Construct the adjacency matrix a] it has a value 1 if there exists a direct path. b] it has a value 0 if there is no path. 2. Initialize all the nodes in the graph as unvisited. 3. Select a node from graph as source vertex. 4. Visit the nearby node and find whether it is marked or unmarked. a] if marked return to the previous node. b] mark the node as visited otherwise. 5. If all the nodes are visited stop marking. 6. Otherwise select another node as source vertex and continue all the steps above. 7. Display all the reached vertices.

/* DFS */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #define label 1 class graph { public: int a[100][100],i,j,v,e,reach[100],*pos; void add(); void del(); void dis(); int next(int); int begin(int); void dfs(int,int[]); void menu() { cout<<"1.ADD\n2.DEL\n3.DIS\n4.DFS\n5.EXIT\n"; } graph() { e=0; cout<<"Enter no of vertices\n"; cin>>v; for(i=1;i<=v;i++) reach[i]=0; for(i=1;i<=v;i++) for(j=1;j<=v;j++) a[i][j]=0; pos=new int[v+1]; } }; void graph::add() { cout<<"Enter the row and column value\n"; cin>>i>>j; if(i<1||j>v+1||i>v+1||j<1||a[i][j]==1) cout<<"Can't insert\n"; else { a[i][j]=1; e++; cout<<"No of edges\t"<<e<<"\n"; } } void graph::del() { cout<<"Enter the row and column value\n"; cin>>i>>j; if(i<1||j>v||i>v||j<1||a[i][j]==0) cout<<"Can't delete\n"; else { a[i][j]=1; e--; cout<<"No of edges\t"<<e<<"\n"; } } void graph::dis() { for(i=1;i<=v;i++) { cout<<endl; for(j=1;j<=v;j++) cout<<a[i][j]<<"\t"; } cout<<endl; }

void graph::dfs(int v,int reach[]) { reach[v]=label; int u=begin(v); if(u!=0) while(u) { if(!reach[u]) cout<<"reached vertices are"<<u<<"\n"; dfs(u,reach); u=next(v); } } int graph::begin(int i) { if(i<1||i>v) cout<<"Invalid operation\n"; else { for(int j=1;j<=v;j++) if (a[i][j] !=0) { pos[i] = j; return j; } pos[i] = v+1; return 0; } } int graph ::next(int i) { if ( 1 || i >v) cout<<Invalid operation\n; else { for (j=pos[i]+1;j<=v;j++) if ( a[i][j] != 0 ) { pos[i] = j; return j; } pos[i] = v+1; return 0; } } Void main() { clrscr(); graph o; int c,v; a:o.menu(); cout<<Enter the choice\n; cin>>c; switch(c) { case 1: o.add( ); goto a; break; case 2: o.del( ); goto a; break; case 3: o.dis( ); goto a; break; case 4:

cout<<Enter source vertex \n; cin>>v; o.dfs(v,o.reach); goto a; break; case 5: exit (0); } getch( ); } OUTPUT : Enter no of vertices : 4 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 1 4 No of edges 2 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 1 2 No of edges 3 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 2 3 No of edges 4 1.ADD 2.DEL 3.DIS Enter the choice : 1 Enter the row and column value 3 4 No of edges 5 1.ADD 2.DEL Enter the choice : 3 0 1 0 0 0 0 0 0 3.DIS 0 1 0 0 1 0 1 0 3.DIS 4.DFS 5.EXIT

4.DFS

5.EXIT

4.DFS

5.EXIT

4.DFS

5.EXIT

4.DFS

5.EXIT

4.DFS

5.EXIT

1.ADD 2.DEL Enter the choice : 4 Enter the source vertex : 1 Reached vertex are 2 Reached vertex are 3 Reached vertex are 4 1.ADD 2.DEL Enter the choice : 5

3.DIS

4.DFS

5.EXIT

RESULT : Thus the depth first search was developed and executed successfully. DICTIONARY AIM:To implement dictionary using file concept. ALGORITHM: 1. Declare the structure for the dictionary. 2. Open a file and enter the word and its corresponding meaning. 3. Continue adding words in to the file until it is needed. 4. Search the word in the file. a] if the word being searched is present it is displayed with its corresponding meaning. b] if the word being searched is not present in the file, append the word into the file with its meaning. 5. Sort the meaning in the file in a lexicographic order.

/* Dictionary*/ #include<fstream.h> #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> #include<stdlib.h> class words { private: struct dn { char wd[15]; char mean[100]; }a,b; public: void getdata() { cout<<"Enter word:"; cin>>a.wd; cout<<"Enter meaning:"<<endl; gets(a.mean); } char *st() { return(a.mean); } char *ss() { return(a.wd); } void menu() { cout<<endl<<" MENU"; cout<<endl<<" 1.ADD WORDS"; cout<<endl<<" 2.DISPLAY ALLWORDS"; cout<<endl<<" 3.SEARCH WORD"; cout<<endl<<" 4.SORT WORD FILE"; cout<<endl<<" 5.EXIT"; cout<<endl<<" Enter the choice:"; } }; void main() { clrscr(); char ch,ch1[15],ch2[15],tw[15],tm[15]; words wd1,wd2,t; int l,i,ln,n,x=0,ct,ct1; char s[10][15],m[10][100]; const int wx=15; const int mx=100; char bw[wx],bm[mx]; do { wd1.menu(); cin>>l; fstream file; file.open("dy.txt",ios::app|ios::out|ios::in); switch(l) { case 1: do { wd1.getdata(); file<<wd1.ss()<<endl<<wd1.st()<<endl; cout<<"Add more words (y/n):"; cin>>ch; }

while(ch=='y'||ch=='Y'); break; case 2: while(file.eof()==0) { file.getline(bw,15); cout<<endl<<bw; file.getline(bm,100); cout<<"\t"<<bm; } break; case 3: cout<<"Enter the word to be searched:"; cin>>ch1; while(file.eof()==0) { file.getline(bw,15); file.getline(bm,100); i=strcmp(ch1,bw); if(i==0) { cout<<"WORD PRESENT"<<endl; cout<<bw<<"\t"<<bm; goto x2; } } clrscr(); cout<<"WORD NOT PRESENT"<<endl; cout<<"Choose 1 from menu and append the word"<<endl; x2:break; case 4: int j; for(i=0;file.eof()==0;i++) { file.getline(bw,15); file.getline(bm,100); strcpy(s[i],bw); strcpy(m[i],bm); cout<<endl<<s[i]<<endl<<m[i]<<endl; } n=i-1; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(strcmp(s[i],s[j])>0) { strcpy(tw,s[i]); strcpy(s[i],s[j]); strcpy(s[j],tw); strcpy(tm,m[i]); strcpy(m[i],m[j]); strcpy(m[j],tm); } ofstream d("dy.txt"); for(i=0;i<n;i++) d<<s[i]<<endl<<m[i]<<endl; d.close(); ifstream d1("dy.txt"); clrscr(); cout<<"Sorted file"<<endl; while(d1.eof()==0) { d1.getline(bw,15); cout<<endl<<bw; d1.getline(bm,100); cout<<endl<<bm; } d1.close(); break;

case 5: exit(0); break; } } while(l!=5); } OUTPUT : Menu 1.ADD WORDS 2.DISPLAY WORDS 3.SEARCH WORD 4.SORT WORD FILE 5.EXIT Enter the choice: 1 Enter word : seek Enter meaning : find Add more words ( y / n ) : y Enter word : clams Enter meaning : seafood Add more words ( y / n ) : n Menu 1.ADD WORDS 2.DISPLAY WORDS 3.SEARCH WORD 4.SORT WORD FILE 5.EXIT Enter the choice: 2 seek find clams seafood Menu 1.ADD WORDS 2.DISPLAY WORDS 3.SEARCH WORD 4.SORT WORD FILE 5.EXIT Enter the choice: 3 Enter the word to be searched : chat WORD NOT PRESENT Choose 1 from menu and append the word Menu 1.ADD WORDS 2.DISPLAY WORDS 3.SEARCH WORD 4.SORT WORD FILE 5.EXIT Enter the choice: 3 Enter the word to be searched : seek WORD PRESENT seek find

Menu 1.ADD WORDS

2.DISPLAY WORDS 3.SEARCH WORD 4.SORT WORD FILE 5.EXIT Enter the choice: 4 Sorted file clams seafood seek find Menu 1.ADD WORDS 2.DISPLAY WORDS 3.SEARCH WORD 4.SORT WORD FILE 5.EXIT Enter the choice: 5 RESULT : Thus the dictionary was implemented and executed successfully BINARY SEARCH TREE AIM: To create a binary search tree and insert elements, delete the elements and find an element ALGORITHM: 1) Declare the structure for binary search tree. 2) If the element is root, its value. Should be greater than left child and less than the right chills. 3) To insert an element. a) Get the value to be inserted. b) Insert the value in the order of left bottom to the right bottom of the tree. c) Check each element with the root node for each time. d) If value is less than root insert it to the left position. e) If value is greater than root insert it in the right. 4) To perform deletion, a) Get the values to be deleted. b) If it leaf delete it as such. c) If it has only one child swap the pointers and delete the node. d) If it has both left and right child take the min value at he rightmost sub tree and swap it with the node to be deleted. e) Delete the node in right or left is again simple. 5) To search, a) If root is NULL, return NULL. b) If data is less than root search the element in the left sub tree and left tree. c) If data is greater than root search the element in the right sub tree. 6) Display all the elements of the tree. /*BINARY SEARCH TREE*/ #include<iostream.h> #include<conio.h> enum boolean {false=0,true=1}; struct node { int ele; node *left,*right; }*root=NULL; class BST {

node *par,*temp,*temp1; node *newnode(int); public: void insert(int,node*,int); boolean search(int,node*); node *deletemin(node**); void del(int,node**); void display(node*); }; node* BST::newnode(int x) { node *nod=new node; nod->ele=x; nod->left=nod->right=NULL; return(nod); } void BST::insert(int x,node *cur,int pos) { if(cur==NULL) { cur=newnode(x); if(pos==1) par->left=cur; else if(pos==2) par->right=cur; if(root==NULL) root=cur; } else { par=cur; if(x<cur->ele) insert(x,cur->left,1); else if(x>cur->ele) insert(x,cur->right,2); } } boolean BST::search(int x,node *cur) { if(root==NULL) return false; else if(x==cur->ele) return true; else if(x<cur->ele && cur->left!=NULL) return(search(x,cur->left)); else if(x>cur->ele && cur->right!=NULL) return(search(x,cur->right)); return false; } node *BST::deletemin(node **cur) { if((*cur)->left==NULL) { temp=(*cur); (*cur)=(*cur)->right; } else temp=deletemin((&(*cur)->left)); return(temp); } void BST::del(int x,node **cur) { if((*cur)!=NULL) { if(x<(*cur)->ele) del(x,(&(*cur)->left)); else if(x>(*cur)->ele) del(x,(&(*cur)->right));

else { if(((*cur)->left==NULL) && ((*cur)->right==NULL)) { delete((*cur)); (*cur)=NULL; } else if((*cur)->left==NULL) (*cur)=(*cur)->right; else if((*cur)->right==NULL) (*cur)=(*cur)->left; else { temp1=(*cur)->left; (*cur)=deletemin((&(*cur)->right)); (*cur)->left=temp1; } } } } void BST::display(node *cur) { if(cur!=NULL) { display(cur->left); cout<<"\t"<<cur->ele; display(cur->right); } } void main() { int no,x,p; BST bst; clrscr(); do { cout<<endl<<"1.INSERT"; cout<<endl<<"2.DELETE"; cout<<endl<<"3.SEARCH"; cout<<endl<<"4.DISPLAY"; cout<<endl<<"5.EXIT"; cout<<endl<<"Select the option :"; cin>>no; switch(no) { case 1: cout<<endl<<"Enter the no to be inserted : "; cin>>x; bst.insert(x,root,0); break; case 2: cout<<endl<<"Enter the element to be deleted : "; cin>>x; bst.del(x,&root); break; case 3: cout<<endl<<"Enter the element to be searched : "; cin>>x; p=bst.search(x,root); if(p==true) cout<<"\t"<<"Element found in the BST"; else cout<<"\t"<<"Element not found in the BST"; break; case 4:

cout<<endl<<"The element in the list are : "; bst.display(root); } } while(no<5); getch(); } OUTPUT : 1.INSERT 2.DELETE Select the option : 1 Enter the no to be inserted : 2 1.INSERT 2.DELETE Select the option : 1 Enter the no to be inserted : 3 1.INSERT 2.DELETE Select the option : 4 The element in the list are : 3.SEARCH 4.DISPLAY 5.EXIT

3.SEARCH

4.DISPLAY

5.EXIT

3.SEARCH 2 3

4.DISPLAY

5.EXIT

1.INSERT 2.DELETE 3.SEARCH Select the option : 2 Enter the element to be deleted : 3 1.INSERT 2.DELETE Select the option : 4 The element in the list are : 3.SEARCH 2

4.DISPLAY

5.EXIT

4.DISPLAY

5.EXIT

1.INSERT 2.DELETE 3.SEARCH Select the option : 3 Enter the element to be searched : 2 Element found in the BST 1.INSERT 2.DELETE 3.SEARCH Select the option : 3 Enter the element to be searched : 5 Element not found in the BST 1.INSERT 2.DELETE Select the option : 5 3.SEARCH

4.DISPLAY

5.EXIT

4.DISPLAY

5.EXIT

4.DISPLAY

5.EXIT

RESULT : Thus the binary search tree was developed and executed successfully. AVL TREE AIM: To implement height balance binary search tree or AVL tree using linked list method. ALGOROTHM: 1) Declare the structure of binary search tree. 2) Insert the element into the tree. 3) Check whether the tree is empty. a) Check whether the data is less than root data thin insert it to left. b) Otherwise insert it to right. 4) To insert another element check the balance factor (-1, 0, 1) of the tree. a) If satisfied insert as it is done previously in (3). b) If element is inserted into the left child of left sub tree of a node perform single rotation with left. c) If element is inserted into the right child of right sub tree of a node perform single rotation with right. d) If element is inserted into right child of left sub tree of a node perform double rotation with left.

e) If element is inserted into the left child of right sub tree of a node perform double rotation with right. f) Compute the balance factor along the ancestor path. 5) Display the resultant height balanced tree. /*AVL TREE*/ #include<iostream.h> #include<stdlib.h> #include<conio.h> #define FALSE 0 #define TRUE 1 struct AVLnode { int data; int balfact; AVLnode*left; AVLnode*right; }; class avltree { private: AVLnode*root; public: avltree(); AVLnode*insert(int data,int*h); static AVLnode*buildtree(AVLnode*root,int data,int*h); void dis(AVLnode*root); AVLnode*deldata(AVLnode*root,int data,int*h); static AVLnode*del(AVLnode*node,AVLnode*root,int*h); static AVLnode*balright(AVLnode*root,int*h); static AVLnode*balleft(AVLnode*root,int*h); void setroot(AVLnode*avl); ~avltree(); static void deltree(AVLnode*root); }; avltree::avltree() { root=NULL; } AVLnode*avltree::insert(int data,int*h) { root=buildtree(root,data,h); return root; } AVLnode*avltree::buildtree(AVLnode*root,int data,int*h) { AVLnode*node1,*node2; if(root==NULL) { root=new AVLnode; root->data=data; root->left=NULL; root->right=NULL; root->balfact=0; *h=TRUE; return(root); } if(data<root->data) { root->left=buildtree(root->left,data,h); if(*h) { switch(root->balfact) { case 1: node1=root->left; if(node1->balfact==1) {

cout<<"\nRight rotation"; root->left=node1->right; node1->right=root; root->balfact=0; root=node1; } else { cout<<"\nDouble rotation,left then right"; node2=node1->right; node1->right=node2->left; node2->left=node1; root->left=node2->right; node2->right=root; if(node2->balfact==1) root->balfact=-1; else root->balfact=0; if(node2->balfact==-1) node1->balfact=1; else node1->balfact=0; root=node2; } root->balfact=0; *h=FALSE; break; case 0: root->balfact=1; break; case -1: root->balfact=0; *h=FALSE; } } } if(data>root->data) { root->right=buildtree(root->right,data,h); if(*h) { switch(root->balfact) { case 1: root->balfact=0; *h=FALSE; break; case 0: root->balfact=-1; break; case -1: node1=root->right; if(node1->balfact==-1) { cout<<"\nLeft rotation"; root->right=node1->left; node1->left=root; root->balfact=0; root=node1; } else { cout<<"\nDouble rotation, right then left"; node2=node1->left; node1->left=node2->right; node2->right=node1; root->right=node2->left; node2->left=root;

if(node2->balfact==-1) root->balfact=1; else root->balfact=0; if(node2->balfact==1) node1->balfact=-1; else node1->balfact=0; root=node2; } root->balfact=0; *h=FALSE; } } } return(root); } void avltree::dis(AVLnode*root) { if(root!=NULL) { dis(root->left); cout<<root->data<<"\t"; dis(root->right); } } AVLnode*avltree::deldata(AVLnode*root,int data,int*h) { AVLnode*node; if(root->data==13) cout<<root->data; if(root==NULL) { cout<<"\nNo such data"; return (root); } else { if(data<root->data) { root->left=deldata(root->left,data,h); if(*h) root=balright(root,h); } else { if(data>root->data) { root->right=deldata(root->right,data,h); if(*h) root=balleft(root,h); } else { node=root; if(node->right==NULL) { root=node->left; *h=TRUE; delete(node); } else { if(node->left==NULL) { root=node->right; *h=TRUE; delete(node);

} else { node->right=del(node->right,node,h); if(*h) root=balleft(root,h); } } } } } return(root); } AVLnode*avltree::del(AVLnode*succ,AVLnode*node,int*h) { AVLnode*temp=succ; if(succ->left!=NULL) { succ->left=del(succ->left,node,h); if(*h) succ=balright(succ,h); } else { temp=succ; node->data=succ->data; succ=succ->right; delete(temp); *h=TRUE; } return(succ); } AVLnode*avltree::balright(AVLnode*root,int*h) { AVLnode*temp1,*temp2; switch(root->balfact) { case 1: root->balfact=0; break; case 0: root->balfact=-1; *h=FALSE; break; case -1: temp1=root->right; if(temp1->balfact<=0) { cout<<"\nLeft rotation"; root->right=temp1->left; temp1->left=root; if(temp1->balfact==0) { root->balfact=-1; temp1->balfact=1; *h=FALSE; } else { root->balfact=temp1->balfact=0; } root=temp1; } else { cout<<"\nDouble rotation,right then left"; temp2=temp1->left; temp1->left=temp2->right; temp2->right=temp1;

root->right=temp2->left; temp2->left=root; if(temp2->balfact==-1) root->balfact=1; else root->balfact=0; if(temp2->balfact==1) temp1->balfact=-1; else temp1->balfact=0; root=temp2; temp2->balfact=0; } } return(root); } AVLnode*avltree::balleft(AVLnode*root,int*h) { AVLnode*temp1,*temp2; switch(root->balfact) { case -1: root->balfact=0; break; case 0: root->balfact=1; *h=FALSE; break; case 1: temp1=root->left; if(temp1->balfact>=0) { cout<<"\nright rotation"; root->left=temp1->right; temp1->right=root; if(temp1->balfact==0) { root->balfact=1; temp1->balfact=-1; *h=FALSE; } else { root->balfact=temp1->balfact=0; } root=temp1; } else { cout<<"\nDouble rotation,left then right"; temp2=temp1->right; temp1->right=temp2->left; temp2->left=temp1; root->left=temp2->right; temp2->right=root; if(temp2->balfact==1) root->balfact=-1; else root->balfact=0; if(temp2->balfact==-1) temp1->balfact=1; else temp1->balfact=0; root=temp2; temp2->balfact=0; } } return(root);

} void avltree::setroot(AVLnode*avl) { root=avl; } avltree::~avltree() { deltree(root); } void avltree::deltree(AVLnode*root) { if(root!=NULL) { deltree(root->left); deltree(root->right); } delete(root); } void main() { clrscr(); avltree at; AVLnode*avl=NULL; int h,n,a,d; cout<<"\nEnter the no. of nodes:"; cin>>n; for(int i=0;i<n;i++) { cout<<"\nEnter the node value :"; cin>>a; avl=at.insert(a,h); at.setroot(avl); } cout<<"\nAVLtree:\n"; at.dis(avl); cout<<"\nEnter the node to be deleted :"; cin>>d; avl=at.deldata(avl,d,&h); at.setroot(avl); cout<<"\nAvltree after deletion of a node:\n"; at.dis(avl); getch(); } OUTPUT : Enter the no of nodes Enter the node value Enter the node value Enter the node value Left rotation Enter the node value Enter the node value AVL tree : 10 20 25 30 35 Enter the node to be deleted : 25 AVL tree after deletion of a node : 10 20 30 35 : 25 : 35 :5 : 10 : 20 : 30

RESULT : Thus the AVL tree was implemented and executed successfully

DIJKSTRAS ALGORITHM AIM: To find the shortest path from one node to another node using single source shortest path algorithm. ALGORITHM: 1) 2) 3) 4) 5) 6) 7) 8) Get the number of node of the graph. Construct adjacency matrix. Choose one vertex as source. Choose another as destination. Calculate the minimum cost by using Dijkstras algorithm. Display minimum cost. Display the shortest path. Display the path with weights.

/*DIJKSTRA'S ALGORITHM*/ #include<iostream.h> #include<conio.h> #define MAX 50 struct node { int vertex,weight; node *next; }; struct fringe_node { int vertex; fringe_node *next; }; node *adj[MAX]; int totnodes; const int UNSEEN=1, FRINGE=2, INTREE=3; int status[MAX]; fringe_node *fringe_list; void create() { node *new1,*last; int adj1; cout<<"Graph creation"<<endl; cout<<"Enter total nodes in graph"<<endl; cin>>totnodes; for(int i=1;i<totnodes;i++) { last=NULL; cout<<"Total neighbours of "<<i<<endl; cin>>adj1; for(int j=1;j<=adj1;j++) { new1=new node; cout<<"Neighbour "<<j; cin>>new1->vertex; cout<<"Weight"<<endl; cin>>new1->weight; new1->next=NULL; if(adj[i]==NULL) adj[i]=last=new1; else { last->next=new1; last=new1; } } } } void insertbeg(int item) { fringe_node *new1; new1=new fringe_node;

new1->vertex=item; new1->next=NULL; new1->next=fringe_list; fringe_list=new1; } void del(int pos) { int i; fringe_node *tmp,*delnode; for(i=1,tmp=fringe_list;i<(pos-1);tmp=tmp->next,i++) delnode=tmp->next; tmp->next=tmp->next->next; delete(delnode); } void print_path(int s, int d,int par[]) { if(d==s) cout<<" "<<s; else { print_path(s,par[d],par); cout<<"--"<<d; } } void shortpath() { int i,x,par[MAX],edge,ct,w,wt,v; int min_dis,y,dis[MAX],stuck; int so,des; node *ptr1; fringe_node *ptr2; cout<<"Enter Source node :"; cin>>so; cout<<"Enter Destination node :"; cin>>des; fringe_list=NULL; for(i=1;i<totnodes;i++) { status[i]=UNSEEN; par[i]=0; } status[so]=INTREE; dis[so]=0; x=so; stuck=0; while((x!=des)&&(!stuck)) { ptr1=adj[x]; while(ptr1!=NULL) { y=ptr1->vertex; wt=ptr1->weight; if((status[y]==FRINGE)&&(dis[x]+wt<dis[y])) { par[y]=x; dis[y]=dis[y]+wt; } else if(status[y]==UNSEEN) { status[y]=FRINGE; par[y]=x; dis[y]=dis[y]+wt; insertbeg(y); } ptr1=ptr1->next; } if(fringe_list==NULL) stuck=1;

else { x=fringe_list->vertex; min_dis=dis[x]; ptr2=fringe_list->next; while(ptr2!=NULL) { w=ptr2->vertex; if(dis[w]<min_dis) { x=w; min_dis=dis[w]; } ptr2=ptr2->next; } del(x); status[x]=INTREE; } } if(par[des]==0) cout<<"NO path from "<<so<<" to "<<des; else { cout<<"Shortest path"<<endl; print_path(so,des,par); } } void main() { clrscr(); create(); shortpath(); getch(); } OUTPUT : Graph creation Enter total nodes in graph 3 Total neighbors of 1 : 2 Neighbor 1 : 2 Weight 10 Neighbor 2 : 3 Weight 15 Total neighbors of 2 : 1 Neighbor 1 : 4 Weight 35 Enter source node : 1 Enter destination node : 2 Shortest path 1-->2 Graph creation Enter total nodes in graph 3 Total neighbors of 1 : 1 Neighbor 1 : 2 Weight 10 Total neighbors of 2 :3 Neighbor 1 : 3 Weight 14 Neighbor 2 : 4 Weight 25 Neighbor 3 : 7 Weight 2 Enter source node : 2 Enter destination node : 7 No path from 2 to 7 RESULT : Thus Dijkstras algorithm was developed and executed successfully.

You might also like