Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 91

Object Oriented Programming and Data Structures Lab

IMPLEMENTATION OF A LINKED LIST


ALGORITHM
STEP 01: START LIST OPERATIONS. STEP 02: READ CHOICE. STEP 03: IF CHOICE IS EQUAL TO CREATE THEN 1) INPUT DATA TO BE INSERTED. 2) CREATE A NEW NODE. 3) NEW NODE DATA =DATA. 4) NEW NODE->LINK=NULL. 5) IF(START EQUAL TO NULL) i) ii) 6) ELSE i) P->LINK=NEW NODE. P=NEW NODE. START=NEW NODE. P=START.

ii)

STEP 04: IF CHOICE IS EQUAL TO SHOW THEN 1) IF(START IS EQUAL TO NULL) i) ii) DISPLAY THE LIST IS EMPTY. EXIT.

2) INITIALISE TEMP=START. 3) REPEAT STEP 4 AND 5 UNTIL TEMP=NULL. 4) DISPLAY TEMP DATA. 5) TEMP=TEMPNEXT. 6) EXIT. STEAP 05: IF CHOICE IS EQUAL TO INSERT THEN 1) INPUT DATA AND POS TO BE INSERTED.

2) CREATE A NEW NODE.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

3) NEW NODE DATA =DATA. 4) NEW NODE->LINK=NULL. 5) IF POS=1 THEN i) ii) 6) ELSE i) ii) iii) iv) v) TEMP=START AND I=1. REPEAT STEP iii UNTI I< (P0S-1). TEMP=TEMPLINK. NEW NODELINK=TEMPLINK. TEMPLINK=NEW NODE. NEW NODE->LINK=START. START=NEW NODE.

STEP 06: IF CHOICE IS EQUAL TO DELETE THEN 1) INPUT DATA TO BE DELETED. 2) IF(STARTDATA IS EQUAL TO DATA) i) ii) iii) TEMP=START. START=START->LINK. MAKE THE MEMORY ALLOCATED TO TEMP IS FREE. 3) ELSE i) ii) iii) iv) v) vi) vii) PR=P=START. REPEAT STEP 5 TO STEP 6 UNTIL P IS NOT NULL PR=P P=PLINK. PRLINK=P->LINK. PLINK=NULL MAKE THE MEMORY ALLOCATED TO P IS FREE

STEP 07: STOP LIST OPERATIONS.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/*Program to implement a linked list*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> class link { struct node { int data; struct node *link; }*start,*temp,*p,*q,*p1,*p2,*p3; public: link() { start=NULL; } void create() { temp=(struct node*)new(node); cout<<"Enter data :"<<endl; cin>>temp->data; temp->link=NULL; if(start==NULL) { start=temp; p=temp; } else { p->link=temp; p=temp; } }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

void show() { temp=start; cout<<"List contains:"<<"\n"; while(temp!=NULL) { cout<<temp->data<<"\t"; temp=temp->link; } } void insert() { int pos; cout<<"Enter position:"; cin>>pos; temp=new node; cout<<"enter data :"<<endl; cin>>temp->data; temp->link=NULL; if(pos==1) { temp->link=start; start=temp; } else { p=start; int cnt=1; while(cnt<pos-1) { p=p->link; cnt++; } temp->link=p->link; p->link=temp; } } Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

void del() { int f=0; if(start==NULL) { cout<<"List Empty:"; return; } int data1; cout<<"Enter data to deleted:"<<endl; cin>>data1; if(start->data==data1) { f=1; temp=start; start=start->link; temp->link=NULL; delete(temp); } else { temp=start; while(temp!=NULL) { if(temp->data==data1) { p->link=temp->link; temp->link=NULL; delete(temp); f=1; } p=temp; temp=temp->link; } }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

if(!f) cout<<"No Data Present"; } }; void main() { link l; int ch; clrscr(); while(1) { cout<<"1.create"; cout<<"\n2.display"; cout<<"\n3.Count"<<endl; cout<<"4.Insert"<<endl; cout<<"5.Delete"<<endl; cout<<"6.Exit"<<endl; cout<<"enter your choice:"<<endl; cin>> ch; switch(ch) { case 1: l.create(); break; case 2: l.show(); break; case 3: l.count(); break; case 4: l.insert(); break; case 5: l.del(); break;

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

case 6: exit(0); } } getch(); }

OUTPUT
1. create 2. display 3. Insert 4. Delete 5. Exit Enter your choice: 1 Enter data: 27 1. create 2. display 3. Insert 4. Delete 5. Exit Enter your choice: 1 Enter data: 25 1. create 2. display 3. Insert 4. Delete 5. Exit Enter your choice: 2 List contains: 27 25

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

1. create 2. display 3. Insert 4. Delete 5. Exit Enter your choice: 3 Enter position: 2 Enter data: 16 1. create 2. display 3. Insert 4. Delete 5. Exit Enter your choice: 2 List contains: 27 16 25 1. create 2. display 3. Insert 4. Delete 5. Exit Enter your choice: 4 Enter data to deleted: 25 1. create 2. display 3. Insert 4. Delete 5. Exit Enter your choice: 2 List contains: 27 16

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

IMPLEMENTATION OF STACK USING LINKED LIST


ALGORITHM
STEP 01: START STACK OPERATIONS. STEP 02: READ CHOICE. STEP 03: IF CHOICE IS EQUAL TO PUSH THEN 1) INPUT DATA TO BE PUSHED. 2) CREATE A NEW NODE. 3) NEW NODEDATA=DATA. 4) NEW NODELINK=TOP. 5) TOP=NEW NODE 6) EXIT. STEP 04: IF CHOICE IS EQUAL TO POP THEN 1) IF (TOP IS EQUAL TO NULL) i) 2) ELSE i) ii) iii) iv) v) TEMP=TOP. DISPLAY THE POPPRD ELEMENT IS TOPDATA. TOP=TOPLINK. TEMPLINK=NULL. MAKE THE MEMORY ALLOCATED TO TEMP IS FREE. STEP 05: STOP STACK OPERATIONS. DISPLAY THE STACK IS EMPTY.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM /*Implementation of stack using linked list*/


#include<iostream.h> #include<conio.h> #include<stdlib.h> class stack { struct node { int data; struct node *link; }*top,*temp,*p; public: stack() { top=NULL; } void push(); void pop(); void show(); }; void stack::push() { temp=new node; cout<<"Enter data :"; cin>>temp->data; temp->link=NULL; if(top==NULL) { top=temp; } else { temp->link=top; top=temp; } }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

void stack::pop() { if(top==NULL) cout<<"stack is empty"; else { p=top; cout<<"\n Deleted data :"<<top->data; top=top->link; delete p; } } void stack :: show() { p=top; while(p!=NULL) { cout<<p->data<<"\t"; p=p->link; } } void main() { stack s; int ch; clrscr(); while(1) { cout<<"\n1.Push\n2.Pop\n3.Show\n4.Exit\n Enter your choice\n"; cin>>ch; switch(ch) { case 1: s.push(); break; case 2: s.pop(); break; Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

case 3: s.show(); break; case 4: exit(1); } } }

OUTPUT
1. Push 2. Pop 3. Show 4. Exit Enter your choice 1 Enter data: 5 1. Push 2. Pop 3. Show 4. Exit Enter your choice 1 Enter data: 10 1. Push 2. Pop 3. Show 4. Exit Enter your choice 3 5 10 1. Push 2. Pop 3. Show 4. Exit

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

Enter your choice 2 Deleted data 10 1. Push 2. Pop 3. Show 4. Exit Enter your choice 3 5

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

IMPLEMENTATION OF CIRCULAR QUEUE USING ARRAY

ALGORITHM

STEP 01: START CQUEUE OPERATIONS. STEP 02: ACCEPT CHOICE. STEP 03: FRONT=-1. STEP 04: REAR=-1. STEP 04: IF CHOICE IS EQUAL TO INSERT 1) REAR= (REAR+1) %SIZE. 2) IF (FRONT IS EQUAL TO REAR) i) ii) DISPLAY QUEUE IS FULL. EXIT.

