data structures programs

60
K S V KRISHNA SRIKANTH Page 1 :: Data Structures Programs Using C++ :: LIST OF PROGRAMS 1) STACKS USING ARRAYS 2) STACKS USING LINKED LISTS 3) QUEUES USING LINKED LISTS 4) CIRCULAR QUEUES USING ARRAYS 5) CIRCULAR QUEUES USING LINKED LISTS 6) DEQUEUES USING DOUBLE LINKED LISTS 7) EVALUATION OF POSTFIX EXPRESSION 8) CONVERSION OF INFIX TO POSTFIX 9) ADDITION & SUBTRACTION OF 2 SPARSE MATRICES 10) BINARY TREE TRAVERSALS USING RECURSION 11) BINARY SEARCH TREE OPERATIONS 12) POLYNOMIAL ADDITION & MULTIPLICATION 13) HEAP SORT 14) MERGE SORT 15) INSERTION SORT 16) BUBBLE SORT 17) SELECTION SORT 18) BINARY SEARCH 19) LINEAR SEARCH 20) QUICK SORT 21) BREADTH FIRST SEARCH 22) DEPTH FIRST SEARCH 23) CONVERSION OF INFIX TO PREFIX 24) REVERSING A LINKED LIST 25) LINKED LIST CREATED IN ASCENDING ORDER 26) PARANTHESIS MATCHING 27) TOWERS OF HANOI 28) USAGE OF DIFFERENT PARAMETERS 29) QUEUES USING ARRAYS 30) COMBINING TWO LINKED LISTS

Upload: girish-kumar-nistala

Post on 28-Nov-2014

1.296 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures Programs

K S V KRISHNA SRIKANTH Page 1

:: Data Structures Programs Using C++ ::

LIST OF PROGRAMS

1) STACKS USING ARRAYS

2) STACKS USING LINKED LISTS

3) QUEUES USING LINKED LISTS

4) CIRCULAR QUEUES USING ARRAYS

5) CIRCULAR QUEUES USING LINKED LISTS

6) DEQUEUES USING DOUBLE LINKED LISTS

7) EVALUATION OF POSTFIX EXPRESSION

8) CONVERSION OF INFIX TO POSTFIX

9) ADDITION & SUBTRACTION OF 2 SPARSE MATRICES

10) BINARY TREE TRAVERSALS USING RECURSION

11) BINARY SEARCH TREE OPERATIONS

12) POLYNOMIAL ADDITION & MULTIPLICATION

13) HEAP SORT

14) MERGE SORT

15) INSERTION SORT

16) BUBBLE SORT

17) SELECTION SORT

18) BINARY SEARCH

19) LINEAR SEARCH

20) QUICK SORT

21) BREADTH FIRST SEARCH

22) DEPTH FIRST SEARCH

23) CONVERSION OF INFIX TO PREFIX

24) REVERSING A LINKED LIST

25) LINKED LIST CREATED IN ASCENDING ORDER

26) PARANTHESIS MATCHING

27) TOWERS OF HANOI

28) USAGE OF DIFFERENT PARAMETERS

29) QUEUES USING ARRAYS 30) COMBINING TWO LINKED LISTS

Page 2: Data Structures Programs

K S V KRISHNA SRIKANTH Page 2

1) Stacks using arrays #include<iostream.h> #include<process.h> #include<conio.h> template<class T> class stack { T *s; int top,size; public: stack(int); ~stack(); void push(); void pop(); void display(); }; template<class T> stack<T>::stack(int k) { size=k; s=new T[size]; top=0; } template<class T> stack<T>::~stack() { delete s; } template<class T> void stack<T>::push() { if(top==size) cout<<"Stack Overflow!"<<endl; else { T x; cout<<"Enter the data value: "; cin>>x; s[top]=x; top++; } } template<class T> void stack<T>::pop() { if(top==0) cout<<endl<<"Stack Underflow"; else { top--; cout<<"\nThe pop element is: "<<s[top]; } } template<class T>

Page 3: Data Structures Programs

K S V KRISHNA SRIKANTH Page 3

void stack<T>::display() { if(top==0) cout<<endl<<"Stack is Empty!"; else { cout<<endl<<"The stack elements are: "; for(int i=0;i<top;i++) { cout<<s[i]<<" "; } } } void main() { int m; clrscr(); cout<<endl<<"Enter the size of array: "; cin>>m; stack <int> t1(m); while(1) { int ch; cout<<endl<<"*****STACK MENU*****"<<endl; cout<<"1.PUSH"<<endl; cout<<"2.POP"<<endl; cout<<"3.DISPLAY"<<endl; cout<<"4.EXIT"<<endl; cout<<"Enter Choice:"; cin>>ch; switch(ch) { case 1:t1.push(); break; case 2:t1.pop(); break; case 3:t1.display(); break; case 4:exit(0); break; } } } 2) Stacks using linked lists #include<iostream.h> #include<process.h> #include<conio.h> template<class T> class lstack { typedef struct node

Page 4: Data Structures Programs

K S V KRISHNA SRIKANTH Page 4

{ int data; struct node *link; }s; s *head,*tail,*p,*k; public: lstack(); void push(); void pop(); void display(); }; template<class T> lstack<T>::lstack() { head=tail=NULL; } template<class T> void lstack<T>::push() { T x; p=new s; cout<<endl<<"Enter data value:"; cin>>x; p->data=x; p->link=NULL; if(head==NULL) head=tail=p; else { tail->link=p; tail=p; } } template<class T> void lstack<T>::pop() { if(head==NULL) cout<<endl<<"Stack Underflow"; else { if(head->link!=NULL) { p=head; while(p->link!=NULL) { k=p; p=p->link; } tail=k; tail->link=NULL; cout<<endl<<"The pop element is "<<p->data; delete p; } else {

Page 5: Data Structures Programs

K S V KRISHNA SRIKANTH Page 5

p=head; head=tail=NULL; cout<<endl<<"The pop element is "<<p->data; delete p; } } } template<class T> void lstack<T>::display() { if(head==NULL) cout<<endl<<"Stack Empty"; else { cout<<endl<<"The Linked Stack elements are: "; for(p=head;p!=NULL;p=p->link) cout<<p->data<<" "; } } void main() { int ch,n; clrscr(); lstack <int> t; while(1) { cout<<endl<<"*****LINKED STACK MENU*****"<<endl; cout<<"1.PUSH"<<endl; cout<<"2.POP"<<endl; cout<<"3.DISPLAY"<<endl; cout<<"4.EXIT"<<endl; cout<<"Enter Choice:"; cin>>ch; switch(ch) { case 1:t.push(); break; case 2:t.pop(); break; case 3:t.display(); break; case 4:exit(0); break; } } } 3) Queues using linked lists #include<iostream.h> #include<process.h> #include<conio.h> template<class T> class lqueue {

Page 6: Data Structures Programs

K S V KRISHNA SRIKANTH Page 6

