data structures lab manual

67
K.S.R COLLEGE OF ENGINEERING,TIRUCHENGODE-637215. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING. EC 2209 - DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB MANUAL YEAR: II SEM:III Prepared By : K.CHINNUSAMY A.VELLINGIRI EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Upload: hakkembabu

Post on 02-Dec-2014

394 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Data Structures Lab Manual

K.S.R COLLEGE OF ENGINEERING,TIRUCHENGODE-637215.

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING.

EC 2209 - DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING

LAB MANUAL

YEAR: II SEM:III

Prepared By :

K.CHINNUSAMY

A.VELLINGIRI

B.VIJAY KUMAR

K.KRISHNA KUMAR

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 2: Data Structures Lab Manual

LIST OF EXPERIMENTS

CYCLE – I

EXP.NO NAME OF THE EXPERIMENT

1 Basic Programs for C++ Concepts

2 Array implementation of List Abstract Data Type (ADT)

3 Linked list implementation of List ADT

4 Cursor implementation of List ADT

5 Stack ADT - Array and linked list implementations

6 Bubble sort

CYCLE – II

EXP.NO NAME OF THE EXPERIMENT

1 Queue ADT – Array and linked list implementations

2 Search Tree ADT - Binary Search Tree

3 Heap Sort

4 Quick Sort

5 Insertion sort

6

The next two exercises are to be done by implementing the following source files

a) Program source files for Stack Application 1b) Array implementation of Stack ADTc) Linked list implementation of Stack ADTd) Program source files for Stack Application 2

An appropriate header file for the Stack ADT should be #included in (a) and (d)

Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c))

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 3: Data Structures Lab Manual

WORK PLAN

EXPT.NO

NAME OF THE EXPERIMENT STAFF INCHARGE

CYCLE I

1 Basic Programs for C++ Concepts

2Array implementation of List Abstract Data Type (ADT)

3 Linked list implementation of List ADT

4 Cursor implementation of List ADT

5Stack ADT - Array and linked list implementations

6 Bubble sort

EXPT.NO

NAME OF THE EXPERIMENT STAFF INCHARGE

CYCLE II

1 Queue ADT – Array and linked list implementations

2 Search Tree ADT - Binary Search Tree

3 Heap Sort

4 Quick Sort

5 Insertion sort

6

The next two exercises are to be done by implementing the following source files

a) Program source files for Stack Application 1b) Array implementation of Stack ADTc) Linked list implementation of Stack ADTd) Program source files for Stack Application 2

An appropriate header file for the Stack ADT should be #included in (a) and (d)Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c))

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 4: Data Structures Lab Manual

BATCH 1 (CYCLE I)

INTRODUCTION CLASS ON:

DAY: PERIOD/TIME:

DATESUB BATCH NUMBER AND EXPERIMENT NO

Proposed Actual 1 TO 36

1

2

3

4

5

6

INTRODUCTION CLASS ON:

DAY: PERIOD/TIME:

DATESUB BATCH NUMBER AND EXPERIMENT NO

Proposed Actual 1 TO 37

1

2

3

4

5

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 5: Data Structures Lab Manual

6

BATCH 1 (CYCLE II)

DAY: PERIOD/TIME:

DATESUB BATCH NUMBER AND EXPERIMENT NO

Proposed Actual1 TO 36

1

2

3

4

5

6

BATCH 2 (CYCLE II)

DAY: PERIOD/TIME:

DATESUB BATCH NUMBER AND EXPERIMENT NO

Proposed Actual 1 TO 37

1

2

3

4

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 6: Data Structures Lab Manual

5

6

BASIC PROGRAMS FOR C++ CONCEPTSEx.No.: Date:

Aim To write a C++ programs for basic operations.

1.1.SWAP TWO INTEGER VALUES BY REFERENCE

#include iostream.h>void swap(int *x,int *y){Int t;t=*x;*x=*y;*y=t;}Void main(){Int a,b;Cout<<”Enter the values”;Cin>>a>>b;Cout<<”the values before swap is:”<<a<<b;Swap(&a,&b);Cout<<”the values after swap is:”<<a<<b;}

Output:

Enter the values : 10 20Values before swap 10 20Values after swap 20 10

1.2. CLASS AND OBJECT IMPLEMENTATION

#include<iostream.h>#include<string.h>Class student{Int regno;Char name[20];Public:Void setdata(int r, char*n);Void put data()

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 7: Data Structures Lab Manual

{Cout<<”reg.no”<<regno;Cout<<”Name”<<name;}};Void student::setdata(int r, char *n){regno=r;strcpy(name,n);}Int main(){Stuident s1,s2;S1.setdata(100,”ajay”);S2.setdata(101,”vijay”);S1.putdata();S2.putdata();}

Output:

Reg no 100 name ajay reg no 101 name vijay

1.3. INLINE FUNCTIONS

#include<iostream.h>

Inline int square (int num){Return num*num;}int main(){

Float n;cout<<”enter the values”:cin>>n;cout<<”square is :”<<square(n);

}