3) IF (FRONT IS EQUAL TO -1) i) ii) iii) FRONT=0. REAR=0. EXIT.

4) Q [FRONT] =DATA. STEP 05: REPEAT STEP 02 -04 IF WE WANT TO INSERT MORE ELEMENTS. STEP 06: IF CHOICE IS EQUAL TO DELETE) 1) IF (FRONT IS EUAL TO -1) i) DISPLAY QUEUE IS EMPTY.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

ii) EXIT. 2) ELSE i) DATA=Q [FRONT] 3) IF (REAR IS EQUAL TO FRONT) i) FRONT=-1. ii) REAR=-1. 4) ELSE i) FRONT= (FRONT+1) %SIZE. STEP 07: REPEAT STEP 06 IF WE WANT TO DELETE MORE ELEMENTS. STEP 08: STOP CQUEUE OPERATIONS.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM /* Program to implement a circular queue using array*/ #include<iostream.h>


#include<conio.h> #define N 5 #include<stdlib.h> class queue { int a[N],front,rear; public: queue() { front=-1; rear=-1; } void insert(); void del(); void show(); }; void queue :: insert() { rear=(rear+1)%N; int data; if(rear==front) { cout<<"\nQueue is full:\n"; return; } else { if(front==-1) { front=0; rear=0; } cout<<"Enter data:\n"; cin>>a[rear]; } Page __

Department of computer application, TKM College of Engineering

Object Oriented Programming and Data Structures Lab

void queue::del() { if(front==-1) { cout<<"Queue is empty:\n"; return; } else if(front>rear) { front=rear=-1; cout<<"Queue is empty:\n"; return; } else { cout<<"Deleted data :"<<a[front]; front=(front+1)%N; } } void queue::show() { int i; if(front==-1) cout<<"Queue Empty"; else { cout<<"\n Elements in the queue are:\n"; for(i=front;i<=rear;i++) { cout<<a[i]<<"\t"; } } }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

void main() { queue q; int ch; clrscr(); while(1) { cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter your choice\n"; cin>>ch; switch(ch) { case 1: q.insert(); break; case 2: q.del(); break; case 3: q.show(); break; case 4: exit(1); } } getch(); }

OUTPUT
1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter data: 25

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter data: 40 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 Elements in the queue are: 25 40 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 2 Deleted data: 25 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 Elements in the queue are: 40 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 4

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

IMPLEMENTATION OF A DOUBLY LINKED LIST

ALGORITHM

STEP 01: START DLIST OPERATIONS. STEP 02: ACCEPT CHOICE. STEP 03: IF CHOICE IS EQUAL TO CREATE THEN 1) INPUT DATA TO BE INSERTED. 2) CREATE NEW NODE. 3) NEW NODE->DATA=DATA. 4) NEWNODE->LLINK=NULL 5) NEW NODE->RLINK=NULL. 6) IF(START IS EQUAL TO NULL) i) ii) 7) ELSE i) ii) iii) PRLINK=NEW NODE. NEW NODELLINK=P. P=NEW NODE. START=NEW NODE. P=START.

STEP 04: IF CHOICE IS EQUAL TO INSERT THEN 1) INPUT DATA AND POS TO BE INSERTED. 2) CREATE NEW NODE. 3) NEW NODEDATA=DATA. 4) NEWNODELLINK=NULL 5) NEW NODERLINK=NULL. 6) IF (POS IS EQUAL TO 1) THEN

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

i)

TEMPRLINK=START.

ii) STARTLLINK=TEMP. iii) START=TEMP. 7) ELSE i) I=1, P=START. ii) REPEAT STEP iii UNTIL I<POS-1. iii) P=PRLINK. iv) TEMPLLINK=P. v) TEMPRLINK=PRLINK. vi) PRLINKLLINK=TEMP. vii) PRLINK=TEMP. STEP 05: IF CHOICE IS EQUAL TO DELETE THEN 1) ENTER DATA TO DELETE. 2) IF(STARTDATA IS EQUAL TO DATA) i) TEMP=START. ii) START=TEMPRLINK. iii) STARTLLINK=NULL iv) MAKE THE MEMORY ALLOCATED TO TEMP IS FREE. 3) ELSE i) TEMP=START. ii) REPEAT STEP iii TO STEP iv UNTIL TEMP IS NOT NULL. iii) IF(TEMPDATA IS EQUAL TO DATA) a) TEMPLLINKRLINK=TEMPRLINK. b) TEMPRLINKLLINK=TEMPLLINK. c) FREE THE MEMORY ALLOCATED TO TEMP.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

iv) TEMP=TEMPRLINK. STEP 05: IF CHOICE IS EQUAL TO SHOW THEN 1) TEMP=START. 2) REPEAT STEP 3 TO STEP 4 UNTIL TEMP IS NOT NULL. 3) DISPLAY TEMPDATA. 4) TEMP=TEMPRLINK. STEP O6: STOP DLINK OPERATIONS.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to implement a doubly linked list*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> class list { struct node { int data; struct node *llink,*rlink; }*start,*temp,*p; public: list() { start=NULL; } void create() { temp=new node; cout<<"Enter data:"; cin>>temp->data; temp->llink=temp->rlink=NULL; if(start==NULL) { start=temp; p=temp; } else { p->rlink=temp; temp->llink=p; p=temp; } } void show() { p=start; while(p!=NULL) { cout<<p->data<<" "; p=p->rlink; } } Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

void insert() { temp=new node; int pos; cout<<"\nEnter position to insert :"; cin>>pos; cout<<"\nEnter data :"; cin>>temp->data; temp->llink=temp->rlink=NULL; if(pos==1) { start->llink=temp; temp->rlink=start; start=temp; } else { p=start; for(int i=1;i<pos-1;i++) { p=p->rlink; } temp->rlink=p->rlink; p->rlink->llink=temp; temp->llink=p; p->rlink=temp; }

} void del() { int data1; cout<<"Enter data to delete:"; cin>>data1; if(start->data==data1) { temp=start; start=start->rlink; start->llink=NULL; temp->llink=NULL; delete(temp); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

else { p=start; int f=0; while(p!=NULL) { if(p->data==data1) { p->llink->rlink=p->rlink; p->rlink->llink=p->llink; p->llink=NULL; delete(p); f=1; } } if(!f) { cout<<"\nNo data\n"; return; } } } }; void main() { list l; int ch; clrscr(); while(1) { cout<<"\n1.add\n2.show\n3.Delete\n4.Inserat\n5.exit\nEnter choice:"; cin>>ch; switch(ch) { case 1: l.create(); break; case 2: l.show(); break; case 3: l.del(); break; Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

case 4: l.insert(); break; case 5: exit(1); } } }

OUTPUT
1. create 2. show 3. Add 4. Delete 5. Exit Enter your choice: 1 Enter data: 11 1. create 2. show 3. Add 4. Delete 5. Exit Enter your choice: 1 Enter data: 22 1. create 2. show 3. Add 4. Delete 5. Exit Enter your choice: 3 Enter position: 2 Enter data: 33 1. create 2. show 3. Add Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

4. Delete 5. Exit Enter your choice: 2 11 33 22 1 .create 2. show 3. Add 4. Delete 5. Exit Enter your choice: 4 Enter data to delete: 33 1. create 2. show 3. Add 4. Delete 5. Exit Enter your choice: 2 11 22 1. create 2. show 3. Add 4. Delete 5. Exit Enter your choice: 5

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

POLYNOMIAL ADDITION
ALGORITHM
STEP 01: START POLY ADDITION. STEP 02: ACCEPT NUMBER OF NODES OF THE FIRST POLYNOMIAL P1. STEP 03: I=0. STEP 04: REPEAT STEP 04 TO STEP 09 UNTIL I>NUMBER OF NODES. STEP 05: CREATE NEW NODE. STEP 06: ACCEPT COEFICIENT AND EXPONTENT TO TNSERT. STEP 07: NEW NODECO=COEFICIENT, NEW NODEEXPO=EXPONENT, NEW NODE LINK=NULL. STEP 08: IF (START IS EQUAL TO NULL OR STARTEXPO<TEMPEXPO) 1) NEW NODELINK=START. 2) START=NEW NODE. STEP 09: ELSE 1) P=START. 2) REPEAT STEP 3 UNTIL PLINK IS NOT NULL OR PLINKEXPO>NEW NODEEXPO. 3) P=PLINK. 4) NEW NODELINK=PLINK. 5) PLINK=NEW NODE. STEP 10: REPEAT THE ABOVE STEPS FOR A SECOND POLYNOMIAL. STEP 11: REPEAT STEP 12 TO STEP 17 UNTIL FIRST OR SECOND

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