typedef struct node { int data; struct node *link; }s; s *head,*tail,*p,*k; public: lqueue(); void insertion(); void deletion(); void display(); }; template<class T> lqueue<T>::lqueue() { head=tail=NULL; } template<class T> void lqueue<T>::insertion() { T x; p=new s; cout<<"Enter data value:"; cin>>x; cout<<endl; p->data=x; p->link=NULL; if(head==NULL) head=tail=p; else { tail->link=p; tail=p; } } template<class T> void lqueue<T>::deletion() { if(head==NULL) cout<<"Queue Underflow"<<endl; else { p=head; head=head->link; cout<<"The deleted element is "<<p->data; delete p; } cout<<endl; } template<class T> void lqueue<T>::display() { if(head==NULL) cout<<"Queue Empty"<<endl; else

Page 7: Data Structures Programs

K S V KRISHNA SRIKANTH Page 7

{ cout<<"The Linked Queue elements are "; for(p=head;p!=NULL;p=p->link) cout<<p->data<<" "; cout<<endl; } } void main() { lqueue <int> t1; clrscr(); while(1) { int ch; cout<<"****LINKED QUEUE MENU****"<<endl; cout<<"1. Insertion"<<endl; cout<<"2. Deletion"<<endl; cout<<"3. Display"<<endl; cout<<"4. Exit"<<endl; cout<<"Enter choice:"; cin>>ch; cout<<endl; switch(ch) { case 1: t1.insertion(); break; case 2: t1.deletion(); break; case 3: t1.display(); break; case 4: exit(0); break; default: cout<<"Enter correct choice..."; break; } } } 4) Circular Queues using arrays #include<iostream.h> #include<process.h> #include<conio.h> template<class T> class cqueue { T *v; int size,front,rear; public: cqueue(int); ~cqueue(); void insertion(); void deletion(); void display(); }; template<class T> cqueue<T>::cqueue(int k) { size=k;

Page 8: Data Structures Programs

K S V KRISHNA SRIKANTH Page 8

v=new T[size]; front=rear=-1; } template<class T> cqueue<T>::~cqueue() { delete v; } template<class T> void cqueue<T>::insertion() { T x; if(front==(rear+1)%size) cout<<endl<<"Queue Overflow"<<endl; else { cout<<endl<<"Enter a value: "; cin>>x; if(front==-1) front=rear=0; else rear=(rear+1)%size; v[rear]=x; } } template<class T> void cqueue<T>::deletion() { if(front==-1) cout<<endl<<"Queue Empty"<<endl; else { cout<<endl<<"Deleted element is "<<v[front]<<endl; if(front==rear) front=rear=-1; else front=(front+1)%size; } } template<class T> void cqueue<T>::display() { if(front==-1) cout<<endl<<"Queue Empty"<<endl; else { cout<<endl<<"Elements of circular queue: "; for(int i=front;i!=rear;i=(i+1)%size) { cout<<v[i]<<" "; } cout<<v[rear]<<" "; cout<<endl; }

Page 9: Data Structures Programs

K S V KRISHNA SRIKANTH Page 9

} void main() { cqueue <int> t(5); int ch; clrscr(); do { cout<<endl<<"##Circular Queue Menu##"<<endl; cout<<"1. Insertion"<<endl; cout<<"2. Deletion"<<endl; cout<<"3. Display"<<endl; cout<<"4. Exit"<<endl; cout<<"Enter choice: "; cin>>ch; switch(ch) { case 1: t.insertion(); break; case 2: t.deletion(); break; case 3: t.display(); break; case 4: exit(0); break; default: cout<<"Enter valid choice..."; break; } } while(ch<=4); } 5) Circular Queues using linked lists #include<iostream.h> #include<process.h> #include<conio.h> template <class T> class cqueue { typedef struct node { int data; struct node *link; }s; s *front,*rear,*p,*k; public: cqueue(); void insertion(); void deletion(); void display(); }; template<class T> cqueue<T>::cqueue() { front=rear=NULL; } template<class T> void cqueue<T>::insertion()

Page 10: Data Structures Programs

K S V KRISHNA SRIKANTH Page 10

{ T x; p=new s; cout<<endl<<"Enter data value:"; cin>>x; p->data=x; p->link=NULL; if(front==NULL) front=rear=p; else { rear->link=p; p->link=front; rear=p; } } template<class T> void cqueue<T>::deletion() { if((front==NULL)&&(rear==NULL)) { cout<<endl<<"Queue Empty"; } else { if(front==rear) { cout<<endl<<"The deleted element is "<<front->data; delete front; front=NULL; rear=NULL; } else { p=front; front=front->link; rear->link=front; cout<<endl<<"The deleted element is "<<p->data; delete p; } } } template<class T> void cqueue<T>::display() { if(front==NULL) cout<<endl<<"Queue Empty"; else { cout<<endl<<"The elements are: "; for(p=front;p->link!=front;p=p->link) { cout<<p->data<<" ";} cout<<p->data<<" "; cout<<endl;

Page 11: Data Structures Programs

K S V KRISHNA SRIKANTH Page 11

} } void main() { int ch; clrscr(); cqueue <int> t1; while(1) { cout<<endl<<"****CIRCULAR QUEUE MENU****"<<endl; cout<<"1. Insertion"<<endl; cout<<"2. Deletion"<<endl; cout<<"3. Display"<<endl; cout<<"4. Exit"<<endl; cout<<"Enter choice:"; cin>>ch; switch(ch) { case 1: t1.insertion(); break; case 2: t1.deletion(); break; case 3: t1.display(); break; case 4: exit(0); break; default: cout<<"Enter valid choice..."; break; } } } 6) Dequeue using double linked lists #include<iostream.h> #include<conio.h> #inlcude<process.h> template<class T> class dequeue { typedef struct nnode { int data; node *left,*right; }s; s *front,*rear,*p,*q,*k; public: dequeue() { front=rear=NULL; } void insbegin(); void insend(); void delbegin(); void delend(); void display(); };

Page 12: Data Structures Programs

K S V KRISHNA SRIKANTH Page 12

template<class T> void dequeue<T>::display() { cout<<"The Dequeue elements are: "; for(p=front;p!=NULL;p=p->right) cout<<p->data<<" "; cout<<endl; } template<class T> void dequeue<T>::insbegin() { T x; q=new s; cout<<"Enter data value: "; cin>>x; q->data=x; q->left=q->right=NULL; if(front==NULL) front=rear=q; else { q->right=front; front->left=q; front=q; } } template<class T> void dequeue<T>::insend() { T x; q=new s; cout<<"Enter data value: "; cin>>x; q->data=x; q->left=q->right=NULL; if(front==NULL) front=rear=q; else { rear->right=q; q->left=rear; rear=q; } } template<class T> void dequeue<T>::delbegin() { p=front; front=front->right; front->left=NULL; cout<<"Deleted Element is "<<p->data; delete p; }

Page 13: Data Structures Programs

K S V KRISHNA SRIKANTH Page 13

template<class T> void dequeue<T>::delend() { p=rear; rear=rear->left; rear->right=NULL; cout<<"Deleted Element is "<<p->data; delete p; } void main() { int ch; clrscr(); dequeue <int> t1; while(1) { cout<<"**Dequeue Menu**"<<endl; cout<<"1.Insertion at front"<<endl; cout<<"2.Insertion at rear"<<endl; cout<<"3.Deletion at front"<<endl; cout<<"4.Deletion at rear"<<endl; cout<<"5.Display"<<endl; cout<<"6.Exit"<<endl; cout<<"Enter choice: "; cin>>ch; switch(ch) { case 1: t1.insbegin(); break; case 2: t1.insend(); break; case 3: t1.delbegin(); break; case 4: t1.delend(); break; case 5: t1.display(); break; case 6: exit(0); break; default: cout<<"Enter valid Option"; break; } } } 7) Evaluation of Postfix Expression (POSTFIXE.cpp) #include<iostream.h> #include<process.h> #include<conio.h> #inlcude<math.h> #inlcude<string.h> class postfix