Output:

Enter the values 4Square is: 16

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 8: Data Structures Lab Manual

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 9: Data Structures Lab Manual

1.4. INHERITENCE (MULTIPLE INHERITANCE)

#include<iostream.h>Class student{Protected: int rollno;Public: void getnumber(int a){rollno=a;}Void putnumber(){Cout<<rollno;}};Class test: public student {Protected: float sub1,sub2;Public: void getmarks (float x, float y){Sub1=x;Sub2=y;}Void putmarks(){Cout<<sub1<<sub2;}};Class result: public test {Float total;Public: void display (){Total=Sub1+Sub2;Putnumber();Putmarks();Cout<<total;}};Int main(){result stud1;Sud1.getnumber(100);Sud1.getmarks(85,75);Sud1.diaplay();}Output:

1008575160

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 10: Data Structures Lab Manual

1.5. POLYMORPHIC FUNCTION

#include<iostream.h>Class base {Private : int x;Public: base() { x=5;}Virtual void print() { cout<<”x=”<<x;}};Class derived : public base {Private : int y;Public: derived() { y=10;}Void print() { cout<<”y=”<<y;}};Int main(){Base *bptr;Base obj1;Derived obj2;Obj1.print();Obj2.print();Bptr=&obj1;Bptr->print();Bptr=&obj2;Bptr->print();}

Output:

X=5y=10x=5y=10

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 11: Data Structures Lab Manual

ARRAY IMPLEMENTATION OF LIST ABSTRACT DATA TYPE (ADT)

Ex.No.: Date:

Aim:To write a C++ program to do the list operations.

Algorithm:1. Initialize the list with n items.2. Get the choice of operation either insertion or deletion.3. For insertion, get the item to be inserted and its position. Then position to last

position move the items in the list one step forward, then insert the new item in its corresponding position.

4. For deletion get the item to be deleted and find its position then move the items in the list one step backward from last position to position found.

Program:#include<iostream.h>#include<conio.h>int a[20],n;int main(){ int i, ch,p,v;

cout<<" Enter the No of Element in the list : "; cin>>n; cout<<" Enter the List of Element one by one : "; for (i=0;i<n;i++) cin>>a[i]; while(1) { cout<<"\n 1. Insert.... 2,Delete... 3. Exit... :"; cin>>ch; switch(ch) { case 1: cout<<"\n Enter the Position & Element : " ; cin>>p>>v; for(i=n;i>=p;i--) a[i]=a[i-1]; a[p]=v; n++; break; case 2: cout<<"\n Enter the Deleting Element : " ; cin>>v; p=0; for(i=0;i<n;i++)

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 12: Data Structures Lab Manual

{ if (a[i]==v) p=1; a[i]=a[i+p]; } n--; break; case 3: return(1); } cout<<"\n the List of Element : " ; for(i=0;i<n;i++)

cout<<a[i]<<"\t";}getch();} Output:

Enter the no of elements in the list : 5

Enter the List of Element one by one : 10 20 30 40 50

1.Insert.... 2,Delete... 3. Exit... : 1

Enter the Position & Element: 215

List of the Elements : 10 15 20 30 40 50

1.Insert.... 2,Delete... 3. Exit... : 2

Enter the Deleting Element : 20

List of the Elements : 10 15 30 40 50

1.Insert.... 2,Delete... 3. Exit... : 2

Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 13: Data Structures Lab Manual

LINKED LIST IMPLEMENTATION OF LIST ADT

Ex.No.: Date:

I. Linked list implementation of List ADT- Singly Linked ListII. Linked list implementation of List ADT- Doubly Linked List

III. Linked list implementation of List ADT- Circular Linked List

I. LINKED LIST IMPLEMENTATION OF LIST ADT- SINGLY LINKED LIST

Aim:To write a C++ program to implement the single linked list

Algorithm:

1. Start the program2. Create a menu in main program3. Get the choice4. If the choice is one create a new node5. If choice is two then insert into the node6. If the choice is three Delete from the node7. else come out of the menu program8. Stop

Application:This program teaches with how to create space for a node and also how inset and delete

in dynamic memory allocation schemes

Program:#include<iostream.h>#include<conio.h>class Node { public :

int data; Node *next;

void getdata() {

cout<<"Enter the Data"; cin>>data; }

void putdata() {

cout<<data<<"\t"; }

}; int main()

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 14: Data Structures Lab Manual