POLYNOMIAL IS NULL. STEP 12: CREATE A NEW NODE TEMP. STEP 13: IF (START IS EQUAL TO NULL) i) ii) STEP 14: ELSE i) ii) PLINK=TEMP. P=TEMP. START=TEMP. P=START.

STEP 15: IF (P1EXPO>P2EXPO) i) ii) iii) TEMPCO=P1CO. TEMPEXPO=P2EXPO. P1=P1LINK.

STEP 16: ELSE IF (P2EXPO>P1EXPO) i) ii) iii) STEP 17: ELSE i) ii) iii) iv) TEMPCO=P1CO+P2CO. TEMPEXPO=P1EXPO. P1=P1LINK. P2=P2LINK. TEMPCO=P2CO. TEMPEXPO=P2EXPO. P2=P2LINK.

STEP 18: REPEAT STEP 19 TO STEP 24 UNTIL P1 IS NOT NULL.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

STEP 19: CREATE A NEW NODE TEMP. STEP 20: TEMPCO=P1CO. STEP 21: TEMPEXPO=P1EXPO. STEP 22: IF (START IS EQUAL TO NULL) 1) STARTEMP. 2) P=START. STEP 23: ELSE 1) PLINK=TEMP. 2) P=TEMP. STEP 24: P1=P1LINK.
S

STEP 25: REPEAT STEP 26 REPEAT STEP 19 TO STEP 24 UNTIL P1 IS NOT NULL.

STEP 26: TEMP=NEW NODE. STEP 27: TEMPCO=P2CO. STEP 28: TEMPEXPO=P2EXPO. STEP 29: IF (START IS EQUAL TO NULL) 1) START=TEMP. 2) P=START. STEP 30: ELSE 1) PLINK=TEMP. 2) P=TEMP.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

STEP 31: P2=P2LINK. STEP 32: TEMP=START (RESULTANT POLYNOMIAL) STEP 33: REPEAT STEP 34 TO STEP 35 UNTIL TEMP IS NOT NULL. STEP 34: DISPLAY TEMPCO, TEMPEXPO. STEP 35: TEMP=TEMPLINK. STEP 36: STOP POLY ADDITION.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM

/*Program to perform polynomial addition*/ #include<iostream.h> #include<conio.h> class poly { struct node { int co; int expo; struct node *link; }*start,*temp,*p; public: void read(int a) { int i; for(i=0;i<a;i++) { create(); } } poly() { start=NULL; } void create() { temp=new node; cout<<"enter coefficient and exponent:"; cin>>temp->co; cin>>temp->expo; temp->link=NULL; if(start==NULL||start->expo<temp->expo) { temp->link=start; start=temp; p=start; } else { while(p->link!=NULL&&(p->link->expo>temp->expo)) p=p->link; Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

temp->link=p->link; p->link=temp; } } void show() { p=start; while(p->link!=NULL) { cout<<p->co<<"x^ "<<p->expo<<"+"; p=p->link; } cout<<p->co<<"x^ "<<p->expo<<"\n"; } poly add(poly po2) { struct node *p1,*p2,*p3; poly po3; p1=start; p2=po2.start; p3=po3.start; if(p1==NULL&&p2==NULL) p3=NULL; while(p1!=NULL&&p2!=NULL) { temp=new node; temp->link=NULL; if(p3==NULL) { p3=temp; p=p3; } else { p->link=temp; p=p->link; } if(p1->expo>p2->expo) { temp->co=p1->co; temp->expo=p1->expo; p1=p1->link; }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

else if(p2->expo>p1->expo) { temp->co=p2->co; temp->expo=p2->expo; p2=p2->link; } else { temp->co=p2->co+p1->co; temp->expo=p2->expo; p2=p2->link; p1=p1->link; } } while(p1!=NULL) { temp=new node; temp->link=NULL; if(p3==NULL) { p3=temp; p=p3; } else { p->link=temp; p=p->link; } temp->co=p1->co; temp->expo=p1->expo; p1=p1->link; } while(p2!=NULL) { temp=new node; temp->link=NULL; if(p3==NULL) { p3=temp; p=p3; } else { p->link=temp; Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

p=p->link; } temp->co=p2->co; temp->expo=p2->expo; p2=p2->link; } po3.start=p3; return po3; } }; void main() { poly p1,p2,p3; clrscr(); int f,s; cout<<"Enter no.of node of first polynomial :"; cin>>f; p1.read(f); cout<<"Enter no.of node of second polynomial :"; cin>>s; p2.read(s); cout<<"First polynomial\n"; p1.show(); cout<<"\nSecond polynomial\n"; p2.show(); cout<<"\n"; p3=p1.add(p2); cout<<"Result\n"; p3.show(); getch(); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

OUTPUT
Enter no. of node of first polynomial: 3 Enter coefficient and exponent: 1 2 Enter coefficient and exponent: 1 3 Enter coefficient and exponent: 3 4

Enter no. of node of second polynomial: 3 Enter coefficient and exponent: 3 4 Enter coefficient and exponent: 4 5 Enter coefficient and exponent: 2 1

First polynomial: 4x^5+1x^3+1x^2 Second polynomial: 4x^5+3x^4+2x^1 Result: 4x^5+6x^4+1x^2+2x^1

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

CONVERSION OF INFIX EXPRESSION TO POSTFIX EXPRESSION


ALGORITHM
STEP 01: START CONVERSION. STEP 02: PUSH (ON TO STACK.ADD ) AT END OF THE INFIX EXPRESSION P. STEP 03: SCAN FROM LEFT TO RIGHT AND REPEAT STEP 03-06 FOR EACH

ELEMENT OF P UNTIL STACK IS EMPTY.


STEP 04: IF AN OPERAND IS ENCOUNTERED ADD IT TO THE RESULT Q. STEP 04: IF A LEFT PARATHESIS IS ENCOUNTERED PUSH IT ON TO THE STACK. STEP 05: IF AN OPERATOR (OP) IS ENCOUNTERED 1) REPEATEDLY POP FROM STACK AND ADD Q EACH OPERATOR WHICH HAS THE SAME PRECEDENCE OR HIGHER THAN (OP). 2) ADD (OP) TO STACK. STEP 06: IF A RIGHT PARANTHESIS IS ENCOUNTERED 1) REPEATEDLY POP FROM STACK ADD TO Q UNTIL ( IS ENCOUNTERED. 2) REMOVE THE LEFT PARANTHESIS. STEP 07: EXIT.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/*Program to convert an infix expression to postfix expression*/ #include<iostream.h> #include<conio.h> #include<math.h> #include<string.h> #define MAX 100 class infix { char stack[MAX]; int top; public: infix() { top=-1; } void push(char); char pop(); int prec(char); void convert(); }; void infix::push(char item) { if(top==(MAX-1)) { cout<<"stack full:"; getch(); } else { stack[++top]=item; } } char infix::pop() { char item; if(top==-1) { cout<<"Stack null:"; getch(); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