Page 14: Data Structures Programs

K S V KRISHNA SRIKANTH Page 14

{ private: int stack[50],len,top; char post[50]; public: postfix() { top=-1; } void push(int); int pop(); int pfix(); }; int postfix::pfix() { int a,b,c,temp; cout<<"Enter postfix expression:"; cin>>post; len=strlen(post); post[len]='#'; for(i=0;post[i]!='#';i++) { if(post[i]<='9'&&post[i]>='0') push(post[i]-48); else { a=pop(); b=pop(); switch(post[i]) { case '+':temp=b+a; 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(temp); } } return(pop()); } void postfix::push(int x) { stack[++top]=x; } int postfix::pop() {

Page 15: Data Structures Programs

K S V KRISHNA SRIKANTH Page 15

int x=stacl[top]; top--; return x; } void main() { int x; postfix b; clrscr(); x=b.pfix(); cout<<endl<<"Result of postfix expression is "<<x; getch(); } OUTPUT 1:

Enter postfix expression: 123*+4-

Result of postfix expression is 3

OUTPUT 2:

Enter postfix expression: 56*42/+47+-

Result of postfix expression is 21

8) Conversion of Infix to Postfix #include<iostream.h> #include<conio.h> #include<process.h> char stack[30],postfix[30],infix[30]; class intopost { public: int top; public: intopost() { top=-1; } int pri(char); void push(char); char stacktop(); int isalnum(char); char pop(); void conintopost(char[],char[]); }; int intopost::pri(char x) { int value; switch(x) { case ')':value=0;

Page 16: Data Structures Programs

K S V KRISHNA SRIKANTH Page 16

break; case '+':value=1; break; case '-':value=1; break; case '*':value=2; break; case '/':value=2; break; case '^':value=3; break; case '(':value=4; break; } return value; } void intopost::push(char x) { top=top+1; stack[top]=x; } char intopost::stacktop() { return stack[top]; } int intopost::isalnum(char x) { return((x>='0'&&x<='9')||(x>='a'&&x<='z')||(x>='A'&&x<='Z')); } char intopost::pop() { return stack[top--]; } void intopost::conintopost(char infix[],char postfix[]) { int i,j=0; char c,pc; for(i=0;(c=infix[i])!='\o';i++) { if(isalnum(c)) postfix[j++]=c; else { while(top!=-1 && (pri(stacktop())>=pri(c))) { if(stacktop()=='('&&c!=')') break; if(stacktop()=='('&&c==')') { pop(); break; } pc=pop(); if(pc!='(')

Page 17: Data Structures Programs

K S V KRISHNA SRIKANTH Page 17

postfix[j++]=pc; else break; } if(c!=')') push(c); } } while(top!=-1) postfix[j++]=pop(); postfix[j]='\o'; } void main() { clrscr(); intopost t1; cout<<"Enter infix expression:"; cin>>infix; t1.conintopost(infix,postfix); cout<<endl<<"Postfix Expression is "; cout<<postfix; getch(); } OUTPUT 1:

Enter infix expression: (a*b)+(c/d)-(g+h)

Postfix expression is ab*cd/+gh+-

OUTPUT 2:

Enter infix expression: (a+(b*c-(d/e^f)*g)*h)

Postfix expression is abc*def^/g*-h*+

9) Addition & Subtraction of 2 sparse matrices #include<iostream.h> #include<conio.h> #include<process.h> int main() { clrscr(); int sp1[10][3],sp2[10][3],sp3[10][3]; int i,j,m,n,p,q,t1,t2,d,s,element; int sum[10][3],diff[10][3]; cout<<"Enter the number of rows and columns\n"; cin>>m; cin>>n; t1=t2=0; cout<<" Enter the first matrix("<<m<<"*"<<n<<")\n"; for(i=1;i<=m;i++)

Page 18: Data Structures Programs

K S V KRISHNA SRIKANTH Page 18

{ for(j=1;j<=n;j++) { cin>>element; if(element!=0) { t1=t1+1; sp1[t1][1]=i; sp1[t1][2]=j; sp1[t1][3]=element; } } } sp1[0][1]=m; sp1[0][2]=n; sp1[0][3]=t1; cout<<"Enter the Second Matrix("<<m<<"*"<<n<<"):\n"; for(i=1;i<=m;i++) { for(j=1;j<=m;j++) { cin>>element; if(element!=0) { t2=t2+1; sp2[t2][1]=i; sp2[t2][2]=j; sp2[t2][3]=element; } } } sp2[0][1]=m; sp2[0][2]=n; sp2[0][3]=t2; cout<<"\n The first sparse matrix is\n Row \t column\t element"; for(i=0;i<=t1;i++) { cout<<"\n"<<sp1[i][1]<<"\t"<<sp1[i][2]<<"\t"<<sp1[i][3]<<"\n"; } cout<<"The second sparse matrix is:"; for(i=0;i<=t2;i++) { cout<<"\n"<<sp2[i][1]<<"\t"<<sp2[i][2]<<"\t"<<sp2[i][3]<<"\n"; } i=j=s=d=1; while((i<=t1)&&(j<=t2)) { if(sp1[i][1]=sp2[j][1]) { if(sp1[i][2]=sp1[j][2]) { sum[s][1]=diff[d][1]=sp1[i][1]; sum[s][2]=diff[d][2]=sp1[j][2];

Page 19: Data Structures Programs

K S V KRISHNA SRIKANTH Page 19

sum[s][3]=sp1[i][3]+sp2[j][3]; diff[d][3]=sp1[i][3]-sp2[j][3]; i++; j++; if(sum[s][3]!=0) s++; if(diff[d][3]!=0) d++; } else { if(sp1[i][2]<sp2[3][2]) { sum[s][1]=diff[d][1]=sp1[i][1]; sum[s][2]=diff[d][2]=sp1[i][2]; sum[s][3]=diff[d][3]=sp1[i][3]; i++; s++; d++; } else { sum[s][1]=diff[d][1]=sp2[j][1]; sum[s][2]=diff[d][2]=sp2[j][2]; sum[s][3]=sp2[j][3]; diff[d][3]=0-sp2[j][3]; j++; d++; s+1; } } } else { if(sp1[i][1]<sp2[3][1]) { sum[s][i]=diff[d][1]=sp1[i][1]; sum[s][2]=diff[d][2]=sp1[i][2]; sum[s][3]=diff[d][3]=sp1[i][3]; i++; d++; s++; } else { sum[s][1]=diff[d][1]=sp2[j][1]; sum[s][2]=diff[d][2]=sp2[j][2]; sum[s][3]=sp2[j][3]; diff[d][3]=0-sp2[j][3]; j++; s++; d++; } }

Page 20: Data Structures Programs

K S V KRISHNA SRIKANTH Page 20

} if(i<=t1) { for(p=1;p<=t1;p++) { sum[s][1]=diff[d][1]=sp1[p][1]; sum[s][2]=diff[d][2]=sp1[p][2]; sum[s][3]=diff[d][2]=sp1[p][3]; s++; d++; } } else if(j<=t2) { for(p=j;p<=t2;p++) { sum[s][1]=diff[d][1]=sp2[p][2]; sum[s][2]=diff[d][2]=sp2[p][2]; sum[s][3]=sp2[p][3]; diff[d][3]=0-sp2[j][3]; s++; d++; } } sum[0][1]=diff[0][1]=m; sum[0][2]=diff[0][2]=n; sum[0][3]=s-1; diff[0][3]=d-1; cout<<"the sum of two sparse matrices is:"; for(i=0;i<s;i++) { cout<<"\n"<<sum[i][1]<<"\t"<<sum[i][2]<<"\t"<<sum[i][3]<<"\n"; } cout<<"The difference of the two matrices is :"; for(i=0;i<d;i++) { cout<<"\n"<<diff[i][1]<<"\t"<<diff[i][2]<<"\t"<<diff[i][3]<<"\n"; } getch(); return 0; } OUTPUT: Enter the number of rows and columns: 3 3