{ Node *a,*tmp,*tmp1; int ch,i,j; a = NULL; while(1) {

cout<<"\n1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. \nSelect... : ";

cin>>ch; switch(ch) {

case 1: tmp= (Node*)new(Node); tmp->getdata(); tmp->next=a; a= tmp; break;

case 2: a->putdata(); a=a->next; break;

case 3: cout<<"Give the Insertion Point Value and L/R (0/1) :"; cin>>i>>j; tmp=a; while((j==1)?(tmp->data!=i):(tmp->next->data!=i))

{ tmp=tmp->next;

} tmp1= (Node*)new(Node); tmp1->getdata(); tmp1->next=tmp->next; tmp->next=tmp1; break; case 4: cout<<"Give the Deletion Point Value :"; cin>>i; tmp=a; while(tmp->next->data!=i) { tmp=tmp->next;

} if(tmp!=NULL)

{tmp->next->putdata(); tmp->next=tmp->next->next; } break; case 5: cout<<"Give the Modify Point Value & New Value :"; cin>>i>>j; tmp=a; while(tmp->data!=i)

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 15: Data Structures Lab Manual

{ tmp=tmp->next;

} if(tmp!=NULL) { tmp->data=j; tmp->putdata(); } break; case 6: cout<<"The Elements are....:"; tmp=a; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->next; } break; case 7: return(1); } getch(); } } Output :

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 1Enter the Data 10

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 1Enter the Data 20

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 1Enter the Data 301. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 1Enter the Data 40

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 6

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 16: Data Structures Lab Manual

The Elements are ….. : 40 30 20 10

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 240

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 6The Elements are ….. : 30 20 10

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 3Give the Insertion Point Value and F/B (0/1) : 20 0Enter the Data 251. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... :6The Elements are ….. : 30 25 20 10

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 3Give the Insertion Point Value and F/B (0/1) : 20 1Enter the Data 151. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... :6The Elements are ….. : 30 25 20 15 10

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 5Give the Modify Point Value & New Value : 25 27271. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 6The Elements are ….. : 30 27 20 15 10

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit..Select... : 7

Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 17: Data Structures Lab Manual

II. LINKED LIST IMPLEMENTATION OF LIST ADT- DOUBLY LINKED LIST

Ex.No.: Date:

Aim:To write a C++ program for doubly linked list

Algorithm:1. Start the program2. Create a menu in main program3. Get the choice4. If the choice is one create a new node which has double link5. If choice is two then insert into the node ,change the appropriate link6. If the choice is three Delete from the node, remove the link and join it7. else come out of the menu program8. Stop

Application:

This program helps the student how to create a double linked list. This dynamic memory allocation sees how the link is established between the two nodes and how to navigate through the double linked list.

Program:

#include<iostream.h>#include<conio.h>class Node { public : int data; Node *next, *pre; void getdata() { cout<<"Enter the Data"; cin>>data; } void putdata() { cout<<data<<"\t"; } }; int main() { Node *h,*tmp,*tmp1;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 18: Data Structures Lab Manual

int ch,i,j; h = NULL; while(1) { cout<<"\n1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. \nSelect... : "; cin>>ch; switch(ch) { case 1: tmp= (Node*)new(Node); tmp->getdata(); tmp->pre=h; h->next=tmp; tmp->next=NULL; h=tmp; break; case 2: cout<<"Give the Insertion Point Value and L/R (0/1) :"; cin>>i>>j; tmp=h; while(tmp->data!=i) { tmp=tmp->pre; } tmp1= (Node*)new(Node); tmp1->getdata(); if(j==0)tmp=tmp->next; tmp1->pre=tmp->pre; tmp->pre->next=tmp1; tmp1->next=tmp; tmp->pre=tmp1; break; case 3: cout<<"Give the Deletion Point Value :"; cin>>i; tmp=h; while(tmp->data!=i) { tmp=tmp->pre;

} if(tmp!=NULL) {tmp->putdata(); tmp->pre->next=tmp->next; tmp->next->pre=tmp->pre; } break; case 4: cout<<"Give the Modify Point Value & New Value :"; cin>>i>>j; tmp=h; while(tmp->data!=i && tmp!=NULL)

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 19: Data Structures Lab Manual

{ tmp=tmp->pre;

} if(tmp!=NULL) { tmp->data=j; tmp->putdata(); } break; case 5: cout<<"The Elements are....:"; tmp=h; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->pre; } break; case 6: return(1); } getch(); } } Output:

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1Enter the Data 10

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1Enter the Data 20

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1Enter the Data 30

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1Enter the Data 40

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 5The Elements are ….. : 40 30 20 10

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 20: Data Structures Lab Manual

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 340

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 5The Elements are ….. : 30 20 10

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 2Give the Insertion Point Value and F/B (0/1) : 20 0Enter the Data 25

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... :5The Elements are ….. : 30 25 20 10

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit..Select... : 2Give the Insertion Point Value and F/B (0/1) : 20 1Enter the Data 15

11. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit..Select... :5The Elements are ….. : 30 25 20 15 10

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 4Give the Modify Point Value & New Value : 25 2727

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit..Select... : 4The Elements are ….. : 30 27 20 15 10

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit..

Select... : 6

Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 21: Data Structures Lab Manual

III. LINKED LIST IMPLEMENTATION OF LIST ADT- CIRCULAR LINKED LIST

Aim:To write a C++ program to create and manipulate the circular linked list