else { item=stack[top--]; } return item; } int infix::prec(char item) { switch(item) { case '(': return 1; break; case ')': return 2; break; case '+': case '-': return 3; break; case '*': case '/': case '%': return 4; break; case '^': return 5; break; default: return 0; } } void infix::convert() { int i,j; char exp[MAX],post[MAX],ch; cout<<"Enter an infix expression:\n"; cin>>exp; int len=strlen(exp); exp[len++]=')'; push('('); for(i=0,j=0;i<len;i++) { int val=prec(exp[i]);

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

switch(val) { case 0: post[j++]=exp[i]; break; case 1: push('('); break; case 2: ch=pop(); while(ch!='(') { post[j++]=ch; ch=pop(); } break; case 3: ch=pop(); while(prec(ch)>=3) { post[++j]=ch; ch=pop(); } push(ch); push(exp[i]); break; case 4: ch=pop(); while(prec(ch)>=4) { post[++j]=ch; ch=pop(); } push(ch); push(exp[i]); break; case 5: ch=pop(); while(prec(ch)>=3) { post[++j]=ch; ch=pop(); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

push(ch); push(exp[i]); break; } } cout<<"Postfix expression:\n"; for(i=0;i<j;i++) cout<<post[i]; } void main() { char c; infix ob; do { clrscr(); ob.convert(); cout<<"Continue:\t"; cin>>c; } while(c=='y'||c=='Y'); getch(); }

OUTPUT
Enter the infix expression :( (a +b) / ((c * (d e))) Post fix expression is a b + c d e - * / Do you want to continue? n

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

EVALUATION OF POSTFIX EXPRESSION


ALGORITHM
STEP 01: ADD A ) AT THE END OF THE POSTFIX EXPRESSION P. STEP 02: SCAN P FROM LEFT TO RIGHT AND REPEAT STEP 03-04 FOR EACH ELEMENT OF P UNTIL THE SENTINAL ) IS ENCOUNTERED. STEP 03: IF AN OPERAND IS ENCOUNTERED PUT IT ON THE STACK. STEP 04: IF AN OPERATOR (OP) IS ENCOUNTERED, THEN 1) REMOVE THE TWO TOP ELEMENTS OF STACK WHERE A IS THE TOP ELEMENT AND B IS THE NEXT TO TOP ELEMENT. 2) EVALUATE B (OP) A. 3) PLACE THE RESULT ON TO THE STACK. STEP 05: RESULT IS EQUAL TO TH TOP ELEMENT OF THE STACK. STEP 06: EXIT.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/*Program for postfix evaluation*/ #include<iostream.h> #include<conio.h> #include<string.h> #include<math.h> #define MAX 100 class eval { char stack[MAX]; int top; public: eval() { top=-1; } void push(char); char pop(); int postcalc(); }; void eval::push(char item) { if(top==MAX-1) { cout<<"Stack is full:"; getch(); } else { stack[++top]=item; } } char eval::pop() { char item; if(top==-1) { cout<<"Stack is empty:"; return 0; } else { item=stack[top--]; } Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

return item; } int eval::postcalc() { int a,b,temp=0,len; char exp[MAX]; cout<<"Enter a postfix expression:\n"; cin>>exp; len=strlen(exp); for(int i=0;i<len;i++) { if(exp[i]<='9'&&exp[i]>='0') push(exp[i]-48); else { a=int(pop()); b=int(pop()); switch(exp[i]) { case '+': temp=a+b; break; case '-': temp=b-a; break; case '*': temp=b*a; break; case '/': temp=b/a; break; case '%': temp=b%a; break; case '^': temp=pow(b,a); break; } push(char(temp)); } } return (pop()); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

void main() { eval e; char ch; int res; do { clrscr(); res=e.postcalc(); cout<<"Result="<<res<<"\n"; cout<<"Want to continue:"; cin>>ch; } while(ch=='y'||ch=='Y'); getch(); }

OUTPUT
Enter a postfix expression: 123+ * Result=5 Want to continue: n

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

BINARY TREE CREATION


ALGORITHM

STEP 01: START BINARY TREE OPERATIONS. STEP 02: CREATE ROOT NODE. STEP 03: READ OPT. STEP 04: IF OPT = 1, CALL CREATE FUNCTION. STEP 05: IF OPT = 2, CALL PRE-ORDER TRAVERSAL. STEP 06: IF OPT = 3, CALL IN-ORDER TRAVERSAL. STEP 07: IF OPT = 4, CALL POST-ORDER TRAVERSAL. STEP 08: IF OPT=5, GO TO STEP 09, ELSE GO TO STEP 03. STEP 09: STOP BINARY TREE OPERATIONS. A) ALGORITHM FOR CREATING BINARY TREE STEP 01: START CREATION. STEP 02: PRINT ROOTDATA, HAS A LEFT CHILD ? . STEP 03: READ CH. STEP 04: IF(CH EQUAL TO Y) i. CREATE NEW NODE TEMP. ii. READ TEMPDATA. iii. ASSIGN TEMPLC=TEMPRC=NULL ROOTLC=TEMP. iv. CALL CREATE(ROOTLC) STEP 05: PRINT ROOTDATA, HAS A RIGHT CHILD ? . STEP 06: READ CH. STEP 07: IF(CH EQUAL TO Y) i. CREATE NEW NODE TEMP. ii. READ TEMPDATA. iii. ASSIGN TEMPLC=TEMPRC=NULL, ROOTRC=TEMP. iv. CALL CREATE(ROOTRC). STEP 08: STOP CREATION. Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

B) ALGORITHM FOR PRE-ORDER TRAVERSAL STEP 01: START PRE-ORDER TRAVERSAL. STEP 02: VISIT THE ROOT NODE. STEP 03: TRAVERSE THE LEFT SUB TREE IN PRE-ORDER. STEP 04: TRAVERSE THE RIGHT SUB TREE IN PRE-ORDER. STEP 05: STOP PREORDER TRAVERSAL.

C) ALGORITHM FOR IN-ORDER TRAVERSAL STEP 01: START IN-ORDER TRAVERSAL. STEP 02: TRAVERSE THE LEFT SUB TREE IN IN-ORDER. STEP 03: VISIT THE ROOT NODE. STEP 04: TRAVERSE THE RIGHT SUB TREE IN IN-ORDER. STEP 05: STOP IN-ORDER TRAVERSAL.

D) ALGORITHM FOR POST-ORDER TRAVERSAL STEP 01: START POST-ORDER TRAVERSAL. STEP 02: TRAVERSE THE LEFT SUB TREE IN POST-ORDER. STEP 03: TRAVERSE THE RIGHT SUBTREE IN POST-ORDER. STEP 04: VISIT THE ROOT NODE. STEP 05: STOP POST-ORDER TRAVERSAL.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM

/*Program to create a binary tree and perform preorder, inorder and post order traversal*/ #include<iostream.h> #include<conio.h> class tree { struct node { int data; struct node *rc,*lc; }*temp; public: struct node *root; tree() { root =NULL; root=new node; cout<<"\n\tEnter root node"; cin>>root->data; root->lc=root->rc=NULL; } void create(struct node *rt); void preorder(struct node *rt); void inorder(struct node *rt); void postorder(struct node *rt); }; void tree::create(struct node *rt) { char ch='n'; cout<<"\n\t"; cout<<rt->data; cout<<"Has a left child?"; cin>>ch; if(ch=='y') { temp=new node; cout<<"\n\tEnter data"; cin>>temp->data; temp->lc=temp->rc=NULL; rt->lc=temp; create(rt->lc); } cout<<"\n\t"; cout<<rt->data; Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab cout<<"Has a right child?"; cin>>ch; if(ch=='y') { temp=new node; cout<<"\n\tEnter data"; cin>>temp->data; temp->lc=temp->rc=NULL; rt->rc=temp; create(rt->rc); } } void tree::preorder(struct node *tr) { if(tr!=NULL) { cout<<tr->data; preorder(tr->lc); preorder(tr->rc); } } void tree::inorder(struct node *tr) { if(tr!=NULL) { inorder(tr->lc); cout<<tr->data; inorder(tr->rc); } } void tree::postorder(struct node *tr) { if(tr!=NULL) { postorder(tr->lc); postorder(tr->rc); cout<<tr->data; } }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab void main() { clrscr(); cout<<"\n\n\tBINARY TREE CREATION"; cout<<"\n\t------------------------------------"; tree t1; t1.create(t1.root); clrscr(); cout<<"\n\tPreorder Traversal : "; t1.preorder(t1.root); cout<<"\n\tInorder Traversal : "; t1.inorder(t1.root); cout<<"\n\tPost Order Traversal: "; t1.postorder(t1.root); getch();