Enter the first matrix (3*3)

0 0 0 0 2 0 0 0 6 Enter the Second Matrix (3*3):

Page 21: Data Structures Programs

K S V KRISHNA SRIKANTH Page 21

0 0 1 0 0 3 0 0 4

Enter the number of rows and columns: 3 3

The first sparse matrix is

Row Column Element

3 3 2

2 2 2

3 3 6

The second sparse matrix is:

3 3 3

1 3 1

2 3 3

3 3 4

The sum of two sparse matrices is:

3 3 3

1 2 3

2 3 9

3 3 4

The difference of the two matrices is:

3 3 3

1 2 1

2 3 3

3 3 -4

10) Binary tree traversals using recursion #include<iostream.h> #include<conio.h> #include<process.h> template<class T> class bstree { typedef struct node

Page 22: Data Structures Programs

K S V KRISHNA SRIKANTH Page 22

{ int data; struct node *left,*right; }s; s *head,*prev,*p,*c,*k,*q; int t; public:bstree(); void creation(); void display(); void insertion(); void deletion(); void preorder(s *p) { if(p!=NULL) { cout<<p->data<<" "; preorder(p->left); preorder(p->right); } else return; } void inorder(s *p) { if(p!=NULL) { inorder(p->left); cout<<p->data<<" "; inorder(p->right); } else return; } void postorder(s *p) { if(p!=NULL) { postorder(p->left); postorder(p->right); cout<<p->data<<" "; } else return; } }; template<class T> bstree<T>::bstree() { head=prev=c=NULL; } template<class T> void bstree<T>::creation()

Page 23: Data Structures Programs

K S V KRISHNA SRIKANTH Page 23

{ int n; T x; cout<<"Enter the n value: "; cin>>n; cout<<endl; for(int i=1;i<=n;i++) { p=new s; cout<<"Enter the data value:"; cin>>x; p->data=x; p->left=p->right=NULL; if(head==NULL) head=p; else c=head; while(c!=NULL) { prev=c; if(c->data<p->data) c=c->right; else c=c->left; } if(prev!=NULL) { if(prev->data<p->data) prev->right=p; else prev->left=p; } } } template<class T> void bstree<T>::insertion() { T x; q=new s; cout<<endl<<"Enter the data value: "; cin>>x; q->data=x; q->left=q->right=NULL; c=head; while(c!=NULL) { prev=c; if(c->data<q->data) c=c->right; else c=c->left; } if(prev!=NULL)

Page 24: Data Structures Programs

K S V KRISHNA SRIKANTH Page 24

{ if(prev->data<q->data) prev->right=q; else prev->left=q; } } template<class T> void bstree<T>::display() { int ch; cout<<endl<<"The tree elements:\n "; cout<<"\n---------------------------\n"; cout<<" DISPLAY MENU \n"; cout<<"----------------------------\n"; cout<<" 1. Pre-Order \n"; cout<<" 2. In-Order \n"; cout<<" 3. Post-Order \n"; cout<<"----------------------------\n"; do { cout<<endl<<"Enter the display choice: "; cin>>ch; switch(ch) { case 1: cout<<endl<<"Preorder Display: "; preorder(head); cout<<endl; break; case 2: cout<<endl<<"Inorder Display: "; inorder(head); cout<<endl; break; case 3: cout<<endl<<"Postorder Display: "; postorder(head); cout<<endl; break; } }while(ch<=3); } template<class T> void bstree<T>::deletion() { if(head==NULL) cout<<endl<<"Deletion not possible"; else { int x; cout<<endl<<"Enter the delete element: "; cin>>x; if(head->data==x)

Page 25: Data Structures Programs

K S V KRISHNA SRIKANTH Page 25

cout<<endl<<"The root element deletion not possible "; else { c=head; while(c->data!=x) { prev=c; if(x<c->data) c=c->left; else c=c->right; } if(c->left==NULL && c->right==NULL) { if(prev->left==c) prev->left=NULL; else prev->right=NULL; } else if(c->left!=NULL && c->right==NULL) prev->left=c->left; else if(c->left==NULL && c->right!=NULL) { if(prev->left==c) prev->left=c->right; else prev->right=c->right; } else if(c->left!=NULL && c->right!=NULL) { while(c->right!=NULL) { c->data=c->right->data; prev=c; c=c->right; } prev->right=NULL; } } } } void main() { int ch; clrscr(); bstree <int> t1; t1.creation(); while(1) {

Page 26: Data Structures Programs

K S V KRISHNA SRIKANTH Page 26

cout<<"\n--------------------- \n"; cout<<" MAIN MENU \n"; cout<<"----------------------\n"; cout<<" 1.Insertion \n"; cout<<" 2.Deletion \n"; cout<<" 3.Display \n"; cout<<"-----------------------\n"; cout<<endl<<"Enter the choice: "; cin>>ch; switch(ch) { case 1: t1.insertion();break; case 2: t1.deletion();break; case 3: t1.display();break; default:exit(0); } } } 11) Binary Search Tree Operations #include<iostream.h> #include<conio.h> #include<process.h> #define TRUE 1 #define FALSE 0 class bstree { private: struct node { node *lc; int data; node *rc; }*root; public: bstree { root=NULL; } void buildtree(int num) { insert(&root,num); } static void insert(node **sr,int); static void search(node **sr,int num,node **par,node **x,int *found); void remove(int num) { rem(&root,num); } static void rem(node **sr,int num); void display() { inorder(root); } static void inorder(node *sr); ~bstree() { del(root);

Page 27: Data Structures Programs

K S V KRISHNA SRIKANTH Page 27

} static void del(node *sr) { if(sr!=NULL) { del(sr->lc); del(sr->rc); } delete sr; } }; void bstree::insert(node **sr,int num) { if(*sr==NULL) { *sr=new node; (*sr)->lc=NULL; (*sr)->data=num; (*sr)->rc=NULL; } else { if(num<(*sr)->data) insert(&((*sr)->lc),num); else insert(&((*sr)->rc),num); } } void bstree::rem(node **sr,int num) { int found; node *parent,*x,*xsucc; if(*sr==NULL) { cout<<"Tree is Empty"; return; } parent=x=NULL; search(sr,num,&parent,&x,&found); if(found==FALSE) { cout<<endl<<"Data to be deleted, not found"; return; } if(x->lc!=NULL && x->rc!=NULL) { parent=x; xsucc=x->rc; while(xsucc->lc!=NULL) { parent=xsucc; xsucc=xsucc->lc; } x->data=xsucc->data; x=xsucc; } if(x->lc==NULL && x->rc!=NULL) { if(parent->lc==x) parent->rc=x->rc;

Page 28: Data Structures Programs

K S V KRISHNA SRIKANTH Page 28

else parent->lc=x->rc; delete x; return; } if(x->lc!=NULL && x->rc==NULL) { if(parent->lc==x) parent->lc=x->lc; else parent->rc=x->lc; delete x; return; } } void bstree::search(node **sr,int num,node **par,node **x,int *found) { node *q; q=*sr; *found=FALSE; *par=NULL; while(q!=NULL) { if(q->data==num) { *found=TRUE; *x=q; return; } *par=q; if(q->data>num) q=q->lc; else q=q->rc; } } void bstree::inorder(node *sr) { if(sr!=NULL) { inorder(sr->lc); cout<<sr->data<<" "; inorder(sr->rc); } } void main() { bstree bst; int req,i=0,num,a[]={11,9,13,8,10,12,14,15,7}; while(i<=8) { bst.buildtree(a[i]); i++;

Page 29: Data Structures Programs

K S V KRISHNA SRIKANTH Page 29

} cout<<"BST before deletion: \n"; bst.display(); bst.remove(10); cout<<"BST after deletion: \n"; bst.display(); cout<<"BST before deletion: \n"; bst.remove(13); cout<<"BST after deletion: \n"; bst.display(); } OUTPUT:

