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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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>
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
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
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() {
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;
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!='(')
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++)
} 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++)
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();
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() {
} } 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();
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;
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 {
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
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(); }
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:
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:
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]<<" ";
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(); };
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;
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; }
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
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)
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++; }
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)
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;
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) {
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() {
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);
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
Move from A to C Move from A to B Move from C to B
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 {