OUTPUT
BINARY TREE CREATION -----------------------------Enter root node10 10Has a left child?y Enter data20 20Has a left child?y Enter data30 30Has a left child?n 30Has a right child?n 20Has a right child?y Enter data40 40Has a left child?n

40Has a right child?n 10Has a right child?y Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

Enter data50

50Has a left child?y Enter data60 60Has a left child?n 60Has a right child?n 50Has a right child?y Enter data70 70Has a left child?n 70Has a right child?n Preorder Traversal : 10 20 30 40 50 60 70 Inorder Traversal : 30 20 40 10 60 50 70 Post Order Traversal: 30 40 20 60 70 50 10

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

BINARY SEARCH TREE


ALGORITHM
STEP 01: START BINARY SEARCH TREE OPERATIONS. STEP 02: CREATE ROOT NODE. STEP 03: READ OPT. STEP 04: IF OPT = 1, CALL CREATE FUNCTION. STEP 05: IF OPT = 2, CALL DELETE FUNCTION. STEP 06: IF OPT = 3, CALL SHOW FUNCTION. STEP 07: IF OPT = 4, CALL SEARCH FUNCTION. STEP 08: IF OPT = 5, GO TO STEP 08, ELSE GO TO STEP 03. STEP 09: STOP BINARY SEARCH TREE OPERATIONS.

A) ALGORITHM TO CREATE BINARY SEARCH TREE

STEP 01: START CREATION. STEP 02: INPUT THE DATA TO BE PUSHED AND ROOT NODE OF THE TREE. STEP 03: CREATE NEW NODE, TEMP. STEP 04: ASSIGN TEMPDATA=DATA1, TEMPRC=TEMPLC=NULL. STEP 05: IF(ROOT EQUAL TO NULL) a) ROOT=TEMP. STEP 06: ELSE IF(DATA1<ROOTDATA) Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab a) ROOT=ROOTLC. b) GO TO STEP 05. STEP 07: ELSE IF(DATA1>ROOTDATA) a) ROOT=ROOTRC. b) GO TO STEP 05. STEP 08: IF(DATA1<ROOTDATA) a) ROOTLC=TEMP.

STEP 09: ELSE IF(DATA1>ROOTDATA) a) ROOTRC=TEMP. STEP 10: STOP CREATION.

B) ALGORITHM FOR DELETION

STEP 01: START DELETION. STEP 02: READ DATA TO BE DELETED, DATA1. STEP 03: ASSIGN TEMP=ROOT. STEP 04: IF(ROOT EQUAL TO NULL) a) DISPLAY DATA IS NOT IN TREE. b) EXIT. STEP 05: IF(TEMPLC EQUAL TO NULL) a) ASSIGN LOC=TEMP. b) TEMP=TEMPRC. STEP 06: IF(TEMPRC EQUAL TO NULL) a) ASSIGN LOC=TEMP. b) TEMP=TEMPLC. STEP 07: IF((TEMPLC != NULL) AND (TEMPRC != NULL)) a) LOC=TEMP->RC. STEP 08: WHILE(TEMPLC NOT EQUAL TO NULL) a) LOC=LOCLC. Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab STEP 09: LOCLC=TEMPLC. STEP 10: LOCRC=TEMPRC. STEP 11: STOP DELETION. C) ALGORITHM FOR DISPLY STEP 01: START PRE-ORDER TRAVERSAL. STEP 02: VISIT THE ROOT NODE.

STEP 03: TRAVERSE THE LEFT SUB TREE IN PRE-ORDER. STEP 04: TRAVERSE THE RIGHT SUB TREE IN PRE-ORDER. STEP 05: STOP PREORDER TRAVERSAL.

D) ALGORITHM FOR SEARCH

STEP 01: START SEARCHING. STEP 02: READ THE DATA TO BE DELETED. STEP 03: ASSIGN RT=ROOT. STEP 04: REPEAT STEP 05 TO STEP 07 UNTIL RT NOT EQUAL TO NULL. STEP 05: IF(RT->DATA=DATA) i) ii) DISPLAY DATA FOUND. RETURN

STEP 06: IF(RT->DATA<DATA) i) RT=RT->LC.

STEP 07: IF (RT->DATA>DATA) i) RT=RT->DATA.

STEP 08: IF(RT EQUALS NULL) i) DISPLAY DATA NOT FOUND

STEP 09: STOP SEARCHING. Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to create a binary search tree and perform deletion*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> class tree { struct node { int data; struct node *lc,*rc; }*temp,*par; public: struct node *root; tree() { root=NULL; } void create(struct node *rt) { temp=new node; cout<<"\nEnter data:"; cin>>temp->data; temp->rc=temp->lc=NULL; if(root==NULL) { root=temp; } else Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab { rt=root; while(rt!=NULL) { if(rt->data<temp->data) { par=rt; rt=rt->rc; } else if(rt->data>temp->data) { par=rt; rt=rt->lc; } else { cout<<"\n\tNumber already exist"; return; } } } if(temp->data>par->data) { par->rc=temp; } else par->lc=temp; } void insert(struct node *rt) { temp=new node; cout<<"\nEnter data:"; cin>>temp->data; temp->rc=temp->lc=NULL; if(root==NULL) { root=temp; } else { rt=root; while(rt!=NULL) { if(rt->data<temp->data) { par=rt; rt=rt->rc; Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab } else if(rt->data>temp->data) { par=rt; rt=rt->lc; } else { cout<<"\n\tNumber already exist"; return; } } } if(temp->data>par->data) { par->rc=temp; } else par->lc=temp; } void show(struct node *rt) { if(rt!=NULL) { show(rt->lc); cout<<rt->data; show(rt->rc); } } void del(struct node *rt) { int data; cout<<"\n\tEnter data to delete:"; cin>>data; par=NULL; while(rt->data!=data) { if(rt->data<data) { par=rt; rt=rt->rc; } else { par=rt; rt=rt->lc;

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab } } if(rt->lc==NULL &&rt->rc==NULL) { if(par->lc==rt) par->lc=NULL; else par->rc=NULL; }

if((rt->lc==NULL&&rt->rc!=NULL)|| (rt->lc!=NULL&&rt->rc==NULL)) { struct node *child; if(rt->lc!=NULL) child=rt->lc; else child=rt->rc; if(par->lc==rt) par->lc=child; else par->rc=child; } if(rt->lc!=NULL&&rt->rc!=NULL) { struct node *temp,*t; temp=rt->rc; while(temp->lc!=NULL) { t=temp; temp=temp->lc; } rt->data=temp->data; if(temp->rc!=NULL) t->lc=temp->rc; else t->lc=NULL; }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab } void search(struct node *rt) { int data; cout<<"\n\tEnter data to search:"; cin>>data; while(rt!=NULL) { if(rt->data==data) { cout<<"\n\tData found";

break; } else if(rt->data<data) { rt=rt->rc; } else { rt=rt->lc; } } if(rt==NULL) cout<<"\n\t Data not found"; } }; void main() { tree t; clrscr(); int ch; char c; while(1) { cout<<"\n BINARY SEARCH TREE "; cout<<"\n-------------------------------------------------------"; cout<<"\n\t1.CREATE\n\t2.SHOW\n\t3.INSERT\n\t4.DELETE\n\t5.SEARCH\n\ t\6.EXIT\n\tEnter your choice:"; cin>>ch; switch(ch) { case 1:

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab do { t.create(t.root); cout<<"\n\tcontinue:"; cin>>c; }while(c=='Y'||c=='y'); break; case 2: cout<<"\n\tPreorder Traversal :"; t.show(t.root); break;

case 3: t.create(t.root); break; case 4: t.del(t.root); break; case 5: t.search(t.root); break; case 6: exit(1); } } getch(); }

OUTPUT
BINARY SEARCH TREE ------------------------------------------------------1.CREATE 2.SHOW 3.INSERT 4.DELETE 5.SEARCH 6.EXIT Enter your choice:1 Enter data:50 continue:y Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab Enter data:30 continue:y Enter data:40 continue:y Enter data:20 continue:y

Enter data:70 continue:y Enter data:60 continue:y Enter data:80 Continue:n BINARY SEARCH TREE ------------------------------------------------------1.CREATE 2.SHOW 3.INSERT 4.DELETE 5.SEARCH 6.EXIT Enter your choice:2 Preorder Traversal : 20 30 40 50 60 70 80 BINARY SEARCH TREE ------------------------------------------------------1.CREATE 2.SHOW 3.INSERT 4.DELETE 5.SEARCH 6.EXIT Enter your choice:3 Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

Enter data:10 BINARY SEARCH TREE ------------------------------------------------------1.CREATE 2.SHOW 3.INSERT 4.DELETE 5.SEARCH 6.EXIT Enter your choice:2 Preorder Traversal :10 20 30 40 50 60 70 80

BINARY SEARCH TREE ------------------------------------------------------1.CREATE 2.SHOW 3.INSERT 4.DELETE 5.SEARCH 6.EXIT Enter your choice:4 Enter data to delete:20 BINARY SEARCH TREE ------------------------------------------------------1.CREATE 2.SHOW 3.INSERT 4.DELETE 5.SEARCH 6.EXIT Enter your choice:2 Preorder Traversal :10 30 40 50 60 70 80

BINARY SEARCH TREE ------------------------------------------------------1.CREATE 2.SHOW 3.INSERT Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab 4.DELETE 5.SEARCH 6.EXIT Enter your choice:5 Enter data to search:10 Data found

BUBBLE SORT
ALGORITHM
STEP 01: START BUBBLE SORT. STEP 02: INPUT N NUMBERS OF AN ARRAY. STEP 03: INITIALISE I=0 AND REPEAT THROUGH STEP 05 IF (I<N). STEP 04: INITIALISE J=0 AND REPEAT THROUGH STEP 05 IF (J<N-I-1). STEP 05: IF (A[J]>A[J+1]) a) TEMP=A[J]. b) A[J]=A[J+1]. c) A[J+1]=TEMP. STEP 06: DISPLAY THE SORTED NUMBERS OF ARRAY. STEP 07: STOP BUBBLE SORT.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to perform sorting using bubble sort*/ #include<iostream.h> #include<conio.h> void main() { int a[10],n; clrscr(); cout<<"\n BUBBLE SORT"; cout<<"\n------------------------------------------------------"; cout<<"\n\tEnter no.of elements :"; cin>>n; cout<<"\n\tEnter "<<n<<" numbers:\n"; for(int i=0;i<n;i++) { cout<<"\n\t"; cin>>a[i]; } cout<<"\n\n\tBEFORE SORTING:\n\n\t"; for( i=0;i<n;i++) cout<<a[i]<<" "; for(i=0;i<n-1;i++) { int echg=0,temp; for(int j=0;j<n-1-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; echg++; } } if(echg==0) break; } cout<<"\n\n\tAFTER SORTING :\n\n\t"; for(i=0;i<n;i++) cout<<a[i]<<" "; getch(); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

OUTPUT
BUBBLE SORT -----------------------------------------------------Enter no.of elements :10 Enter 10 numbers: 56 89 74 45 36 25 14 47 58

BEFORE SORTING: 56 89 74 45 12 36 25 14 47 58 AFTER SORTING : 12 14 25 36 45 47 56 58 74 89

SELECTION SORT
ALGORITHM
STEP 01: START SELECTION SORT. STEP 02: INPUT N NUMBERS OF AN ARRAY. STEP 03: INITIALISE I=0 AND REPEAT THROUGH STEP 06 IF (I<N-1). a) MIN=A [I]. b) LOC=I STEP 04: INITIALISE J=I+1 AND REPEAT THROUGH STEP 05 IF (I<N-1) STEP 05: IF (A[J]<MIN) a) MIN=A [J]. b) LOC=J. Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab STEP 06: IF (LOC NOT EQUAL TO I) a) TEMP=A [I]. b) A [I] =A [LOC]. c) A [LOC]=TEMP. STEP 07: DISPLAY SORTED NUMBERS OF ARRAY A. STEP 08: STOP SELECTION SORT.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to perfom sorting using selection sort*/ #include<iostream.h> #include<conio.h> void main() { int a[10],n; clrscr(); cout<<"\n SELECTION SORT"; cout<<"\n------------------------------------------------------"; cout<<"\n\tEnter no.of elements :"; cin>>n; cout<<"\n\tEnter "<<n<<" numbers:\n"; for(int i=0;i<n;i++) { cout<<"\n\t"; cin>>a[i]; } cout<<"\n\n\tBEFORE SORTING:\n\n\t"; for( i=0;i<n;i++) cout<<a[i]<<" "; for(i=0;i<n-1;i++) { int sma=i; for(int k=i+1;k<n;k++) { if(a[sma]>a[k]) sma=k; } if(i!=sma) { int temp; temp=a[i]; a[i]=a[sma]; a[sma]=temp; } } cout<<"\n\n\tAFTER SORTING :\n\n\t"; for(i=0;i<n;i++) cout<<a[i]<<" "; getch(); } Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

OUTPUT
SELECTION SORT -----------------------------------------------------Enter no.of elements :10 Enter 10 numbers: 63 42 53 67 13 41 38 14 12 98

BEFORE SORTING: 63 42 53 67 13 41 38 14 12 98 AFTER SORTING : 12 13 14 38 41 42 53 63 67 98

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

INSERTION SORT
ALGORITHM
STEP 01: START INSERTION SORT. STEP 02: INPUT AN ARRAY A OF N NUMBERS. STEP 03: INITIALISE I=1 AND REPEAT THROUGH SEPS 05 BY INCREMENTING I BY 1. a) IF (I<=N-1) i. ii. STEP 04: TEMP= A [I]. POS= I-1.

REPEAT STEP 03 IF (TEMP < A[POS] AND (POS>=0)) a) A [POS+1]=A[POS]. b) POS=POS-1.

STEP 05: A [POS+1]=TEMP. STEP 06: STOP INSERTION SORT.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to perform sorting using insertion sort */ #include<iostream.h> #include<conio.h> void main() { int a[10],n; clrscr(); cout<<"\n\n\n\t\t INSERTION SORT"; cout<<"\n\t\t----------------------------"; cout<<"\n\tEnter the limit:"; cin>>n; cout<<"\n\tEnter "<<n<<"elements"; for(int i=0;i<n;i++) { cout<<"\n\t\t\t"; cin>>a[i]; } cout<<"\n\n\tBefore Sorting:"; for(i=0;i<n;i++) { cout<<"\t"; cout<<a[i]; } for(int j=1;j<n;j++) { int k=a[j]; for(i=j-1;i>=0&&k<=a[i];i--) { a[i+1]=a[i]; } a[i+1]=k; } cout<<"\n\n\tAfter Sorting :"; for(i=0;i<n;i++) { cout<<"\t"; cout<<a[i]; } getch();

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

OUTPUT
INSERTION SORT ---------------------------Enter the limit :10 Enter 10 numbers: 63 42 53 67 13 41 38 14 12 98