Algorithm:1. Start the program2. Create a menu in main program3. Get the choice4. If the choice is one create a new node for circular linked list5. If choice is two then insert into the node, and change the appropriate link6. If the choice is three Delete from the node, change the link before deletion7. else come out of the menu program8. Stop

Application:

This program helps the student how to create a circular linked list and how to navigate, display, insert and delete them.

Program:

#include<iostream.h>#include<conio.h>template <class T>class Linklist{

int n,i,j,ins_ctr;T *a,temp,y;class node{

friend class Linklist<T>;T data;node *link;

} *head,*p,*q,*tail;public:

Linklist(){

head=NULL;tail=NULL;Menu();

}void insert(T x){

node *n_node;p=NULL;n_node=new node();n_node->data=x;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 22: Data Structures Lab Manual

n_node->link=NULL;if(!head){

head=n_node;tail=n_node;n_node->link=head;

}else if(head->data >x){

n_node->link=head;head=n_node;

}else{

for(p=head;(p!=tail) && (p->link->data <x);p=p->link);if(p!=tail){

n_node->link=p->link;p->link=n_node;

}else{

p->link=n_node;n_node->link=head;tail=n_node;

}}

} void display() {

for(p=head;p!=tail;p=p->link)cout<<p->data<<"\t";

cout<<p->data<<"\n"; } void deletion(T x) {

if(!head)cout<<"List is empty\n";

if(head->data==x){

q=head;head=head->link;delete q;

}else{for(p=head;(p->link!=tail) && (p->link->data !=x);p=p->link);

if(p->link!=tail)

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 23: Data Structures Lab Manual

{q=p->link;p->link=p->link->link;delete q;

}else if(p->link->data==x){

q=p->link;p->link=p->link->link;tail=p;

}else

cout<<"Element Not in the List\n";}

}

void Menu(){

int ch,flag=1,x,ctr;cout<<"Circular linked list\n";cout<<"1.Add\n2.Delete\n3.Display\n4.Exit\n\n";while(flag){

cout<<"Enter your choice:\n";cin>>ch;switch(ch){

case 1:cout<<"Enter the new element:"; cin>>x; insert(x); display(); break;case 2:

cout<<"Enter the element to be deleted\n";cin>>x;deletion(x);display();break;

case 3:display();break;

case 4:flag=0;

}}getch();

}};

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 24: Data Structures Lab Manual

void main(){

clrscr();Linklist <int> l1;

}

Output :

1. Add2. Delete3. Display4. Exit

Enter your choice:1Enter the new element : 12

Enter your choice:1Enter the new element : 5

Enter your choice:1Enter the new element : 6

Enter your choice:35 6 12

Enter the your choice:2Enter the element to be deleted65 12

Enter your choice:4

Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 25: Data Structures Lab Manual

CURSOR IMPLEMENTATION OF LIST ADT

Ex.No.: Date:

Aim: To write a C++ program to implement the list using cursor.

Algorithm:1. Start the program2. Create a menu in main program3. Get the choice4. The choice is one create a new 5. The choice is two then insertion.6. The choice is three delete a element.7. The choice is four search 8. The choice is five search 9. The choice six exit10. Stop

Program:#include<iostream.h>#include <conio.h>int main(){

int a[20], i,j, ch,n,x;while (1){

clrscr ();cout<<"\n1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit..\n select :";cin>>ch;switch(ch)

{case 1: cout<<"Enter the Total no of value"; cin>>n; for(i=0;i<n;i++) {

cout<<"Enter the "<<i+1<<" value"; cin>>a[i]; }

break;

case 2: cout<<" Enter the Insert Position"; cin>>i; for(j=n;j>i-1;j--) a[j]=a[j-1]; cout<<" Enter the Insert Value"; cin>>a[i-1]; n++;

break;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 26: Data Structures Lab Manual

case 3: if(n<0) cout<<"The List is empty";

else{

cout<<" Enter the Deleting Position"; cin>>j;

cout<<"The Delete element"<< a[j]; for(i=j-1;i<n;i++)

a[i]=a[i+1]; n--;

}break;

case 4: for(i=0;i<n;i++) cout<<a[i]<<"\t";

break;case 5: if(n<0)cout<<"The List is empty";else

{ cout<<" Enter the Searching element";

cin>>x; for(i=0;i<n;i++)

if ( a[i]==x) {

cout<<" the Given Element Found in " <<i+1 <<" Position"; goto aa;

} cout<<" the Given Element Not Found ";

aa: break; case 6: return (0);}getch();

}}}

Output :

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 1Enter the Total no of value 5Enter the value 11Enter the value 22Enter the value 33Enter the value 44Enter the value 55

11 20 22 33 44 55

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 27: Data Structures Lab Manual

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 411 22 33 44 55

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 2Enter the Insert Position 2Enter the Insert Value 20

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 4

11 20 22 33 44 55

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 3Enter the Deleting Position2

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 411 22 33 44 55

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 5Enter the Searching element 55The Given Element Found in Position 5

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exitselect : 6Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 28: Data Structures Lab Manual