Binary search tree before deletion:

7 8 9 10 11 12 13 14 15

Binary search tree after deletion:

7 8 9 11 12 13 14 15

Binary search tree after deletion:

7 8 9 11 12 13 15

Binary search tree after deletion:

7 9 11 12 13 15

Binary search tree after deletion:

7 9 11 12 15

12) Polynomial Addition & Multiplication #include<iostream.h> #include<conio.h> #define n 100 class poly { private: int a[n],b[n],add[n],mul[n],p,q,at; public: void init(); void input(); void process(); void display(); }; void poly :: init() { int i; for(i=0;i<n;i++)

Page 30: Data Structures Programs

K S V KRISHNA SRIKANTH Page 30

a[i] = b[i] = add[i] = mul[i] = 0; } void poly :: input() { int i; cout<<"\nEnter Degree of First Polynomial: "; cin>>p; cout<<"\nEnter Degree of Second Polynomial: "; cin>>q; cout<<"\nEnter Values of First Polynomial\n"; for(i=0;i<=p;i++) { cout<<"\nEnter x^"<<i<<" th Coefficient: "; cin>>a[i]; } cout<<"\nEnter Values of Second Polynomial\n"; for(i=0;i<=q;i++) { cout<<"\nEnter x^"<<i<<" th Coefficient: "; cin>>b[i]; } } void poly :: process() { int i, j; if(p>q) at = p; else at = q; for(i=0;i<=at;i++) add[i]=a[i]+b[i]; for(i=0;i<=p;i++) for(j=0;j<=q;j++) mul[i+j]+=a[i]*b[j]; } void poly :: display() { int i; cout<<"\n\nAddition of 2 Polynomial Expressions is\n\n"; for(i=at;i>=0;i--) cout<<add[i]<<"x^"<<i<<"+"; cout<<"\n\nMultiplication of 2 Polynomial Expressions is\n\n"; for(i=p+q;i>=0;i--) cout<<mul[i]<<"x^"<< i <<"+"; } void main() { poly ob; clrscr(); ob.init();

Page 31: Data Structures Programs

K S V KRISHNA SRIKANTH Page 31

ob.input(); ob.process(); ob.display(); getch(); } OUTPUT: Enter Degree of First Polynomial: 3 Enter Degree of Second Polynomial: 3 Enter Values of First Polynomial Enter x^0 th Coefficient: 9 Enter x^1 th Coefficient: 8 Enter x^2 th Coefficient: 7 Enter x^3 th Coefficient: 6 Enter Values of Second Polynomial Enter x^0 th Coefficient: 5 Enter x^1 th Coefficient: 4 Enter x^2 th Coefficient: 3 Enter x^3 th Coefficient: 2 Addition of 2 Polynomial Expressions is 8x^3+10x^2+12x^1+14x^0+ Multiplication of 2 Polynomial Expressions is 12x^6+32x^5+61x^4+100x^3+94x^2+76x^1+45x^0+ 13) Heap Sort #include<iostream.h> #include<conio.h> class hsort { private: int *a,n; public: hsort(int x) { n=x; a=new int[n]; } void getdata() {

Page 32: Data Structures Programs

K S V KRISHNA SRIKANTH Page 32

cout<<"\n"; for(int i=0;i<n;i++) { cout<<"\tEnter "<<i<<" th value: "; cin>>a[i]; } } void heapsort(int l) { for(int i=(l-1)/2;i>=0;i--) formheap(i,l); for(i=l;i>0;i--) { int t=a[0]; a[0]=a[i]; a[i]=t; formheap(0,i-1); } } void formheap(int i,int n) { int j=2*i+1; while(j<=n) { if(j+1<=n && a[j+1] > a[(j-1)/2]) if(a[j+1] >a[j]) j++; if(a[j] > a[(j-1) / 2]) { int t=a[j]; a[j]=a[(j-1)/2]; a[(j-1)/2]=t; } j=2*j+1; } } void display() { for(int i=0;i<n;i++) cout<<a[i]<<" "; } }; void main() { int n; clrscr(); cout<<"Enter n value:"; cin>>n; hsort t(n); t.getdata(); cout<<"\n\nElements before sorting:\n\n\t"; t.display(); t.heapsort(n-1); cout<<"\n\nElements after sorting are:\n\n\t";

Page 33: Data Structures Programs

K S V KRISHNA SRIKANTH Page 33

t.display(); } OUTPUT:

Enter n value: 10 Enter 0 th value: 90 Enter 1 th value: 57 Enter 2 th value: 25 Enter 3 th value: 13 Enter 4 th value: 11 Enter 5 th value: 9 Enter 6 th value: 17 Enter 7 th value: 1 Enter 8 th value: 2 Enter 9 th value: 3 Elements before sorting: 90 57 25 13 11 9 17 1 2 3 Elements after sorting are: 1 2 3 9 11 13 17 25 57 90 14) Merge Sort #include<iostream.h> #include<conio.h> class msort { int a[50],n; public: msort(int); void getdata(); void sort(int,int); void display(); void merge(int,int,int); }; msort::msort(int size) { n=size; } void msort::getdata() { int m; cout<<endl; for(m=0;m<n;m++) { cout<<"\tEnter "<<m<<" th value:"; cin>>a[m]; } } void msort::display()

Page 34: Data Structures Programs

K S V KRISHNA SRIKANTH Page 34

{ int m; for(m=0;m<n;m++) cout<<a[m]<<" "; } void msort::sort(int left,int right) { int mid; if(left<right) { mid=(left+right)/2; sort(left,mid); sort(mid+1,right); merge(left,mid,right); } else return; } void msort::merge(int first,int mid,int last) { int h,i,j,b[50],k; h=first; i=first; j=mid+1; while((h<=mid)&&(j<=last)) { if(a[h]<=a[j]) { b[i]=a[h]; h++; } else { b[i]=a[j]; j++; } i++; } if(h>mid) { for(k=j;k<=last;k++) { b[i]=a[k]; i++; } } else { for(k=h;k<=mid;k++) { b[i]=a[k]; i++;

Page 35: Data Structures Programs

K S V KRISHNA SRIKANTH Page 35

} } for(k=first;k<=last;k++) a[k]=b[k]; } void main() { int m; clrscr(); cout<<"Enter the array size:"; cin>>m; msort t(m); t.getdata(); cout<<"\n\nElements before sorting: \n\n\t"; t.display(); t.sort(0,m-1); cout<<"\n\nElements after sorting: \n\n\t"; t.display(); } OUTPUT: Enter the array size: 10 Enter 0 th value: 23 Enter 1 th value: 1 Enter 2 th value: 65 Enter 3 th value: 54 Enter 4 th value: 21 Enter 5 th value: 27 Enter 6 th value: 12 Enter 7 th value: 15 Enter 8 th value: 41 Enter 9 th value: 96 Elements before sorting: 23 1 65 54 21 27 12 15 41 96 Elements after sorting: 1 12 15 21 23 41 54 27 65 96 15) Insertion Sort #include<iostream.h> #include<conio.h> class isort { private: int array[100],n,i,j,temp; public: void init(); void sort();