BEFORE SORTING: 63 42 53 67 13 41 38 14 12 98 AFTER SORTING : 12 13 14 38 41 42 53 63 67 98

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

QUICK SORT
ALGORITHM
STEP 01: START QUICK SORT. STEP 02: INPUT N NUMBER OF ELEMENTS IN AN ARRAY A. STEP 03: INITIALISE LOW=2, UP = N, KEY=A [(LOW+UP)/2]. STEP 04: REPEAT THROUGH STEP 09 WHILE (LOW<=UP). STEP 05: REPEAT STEP 06 WHILE (A [LOW] <KEY). STEP 06: LOW=LOW+1. STEP 07: REPEAT STEP 08 WHILE (A [UP] <KEY). STEP 08: UP=UP-1. STEP 09: IF(LOW<=UP) a) TEMP=A[LOW]. b) A[LOW]=A[UP]. c) A[UP]=TEMP. d) LOW=LOW+1. e) UP=UP-1. STEP 10: IF (I<UP), QUICKSORT (A, 1, UP). STEP 11: IF (LOW<N), QUICKSORT (A, LOW, N). STEP 12: STOP QUICK SORT.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to perform sorting using quick sort*/ #include<iostream.h> #include<conio.h> #define MAX 30 enum bool {FALSE,TRUE}; void display(int arr[],int low,int up) { int i; for(i=low;i<=up;i++) cout<<arr[i]<<" "; } void quick(int arr[],int low,int up) { int piv,temp,left,right; enum bool pivot_placed=FALSE; left=low; right=up; piv=low; if(low>=up) return; cout<<"\n\tSublist"; display(arr,low,up); while(pivot_placed==FALSE) { while(arr[piv]<=arr[right]&&piv!=right) right=right-1; if(piv==right) pivot_placed=TRUE; if(arr[piv]>arr[right]) { temp=arr[piv]; arr[piv]=arr[right]; arr[right]=temp; piv=right; } while(arr[piv]>=arr[left]&&left!=piv) left+=1; if(piv==left) pivot_placed=TRUE;

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

if(arr[piv]<arr[left]) { temp=arr[piv]; arr[piv]=arr[left]; arr[left]=temp; piv=left; } } cout<<"\n\tPivot placed :"<<arr[piv]<<"\n\t"; display(arr,low,up); cout<<"\n\t"; quick(arr,low,piv-1); quick(arr,piv+1,up); } void main() { int arr[MAX],n,i; clrscr(); cout<<"\n\n\tQUICK SORT"; cout<<"\n\t---------------------"; cout<<"\n\tEnter no of elements :"; cin>>n; for(i=0;i<n;i++) { cout<<"\n\tEnter element "<<i+1<<":"; cin>>arr[i]; } cout<<"\n\tUnsorted List :"; display(arr,0,n-1); cout<<endl; quick(arr,0,n-1); cout<<"\n\tSorted list"; display(arr,0,n-1); getch(); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

OUTPUT
QUICK SORT --------------------Enter no of elements: 6 Enter element 1:32 Enter element 2:12 Enter element 3:65 Enter element 4:43 Enter element 5:78 Enter element 6:18 Unsorted List: 32 12 65 43 78 18 Sub list: 32 12 65 43 78 18 Pivot placed: 32 18 12 32 43 78 65 Sub list: 18 12 Pivot placed: 18 12 18 Sub list: 43 78 65 Pivot placed: 43 43 78 65 Sub list: 78 65 Pivot placed: 78 65 78

Sorted list: 12 18 32 43 65 78

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

MERGE SORT
ALGORITHM
STEP 01: START MERGE SORT. STEP 02: INPUT AN ARRAY A OF N ELEMENTS TO BE SORTED. STEP 03: SIZE=1. STEP 04: REPEAT THROUGH STEP 14 WHILE (SIZE<N). a) SET L1=0, K=0. STEP 05: REPEAT THROUGH STEP 11 WHILE ((L1+SIZE) <N). a) L2=L1+SIZE. b) U1=L2-1. STEP 06: a) IF ((L2+SIZE-1)<N), ASSIGN U2=L2+SIZE-1. b) ELSE, ASSIGN U2=N-1. STEP 07: INITIALISE I=L1,J=L2 AND REPEAT THROUGH STEP 08 IF (I<=U1) AND (J<=U2). STEP 08: a) IF (A[I]<=A[J]), ASSIGN TEMP[K]=A[I++]. ELSE, ASSIGN TEMP[K]=A[J++]. STEP 09: REPEAT STEP 09 BY INCREMENTING K UNTIL (I<=U1). a) TEMP[K]=A[I++]. STEP 10: REPEAT STEP 10 BY INCREMENTING K UNTIL (J<=U2). a) TEMP[K]=A[J++]. STEP 11: L1=U2+1. STEP 12: INITIALIZE I=L1 AND REPEAT STEP 12 IF (K<N) BY INCREMENTING I BY 1. a) TEMP[K++]=A[I]. STEP 13: INITIALISE I=0 AND REPEAT STEP 12 IF (I<N) BY INCREMENTING I BY 1. a) A[I]=A[L]. STEP 14: SIZE=SIZE *2. STEP 15: STOP MERGE SORT. Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to perform sorting using merge sort*/ #include<iostream.h> #include<conio.h> int a[10]; void msort(int,int); void main() { int n; clrscr(); cout<<"\n\n\n\t\t MERGE SORT"; cout<<"\n\t\t----------------------------"; cout<<"\n\tEnter the limit:"; cin>>n; cout<<"\n\tEnter "<<n<<"elements"; for(int i=0;i<n;i++) { cout<<"\n\t\t\t"; cin>>a[i]; } cout<<"\n\n\tBefore Sorting:"; for(i=0;i<n;i++) { cout<<"\t"; cout<<a[i]; } msort(0,n-1); cout<<"\n\n\tAfter Sorting:"; for(i=0;i<n;i++) { cout<<"\t"; cout<<a[i]; } getch(); } void msort(int low, int high) { void merge(int,int,int); int mid; if(low!=high) { mid=(low+high)/2; msort(low,mid); msort(mid+1,high); merge(low,mid,high); } Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab } void merge(int low,int mid,int high) { int temp[10]; int i=low; int j=mid+1; int k=low; while((i<=mid)&&(j<=high)) { if(a[i]<=a[j]) temp[k++]=a[j+1]; else temp[k++]=a[j++]; } while(i<=mid) temp[k++]=a[i++]; while(j<=high) temp[k++]=a[j++]; for(i=low;i<=high;i++) a[i]=temp[i];

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

OUTPUT
MERGE SORT ---------------------------Enter the limit :10 Enter 10 numbers: 63 42 53 67 13 41 38 14 12 98

BEFORE SORTING: 63 42 53 67 13 41 38 14 12 98 AFTER SORTING : 12 13 14 38 41 42 53 63 67 98

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

HEAP SORT
ALGORITHM
STEP 01: START HEAP SORT. STEP 02: REPEAT FOR J=1 TO N-1,CALL INSHEAP(A, J,A[J+1]). STEP 03: REPEAT WHILE (N >1) a) CALL DELHEAP (A, N , ITEM). b) SET A[N+1]=ITEM. STEP 04: STOP HEAP SORT.

A) ALGORITHM FOR INSERTION.

STEP 01: START INSERTION. STEP 02: ASSIGN TREE=A, N=J, ITEM=A[J+1]. STEP 03: SET N=N+1, PTR=N. STEP 04: REPEAT STEP 05 THROUGH 08 WHILE (PTR>1). STEP 05: SET PAR=PTR/2. STEP 06: IF (ITEM<=TREE[PAR], a) SET TREE[PTR]=ITEM. b) RETURN. STEP 07: SET TREE[PTR]=TREE[PAR]. STEP 08: SET PTR=PAR. STEP 09: ASSIGN ITEM AS THE ROOT OF H. STEP 10: SET TREE[1]=ITEM. STEP 11: STOP INSERTION.

B) ALGORITHM FOR DELETION.

STEP 01: START DELETION. STEP 02: ASSIGN TREE=A, ITEM=TREE[1]. Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab STEP 03: ASSIGN LAST=TREE[N],N=N-1.

STEP 04: SET PTR=1, LEFT=2, RIGHT=3. STEP 05: REPEAT STEPS 06 THROUGH 08 WHILE (RIGHT<=N). STEP 06: IF(LAST>=TREE[LEFT] AND LAST>=TREE[RIGHT]) a) SET TREE[PTR]=LAST. b) EXIT. STEP 07: IF (TREE[RIGHT]<=TREE[LEFT]) SET TREE[PTR]=TREE[LEFT] AND PTR=LEFT. ELSE SET TREE[PTR]=PTR[RIGHT] AND PTR=RIGHT. STEP 08: SET LEFT=2*PTR, RIGHT=LEFT+1. STEP 09: IF (LEFT=N) AND IF(LAST<TREE[LEFT]) a) SET TREE[PTR]=TREE[LEFT]. b) PTR=LEFT. STEP 10: SET TREE[PTR]=LAST. STEP 11: STOP DELETION.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to perform sorting using heap sort*/ #include<iostream.h> #include<conio.h> int arr[20],n; void display() { int i; for(i=0;i<n;i++) { cout<<arr[i]; cout<<"\n"; } } void insert(int num,int loc) { int par; while(loc>0) { par=(loc-1)/2; if(num<=arr[par]) { arr[loc]=num; return; } arr[loc]=arr[par]; loc=par; } arr[0]=num; } void create_heap() { int i; for(i=0;i<n;i++) { insert(arr[i],i); } } void del_root(int last) { int left,right,i,temp; i=0; Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

temp=arr[i]; arr[i]=arr[last]; arr[last]=temp; left=2*i+1; right=2*i+2; while(right<last) { if(arr[i]>=arr[left]&&arr[i]>=arr[right]) { return; } if(arr[right]<=arr[left]) { temp=arr[i]; arr[i]=arr[left]; arr[left]=temp; i=left; } else { temp=arr[i]; arr[i]=arr[right]; arr[right]=temp; i=right; } left=2*i+1; right=2*i+2; } if(left==last-1&&arr[i]<arr[left]) { temp=arr[i]; arr[i]=arr[left]; arr[left]=temp; } } void heap_sort() { int last; for(last=n-1;last>0;last--) { del_root(last); } }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

void main() { int i; clrscr(); cout<<"Enter number of elements :"; cin>>n; for(i=0;i<n;i++) { cout<<"Enter element"<< i+1; cin>>arr[i]; } cout<<"Entered List :"; display(); create_heap(); cout<<"Heap\n"; display(); heap_sort(); cout<<"Sorted List "; display(); getch(); }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

OUTPUT
HEAP SORT -------------------Enter number of elements :10 Enter element1: 99 Enter element2: 88 Enter element3: 55 Enter element4: 22 Enter element5: 33 Enter element6: 66 Enter element7: 44 Enter element8: 11 Enter element9: 77 Enter element10: 100

Entered List :99 88 55 22 33 66 44 11 77 100 Heap :100 99 66 77 88 55 44 11 22 33

Sorted List :11 22 33 44 55 66 77 88 99 100

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

LINEAR SEARCH
ALGORITHM
STEP 01: START LINEAR SEARCH. STEP 02: INPUT AN ARRAY A OF N ELEMENTS AND DATA TO BE SEARCHED STEP 03: INITIALISE LOC=-1. STEP 04: INITIALISE I=0 AND REPEAT THROUGH STEP 05 IF(I<N) BY INCREMENTING I BY 1. STEP 05: IF (DATA=A[I]) a) LOC=I. b) GO TO STEP 04. STEP 06: IF (LOC >0) a) DISPLAY DATA FOUND AND SEARCHING SUCCESSFUL. STEP 07: ELSE a) DISPLAY DATA NOT FOUND AND SEARCHING UNSUCCESSFUL. STEP 08: STOP LINEAR SEARCH.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to search an element using linear search*/ #include<iostream.h> #include<conio.h> void main() { int a[10],n,temp,f=0; char ch; clrscr(); cout<<"\n\n\t LINEAR SEARCH"; cout<<"\n\t------------------------"; cout<<"\n\tEnter Limit:"; cin>>n; cout<<"\n\tEnter elements:"; for(int i=0;i<n;i++) { cout<<"\n\t\t\t"; cin>>a[i]; } do { cout<<"\n\tElements in the list:"; for(i=0;i<n;i++) { cout<<a[i]; cout<<"\n\t"; } cout<<"\n\tEnter the element to search:"; cin>>temp; for(i=0;i<n;i++) { f=0; if(a[i]==temp) { f=1; cout<<"\n\tElement found at position"<<i+1; break; } } if(f==0) { cout<<"\n\tNo element foud"; } Department of computer application, TKM College of Engineering Page __

Object Oriented Programming and Data Structures Lab

cout<<"\n\n\tEnter y to continue search:(y/n)"; cin>>ch; } while(ch=='y'||ch=='Y'); getch(); }

OUTPUT
LINEAR SEARCH ------------------------------Enter Limit:5 Enter elements: 15 25 35 45 55 Elements in the list:15 25 35 45 55 Enter the element to search:25 Element found at position 2 Enter y to continue search:(y/n)y

Elements in the list:15 25 35 45 55 Enter the element to search:67 No element foud Enter y to continue search:(y/n)n

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

BINARY SEARCH
ALGORITHM
STEP 01: START BINARY SEARCH. STEP 02: INPUT AN ARRAY A OF N ELEMENTS AND DATA TO BE SEARCHED STEP 03: ASSIGN LOW=0, HIGH=N, MID=(LOW+HIGH)/2. STEP 04: REPEAT STEPS 05 AND 06 WHILE (LOW<=HIGH) AND (A[MID]!=DATA). STEP 05: IF (DATA<A[MID]) a) HIGH=MID-1. STEP 06: ELSE a) LOW = MID+1. STEP 07: MID = (LOW+HIGH)/2. STEP 08: IF(A[MID]==DATA) a) DISPLAY DATA FOUND. STEP 09: ELSE a) DISPLAY DATA NOT FOUND . STEP 10: STOP BINARY SEARCH.

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

PROGRAM
/* Program to searh an element using binary search*/ #include<iostream.h> #include<conio.h> void main() { int a[10],n,key,f=0,mid,low,high; char ch; clrscr(); cout<<"\n\n\t BINARY SEARCH"; cout<<"\n\t------------------------"; cout<<"\n\tEnter Limit:"; cin>>n; cout<<"\n\tEnter elements:"; for(int i=0;i<n;i++) { cout<<"\n\t\t\t"; cin>>a[i]; } do { cout<<"\n\tElements in the list:"; for(i=0;i<n;i++) { cout<<a[i]; cout<<"\n\t"; } cout<<"\n\tEnter the element to search:"; cin>>key; low=0; high=n; while(low<=high) { f=0; mid=(low+high)/2; if(a[mid]==key) { f=1; cout<<"\n\tElement found at position :"<<mid+1; break; }

Department of computer application, TKM College of Engineering

Page __

Object Oriented Programming and Data Structures Lab

else { if(key<a[mid]) high=mid-1; else low=mid+1; } } if(f==0) cout<<"\n\tElement not Found:"; cout<<"\n\t Enter your choice:(y/n):"; cin>>ch; } while(ch=='y'||ch=='Y'); getch(); }

OUTPUT
BINARY SEARCH -------------------------------Enter Limit:6 Enter elements: 9 5 7 1 2 6 3 Elements in the list: 9 5 7 1 2 6 3

Enter the element to search:7 Element Found at position:3 Enter your choice:(y/n):y

Enter the element to search:8 Element not found Enter your choice:(y/n):n

Department of computer application, TKM College of Engineering

Page __

You might also like