Result: Thus the cursor implementatiton using list has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 29: Data Structures Lab Manual

STACK ADT USING ARRAY AND LINKED IMPLEMENTATION

I. STACK ADT USING ARRAY IMPLEMENTATION

Ex.No.: Date:

Aim: To implement stack using arrays Objective:

To represent stack using an array and to perform In and Out operations in the stack.Algorithm:

1. Declare an array sizeN.2. Assign TOP as a pointer to denote the top element in the stack3. Get the new element Y to be added in to the stack.4. If TOP is greater than or equal to N then display stack over flow; otherwise set TOP=TOP+1.5. Set S [TOP] = Y.6. To delete top element from the stack check if TOP =0,the display stack underflow, otherwise

decrement TOP by one, and display S [TOP+1].7. Display the stack S from 1 to TOP.

Program :

#include<iostream.h>#include <conio.h>int main(){

clrscr ();int a[20], top=-1, i, ch;while (1){

cout<<"\n 1.Push.. 2.Pop.. 3.List.. 4. Exit..\n select :";cin>>ch;switch(ch){

case 1: if(top>=19)cout<<"The stake is full";else{ cout<<"\n Enter the value"; cin>>a[++top];}break;case 2: if(top<0)cout<<"\n The stake is empty";else{cout<<"\n The Out element"<< a[top--];}break;case 3: for(i=0;i<=top;i++) cout<<a[i]<<"\t";

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 30: Data Structures Lab Manual

break;case 4: return (0);}getch();

}}

Output :

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1Enter the value 10

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1Enter the value 20

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1Enter the value 30

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1Enter the value 40

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :310 20 30 40

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :210 20 30

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :210 20

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :210 2

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :210

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :2

1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :210 201.Push.. 2.Pop.. 3.List.. 4. Exit..

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 31: Data Structures Lab Manual

select : 4Exit RESULT:

Thus the Stack ADT using array implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 32: Data Structures Lab Manual

II. STACK ADT USING LINKED IMPLEMENTATION

Aim: To implement stack using Link

Algorithm:1. Start the program2. Create a menu in main program3. Get the choice4. If the choice is one Push5. If choice is two Pop6. If the choice is three Display list 7. else come out of the menu program8. Stop

Program :

#include<iostream.h>#include<conio.h>class Node { public :

int data; Node *next;

void getdata() {

cout<<"Enter the Data"; cin>>data; }

void putdata() {

cout<<data<<"\t"; }

}; int main() {

Node *a,*tmp; int ch; a = NULL; while(1) {

cout<<"\n1. Push... 2. Pop... 3. Display... 4. Exit... \nSelect... : "; cin>>ch; switch(ch) {

case 1: tmp=(Node*)new(Node); tmp->getdata(); tmp->next=a;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 33: Data Structures Lab Manual

a= tmp; break;

case 2: if(a==NULL) { cout<<" Stack is Empty"; } else {

a->putdata(); a=a->next;

} break;

case 3: cout<<"The Elements are....:"; tmp=a; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->next; } break;

case 4: return(1); } getch(); } }

Output :1. Push... 2. Pop... 3. Display... 4. Exit...

Select... :1Enter the Data 10

1. Push... 2. Pop... 3. Display... 4. Exit... Select... :1Enter the Data 20

1. Push... 2. Pop... 3. Display... 4. Exit... Select... :1Enter the Data 30

1. Push... 2. Pop... 3. Display... 4. Exit... Select... :1Enter the Data 40

1. Push... 2. Pop... 3. Display... 4. Exit... Select... :340 30 20 10

1. Push... 2. Pop... 3. Display... 4. Exit... Select... :230 20 10

1. Push... 2. Pop... 3. Display... 4. Exit... Select... :3

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 34: Data Structures Lab Manual

ExitRESULT:

Thus the Stack ADT using linked list implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 35: Data Structures Lab Manual

QUEUE ADT – ARRAY AND LINKED LIST IMPLEMENTATIONS

I. QUEUE ADT – ARRAY LIST IMPLEMENTATIONS

Ex.No.: Date:

Aim: To write a C++ program to do the Queue operations.

Algorithm:1. Start the Program.2. Assign top= -1 in the main function.3. Create a main program. 4. Get the choice.5. If the choice 1 is In.6. If the choice 2 is Out.7. If the choice 3 is List.8. If the choice 4 is come out of the main program.9. Stop

Program :

#include<iostream.h>#include <conio.h>int main(){

clrscr ();int a[20], top=-1, i, ch;while (1){

cout<<"\n 1.Push.. 2.Pop.. 3.List.. 4. Exit..\n select :";cin>>ch;switch(ch){

case 1: if(top>=19)cout<<"The stake is full";else{ cout<<"\n Enter the value"; cin>>a[++top];}break;case 2: if(top<0)cout<<"\n The stake is empty";else{cout<<"\n The Pop element"<< a[0];for (i=1;i<=top;i++)a[i-1]=a[i];top--;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 36: Data Structures Lab Manual

}break;case 3: for(i=0;i<=top;i++) cout<<a[i]<<"\t"; break;case 4: return (0);}getch();

}}