Page 36: Data Structures Programs

K S V KRISHNA SRIKANTH Page 36

}; void isort::init() { cout<<"Enter the size of the array: "; cin>>n; cout<<"\nEnter "<<n<<" numbers\n"; for(i=0;i<n;i++) cin>>array[i]; } void isort::sort() { for(i=0;i<n;i++) { j=i; while((j>0)&&(array[j-1]>array[j])) { temp=array[j]; array[j]=array[j-1]; array[j-1]=temp; j--; } } cout<<"\nArray is sorted in ascending order:\n\n"; for(i=0;i<n;i++) cout<<array[i]<<" "; } void main() { clrscr(); isort b; b.init(); b.sort(); getch(); } 16) Bubble Sort #include<iostream.h> #include<conio.h> class bsort { private: int array[100],n,i,j,temp; public: void init(); void sort(); }; void bsort::init() { cout<<"Enter the size of the array: "; cin>>n;

Page 37: Data Structures Programs

K S V KRISHNA SRIKANTH Page 37

cout<<"Enter "<<n<<" numbers\n"; for(i=0;i<n;i++) cin>>array[i]; } void bsort::sort() { for(i=0;i<n;i++) { for(j=0;j<n-1;j++) if(array[j]>array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } cout<<"\nArray is sorted in ascending order:\n\n\t"; for(i=0;i<n;i++) cout<<array[i]<<" "; } void main() { clrscr(); bsort b; b.init(); b.sort(); getch(); } OUTPUT: Enter the size of the array: 10 Enter 10 numbers 2 3 58 95 45 12 48 23 123 236 Array is sorted in ascending order: 2 3 12 23 45 48 58 95 123 236 17) Selection Sort #include<iostream.h> #include<conio.h> class ssort {

Page 38: Data Structures Programs

K S V KRISHNA SRIKANTH Page 38

private: int array[100],n,i,j,temp,min,x; public: void init(); void sort(); }; void ssort::init() { cout<<"Enter the size of the array: "; cin>>n; cout<<"\nEnter "<<n<<" numbers\n"; for(i=0;i<n;i++) cin>>array[i]; } void ssort::sort() { for(i=0;i<n-1;i++) { x=i; min=array[i]; for(j=i+1;j<n;j++) { if(min>array[j]) { min=array[j]; x=j; } } temp=array[i]; array[i]=array[x]; array[x]=temp; } cout<<"\nArray is sorted in ascending order:\n\n"; for(i=0;i<n;i++) cout<<array[i]<<" "; } void main() { clrscr(); ssort b; b.init(); b.sort(); getch(); } OUTPUT: Enter the size of the array: 10 Enter 10 numbers 1 5

Page 39: Data Structures Programs

K S V KRISHNA SRIKANTH Page 39

52 32 10 85 41 36 25 12 Array is sorted in ascending order:

1 5 10 12 25 32 36 41 52 85 18) Binary Search #include<iostream.h> #include<conio.h> #include<process.h> class bsearch { private: int ar[100],beg,mid,end,i,n,s; public: void search(); }; void bsearch::search() { cout<<"Enter the size of array: "; cin>>n; cout<<"\nEnter "<<n<<" numbers in ascending order: \n"; for(i=0;i<n;i++) cin>>ar[i]; beg=0; end=n-1; cout<<"\nEnter a number to search: "; cin>>s; while(beg<=end) { mid=(beg+end)/2; if(ar[mid]==s) { cout<<"\nItem "<<s<<" found at position "<<(mid+1); getch(); exit(0); } if(s>ar[mid]) beg=mid+1; else end=mid-1; } cout<<"\nSorry! "<<s<<" is not found."; getch(); }

Page 40: Data Structures Programs

K S V KRISHNA SRIKANTH Page 40

void main() { clrscr(); bsearch b; b.search(); } OUTPUT 1: Enter the size of array: 10 Enter 10 numbers in ascending order: 1 3 7 14 15 17 24 31 32 56 Enter a number to search: 15 Item 15 found at position 5 OUPTUT 2: Enter the size of array: 10 Enter 10 numbers in ascending order: 1 3 7 14 15 17 24 31 32 56 Enter a number to search: 51 Sorry! 51 is not found. 19) Linear Search #include<iostream.h> #include<conio.h> class lsearch { private: int c,a[10],i,n,m; public:

Page 41: Data Structures Programs

K S V KRISHNA SRIKANTH Page 41

lsearch(); void init(); void search(); }; lsearch::lsearch() { c=0; } void lsearch::init() { cout<<"Enter the size of an array: "; cin>>n; cout<<"\nEnter the elements of the array: \n"; for(i=0;i<=n-1;i++){ cin>>a[i]; } cout<<"\nThe elements of an array are: "; for(i=0;i<=n-1;i++){ cout<<a[i]<<" "; } } void lsearch::search() { cout<<"\n\nEnter the number to be searched: "; cin>>m; for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) cout<<"\nThe number "<<m<< " is not in the list"; else cout<<"\nThe number "<<m<< " is found"; return 0; } int main() { clrscr(); lsearch l; l.init(); l.search(); } OUTPUT 1: Enter the size of an array: 8 Enter the elements of the array:

Page 42: Data Structures Programs

K S V KRISHNA SRIKANTH Page 42

1 13 2 15 24 38 27 26 The elements of an array are: 1 13 2 15 24 38 27 26 Enter the number to be searched: 24 The number 24 is found OUTPUT 2: Enter the size of an array: 8 Enter the elements of the array: 1 13 2 15 24 38 27 26 The elements of an array are: 1 13 2 15 24 38 27 26 Enter the number to be searched: 45 The number 45 is not in the list 20) Quick Sort #include<iostream.h> #include<process.h> #include<conio.h> void qsort(int numbers[], int left, int right); int numbers[150]; int main() { int i,n; clrscr(); cout<<"How many numbers you want to sort: "; cin>>n; cout<<"\nEnter "<<n<<" numbers:\n"; for (i = 0; i<n; i++) cin>>numbers[i]; //perform quick sort on array qsort(numbers,0,n-1); cout<<"\nSorted Numbers are: \n"; for (i = 0; i<n; i++) cout<<numbers[i]<<" ";

Page 43: Data Structures Programs

K S V KRISHNA SRIKANTH Page 43

getch(); return(0); } // Function to sort void qsort(int numbers[], int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = numbers[left]; while (left < right) { while ((numbers[right] >= pivot) && (left < right)) right--; if (left != right)

{ numbers[left] = numbers[right]; left++; } while ((numbers[left] <= pivot) && (left < right)) left++; if (left != right) { numbers[right] = numbers[left]; right--; } } numbers[left] = pivot; pivot = left; left = l_hold; right = r_hold; if (left < pivot) qsort(numbers, left, pivot-1); if (right > pivot) qsort(numbers, pivot+1, right); } 21) BREADTH FIRST SEARCH #include<iostream.h> #include<conio.h> #define MAX 20 class breadth { private: int a[MAX][MAX],visited[MAX],queue[50]; int n, front, rear; public: void init(); void input(); void bfs(); };

Page 44: Data Structures Programs

K S V KRISHNA SRIKANTH Page 44

void breadth::init() { int i,j; for(i=0;i<MAX;i++) { visited[i]=0; for(j=0;j<MAX;j++) a[i][j]=0; } front = rear = - 1; } void breadth::input() { int i,j; cout<<"\nENTER NUMBER OF NODES IN A GRAPH: "; cin>>n; cout<<"\nENTER ADJACENCY MATRIX FOR A GRAPH:\n \n"; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>a[i][j]; } void breadth::bfs() { int i,start; cout<<"\nSTARTING NODE FOR BFS TRAVERSING: "; cin>>start; cout<<"\nBREADTH FIRST SEARCH TRAVERSING IS: \n\n \t"; cout<<start; visited[start] = 1; rear++; front++; queue[rear]=start; while(front<=rear) { start=queue[front]; front++; for(i=1;i<=n;i++) { if(a[start][i]==1 && visited[i]==0) { cout<<" -> "<<i; visited[i]=1; rear++; queue[rear]=i; } } } } void main ( ) { breadth ob;

Page 45: Data Structures Programs

K S V KRISHNA SRIKANTH Page 45

int start; clrscr(); ob.init(); ob.input(); ob.bfs(); getch(); } OUTPUT FOR BFS ENTER NUMBER OF NODES IN A GRAPH: 9 ENTER ADJACENCY MATRIX FOR A GRAPH: 0 1 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 STARTING NODE FOR BFS TRAVERSING: 1 BREADTH FIRST SEARCH TRAVERSING IS: 1 -> 2 -> 4 -> 5 -> 3 -> 7 -> 6 -> 8 -> 9 22) DEPTH FIRST SEARCH #include<iostream.h> #include<conio.h> #define MAX 20 class depth { private: int a[MAX][MAX],visited[MAX]; int n,top; public: void init(); void input(); void dfs(int); }; void depth::init() { int i,j; for(i=0;i<MAX;i++) { visited[i]=0; for(j=0;j<MAX;j++) a[i][j]=0; }

Page 46: Data Structures Programs

K S V KRISHNA SRIKANTH Page 46

top=-1; } void depth::input() { int i,j; cout<<"\nENTER NUMBER OF NODES IN A GRAPH: "; cin>>n; cout<<"\nENTER ADJACENCY MATRIX FOR A GRAPH: \n\n"; for(i=1;i<=n;i++) for(j=1;j<=n;xj++) cin>>a[i][j]; } void depth::dfs(int v) { int i; visited[v]=1; cout<<v<<" -> "; for(i=1;i<=n;i++) if(a[v][i]==1&&visited[i]==0) dfs(i); } void main() { depth ob; int start; clrscr(); ob.init(); ob.input(); cout<<"\nSTARTING NODE FOR DFS TRAVERSING: "; cin>>start; cout<<"\nDEPTH FIRST SEARCH TRAVERSING IS:\n\n"; ob.dfs(start); getch(); } OUTPUT FOR DFS ENTER NUMBER OF NODES IN A GRAPH: 9 ENTER ADJACENCY MATRIX FOR A GRAPH: 0 1 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

Page 47: Data Structures Programs

K S V KRISHNA SRIKANTH Page 47

STARTING NODE FOR BFS TRAVERSING: 1 BREADTH FIRST SEARCH TRAVERSING IS: 1 -> 2 -> 3 -> 6 -> 5 -> 8 -> 9 -> 4 -> 7 -> 23) Conversion of Infix to Prefix

#include <iostream.h> #include <string.h> #include <ctype.h> const int MAX = 50 ; class infix { private: char target[MAX], stack[MAX] ; char *s, *t ; int top, l ; public: infix(); void setexpr(char *str); void push(char c); char pop(); void convert(); int priority(char c); void show(); }; // initializes data member infix::infix() { top = -1 ; strcpy(target,""); strcpy(stack,""); l = 0; } // reverses the given expression void infix::setexpr(char *str) { s = str; strrev(s); l = strlen(s); * (target+l)='\0'; t = target+(l-1); } // adds operator to the stack void infix :: push(char c)

Page 48: Data Structures Programs

K S V KRISHNA SRIKANTH Page 48

{ if (top == MAX - 1) cout << "\nStack is full\n" ; else { top++ ; stack[top] = c ; } } // pops an operator from the stack char infix::pop() { if (top == -1) { cout << "Stack is empty\n" ; return -1 ; } else { char item = stack[top] ; top-- ; return item ; } } // converts the infix expr. to prefix form void infix :: convert() { char opr ; while(*s) { if(*s==' '||*s=='\t') { s++; continue; } if(isdigit(*s)||isalpha(*s)) { while(isdigit(*s)||isalpha(*s)) { *t = *s; s++; t--; } } if(*s == ')') { push(*s); s++; }

Page 49: Data Structures Programs

K S V KRISHNA SRIKANTH Page 49

if(*s=='*'||*s=='+'||*s=='/'||*s=='%'||*s=='-'||*s=='$') { if(top!=-1) { opr = pop(); while(priority(opr)>priority(*s)) { *t = opr; t--; opr = pop(); } push(opr); push(*s); } else push(*s); s++; } if(*s == '(') { opr = pop(); while((opr)!=')') { *t=opr; t--; opr=pop(); } s++; } } while(top != -1) { opr = pop(); *t = opr; t--; } t++; } // returns the priority of the operator int infix::priority(char c) { if (c == '$') return 3; if (c=='*'||c=='/'||c=='%') return 2; else { if(c=='+'||c=='-') return 1;

Page 50: Data Structures Programs

K S V KRISHNA SRIKANTH Page 50

else return 0; } } // displays the prefix form of given expr. void infix::show() { while(*t) { cout<<" "<<*t; t++; } } void main() { char expr[MAX]; infix q; cout <<"\nEnter an expression in infix form: " ; cin.getline(expr,MAX); q.setexpr(expr) ; q.convert() ; cout << "The Prefix expression is: " ; q.show() ; } 24) Reversing a Linked List

#include <iostream.h> class revll { private: struct node { int data ; node *link ; } *p; public: revll(); void addatbeg(int num); void reverse(); void display() ; int count() ; ~revll() ; }; revll::revll() { p = NULL; } void revll::addatbeg(int num)

Page 51: Data Structures Programs

K S V KRISHNA SRIKANTH Page 51

{ node *temp;

temp = new node; temp -> data = num; temp -> link = p; p = temp; } void revll :: reverse() { node *q, *r, *s; q = p; r = NULL; while (q!= NULL) { s = r; r = q; q = q -> link; r -> link = s; } p = r; } void revll :: display() { node *temp = p; cout << endl; while ( temp != NULL ) { cout << temp -> data << " "; temp = temp -> link; } } int revll :: count() { node *temp = p; int c = 0; while(temp!=NULL) { temp = temp -> link; c++; } return c; } revll::~revll() { node *q; while(p != NULL) { q = p -> link; delete p; p = q;

Page 52: Data Structures Programs

K S V KRISHNA SRIKANTH Page 52

} } void main() { revll l; l.addatbeg(7); l.addatbeg(43); l.addatbeg(17); l.addatbeg(3); l.addatbeg(23); l.addatbeg(5); cout << "\nElements in the linked list before reversing: "; l.display(); cout << "\nNo. of elements in the linked list: " << l.count(); l.reverse(); cout << "\n\nElements in the linked list after reversing: "; l.display(); cout << "\nNo. of elements in the linked list: " << l.count(); } 25) Linked list created in ascending order #include <iostream.h> class linklist { private:

struct node { int data; node *link; } *p; public: linklist(); void add(int num); void display(); int count(); void del(int num); ~linklist(); }; linklist::linklist() { p = NULL; } void linklist :: add(int num) { node *r, *temp = p; r = new node; r -> data = num; if(p==NULL||p->data>num) {

Page 53: Data Structures Programs

K S V KRISHNA SRIKANTH Page 53

p = r; p -> link = temp; } else { while(temp!=NULL) { if(temp->data<=num&&(temp->link->data>num||temp->link==NULL)) { r -> link = temp -> link ; temp -> link = r ; return ; } temp = temp -> link ; } } } void linklist :: display() { node *temp = p ; cout << endl ; while ( temp != NULL ) { cout << temp -> data << " " ; temp = temp -> link ; } } int linklist :: count() { node *temp = p ; int c = 0 ; while ( temp != NULL ) { temp = temp -> link ; c++ ; } return c ; } linklist :: ~linklist() { node *q ; while ( p != NULL ) { q = p -> link ; delete p ; p = q ; } } void main() {

Page 54: Data Structures Programs

K S V KRISHNA SRIKANTH Page 54

linklist l ; l.add(5) ; l.add(1) ; l.add(6) ; l.add(4) ; l.add(7) ; cout << "\nElements in the linked list: " ; l.display() ; cout << "\nNo. of elements in linked list: " << l.count() ; } 26) Parenthesis Matching

#include<iostream.h> #include<string.h> #include<conio.h> class pm { char ex[30]; int stack[20];

int len; public:

void displaypairs(char t[]) { int top=-1; int len=strlen(t); for(int i=0;i<len;i++) if(t[i]=='(') stack[++top]=i; else if(t[i]==')')

{ if(top!=-1) { int temp=stack[top--]; cout<<endl<<temp<<" matched with "<<i; } else cout<<"No match for parenthesis at "<<i;

} while(top!=-1) cout<<"\nNo match for parenthesis at "<<stack[top--]; }; void main() { pm t; int l; char ex[30]; clrscr(); cout<<"Enter the expression: "; cin.getline(ex,30);

Page 55: Data Structures Programs

K S V KRISHNA SRIKANTH Page 55

t.displaypairs(ex); } Output: Enter the expression: (d+(a+b)*c*(d+e)-f))(()

3 matched with 7

11 matched with 15

0 matched with 18

No match for parenthesis at 19

21 matched with 22

No match for parenthesis at 20 27) Towers of Hanoi

#include<iostream.h> #include<conio.h> #include<process.h> class toh { public: void move(int,char,char,char); }; void toh::move(int n,char s,char a,char e) { if(n==1) cout<<"Move from "<<s<<" to "<<e<<endl; else { move(n-1,s,e,a); move(1,s,' ',e); move(n-1,a,s,e); } } void main() { int n=3; clrscr(); toh t; t.move(n,'A','B','C'); getch(); } Output:

Move from A to C Move from A to B Move from C to B

Page 56: Data Structures Programs

K S V KRISHNA SRIKANTH Page 56

Move from A to C Move from B to A Move from B to C Move from A to C 28) Usage of different Parameters

#include<iostream.h> #include<conio.h> class item { int ino; int icost; public: item(int a,int b) { ino=a; icost=b; } int addTwoItems(item *x) { int r=icost+x->icost; return r; } friend int sum(const int *p,const int *q) { int total=(*p)+(*q); return total; } }; void main() { clrscr(); item c(10,30); int m=20,n=70; item y(m,n); cout<<endl<<"Addition of 2 items= "<<c.addTwoItems(&y)<<endl; cout<<endl<<"Sum of 2 items= "<<sum(&m,&n); } OUTPUT Addition of 2 items= 100 Sum of 2 items= 90 29) Queues using Arrays

#include<iostream.h> #include<process.h> template<class T> class queue {

T *s; int front,rear,size;

Page 57: Data Structures Programs

K S V KRISHNA SRIKANTH Page 57

public: queue(int); void insertion(); void deletion(); void display();

}; template<class T> queue<T>::queue(int k) {

size=k; s=new T[size]; front=rear=0;

} template<class T> void queue<T>::insertion() {

if(rear==size) cout<<endl<<"Queue overflow ";

else {

T x; cout<<endl<<"Enter the data value "; cin>>x; s[rear]=x; rear++;

} } template<class T> void queue<T>::display() {

if(front==rear) cout<<endl<<"Queue empty ";

else {

cout<<endl<<"The queue elements are "; for(int i=front;i<rear;i++)

cout<<s[i]<< " "; }

} template<class T> void queue<T>::deletion() {

if(front==rear) cout<<endl<<"Queue underflow ";

else cout<<endl<<"The deleted element is "<<s[front++];

} void main() {

int m, ch; cout<<"Enter the size of array: "; cin>>m; queue <int> t1(m); while(1)

Page 58: Data Structures Programs

K S V KRISHNA SRIKANTH Page 58

{ cout<< “*****LINKED QUEUE MENU*****”<<endl; cout<< “1. INSERTION”<<endl; cout<< “2. DELETION”<<endl; cout<< “3. DISPLAY”<<endl; cout<< “4. EXIT”<<endl; cout<<"Enter the choice "; cin>>ch; switch(ch) {

case 1:t1.insertion();break; case 2:t1.deletion();break; case 3:t1.display();break; default:exit(0);break;

} }

} 30) Combining two Linked Lists. #include <iostream.h> #include <conio.h> class LinkedList { struct node { int data; node *link; } *p; public: void insbeg(int a) { node *q; q=p; p=new node; p->data=a; p->link=q; } void disp() { node *q; q=p; if(q==NULL) { cout<<"Empty List "<< endl; return; } cout<<"List Items ";

Page 59: Data Structures Programs

K S V KRISHNA SRIKANTH Page 59

while(q!=NULL) { cout<< q->data<<" "; q=q->link; } cout<<" # "<< endl; } void insend(int a) { node *q,*t; q=p; while(q->link!=NULL) q=q->link; t=new node; t->data=a; t->link=NULL; q->link=t; } Linkedist() { p=NULL; } friend void combine(LinkedList l1,LinkedList 12); }; //Function for combining Two Linked Lists void combine(LinkedList l1,LinkedList l2) { LinkedList l; l=l1; node *q=l.p; while(q!=NULL) q=q->link; node *r=l2.p; while(r!=NULL) { l.insend(r->data); r=r->link; } l.disp(); } void main() { LinkedList f,t; clrscr(); f.insbeg(10); f.insbeg(20); t.insbeg(111);

Page 60: Data Structures Programs

K S V KRISHNA SRIKANTH Page 60

t.insbeg(222); t.insbeg(333); cout<<"First List : "; f.disp(); cout<<"Second List : "; t.disp(); getch(); cout<<"After Combining \n"; combine(f,t); getch(); }