Output :

1.In.. 2.Out.. 3.List.. 4. Exit.. select :1`Enter the value 10

1.In.. 2.Out.. 3.List.. 4. Exit.. select :1Enter the value 20

1.In.. 2.Out.. 3.List.. 4. Exit.. select :1Enter the value 30

1.In.. 2.Out.. 3.List.. 4. Exit.. select :1Enter the value 40

1.In.. 2.Out.. 3.List.. 4. Exit.. select :310 20 30 40

1.In.. 2.Out.. 3.List.. 4. Exit.. select :2The Out Element is 10

1.In.. 2.Out.. 3.List.. 4. Exit.. select :320 30 40

1.In.. 2.Out.. 3.List.. 4. Exit.. select : 4

Exit

RESULT: Thus the queue ADT array list implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 37: Data Structures Lab Manual

II. QUEUE ADT – LINKED LIST IMPLEMENTATIONSAim:

To write a C++ program to do the Queue operations.

Algorithm:1. Start the program2. Create a menu in main program3. Get the choice4. If the choice is one In5. If choice is two Out6. If the choice is three Display list 7. else come out of the menu program8. Stop

Program:#include<iostream.h>#include<conio.h>class Node { public :

int data; Node *next;

void getdata() {

cout<<"Enter the Data"; cin>>data; }

void putdata() {

cout<<data<<"\t"; }

}; int main() {

Node *a,*h,*tmp; int ch; a = NULL; h = a; while(1) {

cout<<"\n1. In... 2. Out... 3. Display... 4. Exit... \nSelect... : "; cin>>ch; switch(ch) {

case 1:tmp= (Node*)new(Node); tmp->getdata(); tmp->next=a; a= tmp;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 38: Data Structures Lab Manual

break; case 2: if(a==NULL) cout<<"Queue is Empty";

else if (a->next==NULL) { a->putdata(); a=NULL;} else { tmp=a;

while (tmp->next->next!=NULL) tmp=tmp->next; tmp->next->putdata(); tmp->next=NULL; }break;

case 3: cout<<"The Elements are....:";

tmp=a; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->next; } break; case 4: return(1); } getch(); }

} Output :

1.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 101.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 201.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 301.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 401.In... 2. Out... 3. Display... 4. Exit... Select... : 3 The Elements are....40 30 20 101.In... 2. Out... 3. Display... 4. Exit... Select... : 2 101.In... 2. Out... 3. Display... 4. Exit... Select... : 3 The Elements are....40 30 201.In... 2. Out... 3. Display... 4. Exit...

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 39: Data Structures Lab Manual

Select... : 4Exit

Result: Thus the queue ADT linked list implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 40: Data Structures Lab Manual

QUICK SORTEx. No: Date:

Aim : To write a C++ program for Quick sort.

Algorithm:1. Start the program 2. Read the number of elements to be sorted3. Initialize a pivot (center) , left and right positions4. Check whether the center element is less than the left element. Then swap left and

center values5. Check whether the right element is less than the left element. Then swap right and left

values6. Check whether the right element is less than the center element. Then swap center and

right7. Finally the element in the left of pivot will be less than the pivot Element and the

element to the right of the pivot will be greater Than the pivot element . 8. Repeat the steps until the given elements are arranged in a sorted Manner9. Stop the program.

PROGRAM:

#include<process.h>#include<iostream.h>#include<conio.h>#include<stdlib.h>int Partition(int low,int high,int arr[]);void Quick_sort(int low,int high,int arr[]);void main(){int *a,n,low,high,i;clrscr();cout<<"Enter number of elements:";cin>>n;a=new int[n];cout<<"enter the elements:";for(i=0;i<n;i++)cin>>a[i];cout<<"Initial Order of elements"; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<" ";high=n-1;low=0;Quick_sort(low,high,a);cout<<"Final Array After Sorting:";

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 41: Data Structures Lab Manual

for(i=0;i<n;i++) cout<<a[i]<<" ";getch();}/*Function for partitioning the array*/int Partition(int low,int high,int arr[]){ int i,high_vac,low_vac,pivot/*,itr*/; pivot=arr[low]; while(high>low){ high_vac=arr[high]; while(pivot<high_vac) { if(high<=low) break; high--; high_vac=arr[high]; } arr[low]=high_vac; low_vac=arr[low]; while(pivot>low_vac) { if(high<=low) break; low++; low_vac=arr[low]; } arr[high]=low_vac;} arr[low]=pivot; return low;}void Quick_sort(int low,int high,int arr[]){ int Piv_index,i; if(low<high) { Piv_index=Partition(low,high,arr); Quick_sort(low,Piv_index-1,arr); Quick_sort(Piv_index+1,high,arr); }}

Output :

Initial order of elements: 22 12 66 55 02

Final array after sorting: 02 12 22 55 66

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 42: Data Structures Lab Manual

Result: Thus the Quick sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 43: Data Structures Lab Manual

INSERTION SORTEx. No: Date:

Aim :To write a C++ program for insertion sort.

Algorithm:1. Start the program2.enter the size of array and elements.3.compare the adjecent elements if it is not in correct order just swap the values.4.from next pass depends on elements it will be swapped5.in last pass, elements will be in order6.stop the program.

Program:#include <iostream.h>#include<conio.h>

void insertion sort(int x[],int length){ int key,i; for(int j=1;j<length;j++) { key=x[j]; i=j-1; while(x[i]>key && i>=0) {

x[i+1]=x[i];i--;

} x[i+1]=key; }}

int main(){ int A[25]; int size,i; int x; clrscr(); cout<<"Enter size of list"; cin>>size; cout<<"Enter numbers"; for(x=0;x<size;x++) {

cin>>A[x]; }cout<<"NON SORTED LIST:"<<endl; for(x=0;x<size;x++)

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 44: Data Structures Lab Manual

{ cout<<A[x]<<endl; } insertion sort(A,size); cout<<endl<<"SORTED LIST"<<endl; for(x=0;x<size;x++) { cout<<A[x]<<endl; } getch(); return 0;}

Output:Enter size of list: 5Enter numbers:12Enter numbers:22Enter numbers:02Enter numbers:15Enter numbers:36

NON SORTED LIST:1222021536

SORTED LIST:02

12152236

Result:

Thus the insertion sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 45: Data Structures Lab Manual

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 46: Data Structures Lab Manual

BUBBLE SORTEx. No: Date:

Aim :To write a C++ program for bubble sort.

Algorithm:1. Start the program.2. Enter the array of elements.3. Compare the adjacent elements if it is in wrong order. The adjacent element will be swapped.4.The pass through the list is repeated until no swaps required5.Stop the program.

Program:#include <stdio.h>#include <iostream.h>#include<conio.h>

void bubbleSort(int *array,int length) //Bubble sort function{

int i,j;for(i=0;i<5;i++){

for(j=0;j<i;j++){

if(array[i]>array[j]){

int temp=array[i]; //swaparray[i]=array[j];array[j]=temp;

}

}

}

}

void printElements(int *array,int length) //print array elements{

int i=0;printf("after sorting:\n");for(i=0;i<5;i++)

cout<<array[i]<<endl;}

void main()

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 47: Data Structures Lab Manual

{int a[5];clrscr();printf("enter the elements:\n");for(int i=0;i<5;i++)cin>>a[i]; // array elementsprintf("before sorting:\n");for(i=0;i<5;i++) cout<<a[i]<<endl;bubbleSort(a,5); //call to bubble sortprintElements(a,5); // print elementsgetch();

}

Output:Enter numbers:12Enter numbers:22Enter numbers:02Enter numbers:15Enter numbers:36

Before sorting:1222021536

after sorting:0212152236

Result:

Thus the bubble sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 48: Data Structures Lab Manual

HEAP SORTEx. No: Date:

Aim :To write a C++ program for Heap sort.

Algorithm:1. Start the program 2. Construct a min heap, such that the value of the root node should Be less than the left

key value & right key value.3. Use an empty array for storing the sorted elements4. Delete the root value (minimum element) and store it as a first element Of the array.5. Delete the last value of the root and store it as a root6. Reconstruct the min heap7. Repeat these steps until we get the sorted array and display it.8. Stop the program.

PROGRAM:

#include <stdio.h>#include<conio.h>#include<iostream.h>void fnSortHeap(int[], int); //prototypevoid main() //main {

int i, arr_num_items;int arr[6];clrscr();cout<<"Enter the elements:";for(i=0;i<6;i++)cin>>arr[i];

//total number of items in array.arr_num_items = 6;

//call fnSortHeap function for(arr_num_items - 2) times.for(i=arr_num_items; i>1; i--) {

fnSortHeap(arr, i - 1);}

cout<<"The Sorted Array\n"; //print the sorted arrayfor (i = 0; i < arr_num_items; i++)

cout<<arr[i]<<"\n";getch();return;

} //sort heap

void fnSortHeap(int arr[], int arr_ubound) {int i,o;int lChild, rChild, mChild, root, temp;

//find the root element of the current element

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 49: Data Structures Lab Manual

root = (arr_ubound-1)/2; //creating the heap

for(o=root;o>=0;o--) { for(i=root;i>=0;i--)

{lChild = (2*i)+1;rChild = (2*i)+2;if ((lChild <= arr_ubound) && (rChild <= arr_ubound))

{ if(arr[rChild] >= arr[lChild]) mChild = rChild; else mChild = lChild;}else { if(rChild > arr_ubound) mChild = lChild; else mChild = rChild;}if (arr[i] < arr[mChild]) //swap elements { temp = arr[i]; arr[i] = arr[mChild]; arr[mChild] = temp; }

}}temp = arr[0]; //move the max element to the end of the arrayarr[0] = arr[arr_ubound];arr[arr_ubound] = temp;return;

}Output:

Enter the elements : 21 12 02 30 32 11 05 25

The sorted array : 02 05 11 12 21 25 30 32

Result: Thus the heap sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 50: Data Structures Lab Manual

BINARY SEARCH TREE TRAVERSINGEx. No: Date:

Aim:To write a C++ program for Binary search tree traversing.

Algorithm:1. Start the program 2. Select the menu choice3. If the choice is 1 insert a node to binary tree4. If the choice is 2 the node will be in Pre order traversal5. If the choice is 3 the node will be in In order traversal6 If the choice is 4 the node will be in Post order traversal7. If the choice is 5 it exits the program8. Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *left; node *right; }; node *tree=NULL; node *insert(node *tree,int ele); void preorder(node *tree); void inorder(node *tree); void postorder(node *tree); int count=1; void main() { int ch,ele;

clrscr(); do { clrscr(); cout<<"\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a"; cout<<"\n\t\a\a2----PRE-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a3----IN-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a4----POST-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a5----EXIT.\a\a";

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 51: Data Structures Lab Manual

cout<<"\n\t\a\aENTER CHOICE::\a\a"; cin>>ch; switch(ch) { case 1: cout<<"\n\t\a\aENTER THE ELEMENT::\a\a"; cin>>ele; tree=insert(tree,ele); break;

case 2: cout<<"\n\t\a\a****PRE-ORDER TRAVERSAL OF A TREE****\a\a"; preorder(tree); break;

case 3: cout<<"\n\t\a\a****IN-ORDER TRAVERSAL OF A TREE****\a\a"; inorder(tree); break;

case 4: cout<<"\n\t\a\a***POST-ORDER TRAVERSAL OF A TREE***\a\a"; postorder(tree); break;

case 5: exit(0); } }while(ch!=5); } node *insert(node *tree,int ele) { if(tree==NULL) { tree=new node; tree->left=tree->right=NULL; tree->data=ele; count++; } else if(count%2==0) tree->left=insert(tree->left,ele); else tree->right=insert(tree->right,ele); return(tree); } void preorder(node *tree) {

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 52: Data Structures Lab Manual

if(tree!=NULL) { cout<<tree->data; preorder(tree->left); preorder(tree->right); getch(); } } void inorder(node *tree) { if(tree!=NULL) { inorder(tree->left); cout<<tree->data; inorder(tree->right); getch(); } } void postorder(node *tree) { if(tree!=NULL) { postorder(tree->left); postorder(tree->right); cout<<tree->data; getch(); } }

Output:

1.INSERT ANODE IN A BINARY TREE2. PRE ORDER TRAVEL3. IN-ORDER ORDER TRAVEL4.POST ORDER TRAVEL5.EXITENTER THE CHOICE 111ENTER THE CHOICE 122ENTER THE CHOICE 133ENTER THE CHOICE 211 22 33ENTER THE CHOICE 322 11 33

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 53: Data Structures Lab Manual

ENTER THE CHOICE 422 33 11ENTER THE CHOICE 5EXIT

Result: Thus the Binary search tree traversing has been successfully executed.

STACK APPLICATION -CONVERSION OF INFIX TO POSTFIX

Ex. No: Date:

Aim:To write a C++ program for conversion of infix to postfix.

Algorithm:1. Start the program 2. Enter the equation 3.CONVERT THE infix expressio to ppostfix using post function4.print the values of infix and postfix expression5. Stop the program.

PROGRAM:#include<iostream.h>#include<conio.h>class stack{char ch[20],eq[20],st[20];char top;int x,y,z;public:stack(){top='+';x=y=0;z=-1;}void get();void post();int prcd(char,char);

};void stack::get(){cout<<"enter any equation";cin>>st;cout<<"\n"<<st;}void stack::post()

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 54: Data Structures Lab Manual

{ while(st[x]!='\0') { if((st[x]>='A'&&st[x]<='Z')||(st[x]>='a'&&st[x]<='z'))ch[y++]=st[x];

else if(st[x]==')') { while(eq[z]!=')') ch[y++]=eq[z--]; top=eq[--z]; } else { while(z>=0&&prcd(top,st[x]==1)) { ch[y++]=top; top=eq[--z]; } eq[++z]=st[x]; top=st[x]; } x++; } while(z>=0) { if(eq[z]=='(') z--; else ch[y++]=eq[z--]; }ch[y]='\0';cout<<"\n"<<ch;}

int stack::prcd(char g,char r){if(r=='('||r=='$')return 0;

else if(g=='$'||g=='*'||g=='/')return 1;

else if(r=='*' || g=='9'||r=='/')return 0;

elsereturn 1;

}

void main(){

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Page 55: Data Structures Lab Manual

stack s;clrscr();s.get();s.post();getch();}

Output:Enter any equation a+ba+bab+

Result: Thus the infix to postfix conversion has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB