Stack and Queues Stack implementation using Array Stack implementation using Linked List Queue implementation using Array Queue implementation using Linked List Convert Infix Exp into Postfix Evaluate Postfix Expression
Stack using Array Push 10 Push 20 Push 30 Pop Push 50 4 3 30 2 Top 20 Top Top = -1 50
const int max=100; void main ( ) { int DATA [max ] ,Top= -1, c ; do clrscr( ); cout << “1.Push\n2.Pop\n3.Show\nExit Any other number\n”; cin>>c ; switch ( c ) case 1 : push ( DATA , Top) ; break ; case 2 : pop ( DATA , Top) ; break ; case 3 : show ( DATA , Top) ; break ; } } while(c>=1 && c<= 3);
void push ( int Num [ ] , int & Top ) { int item ; //Check overflow condition if ( Top == max – 1 ) cout<<“Overflow “; } else cout << “Enter Item To Push into the stack\n”; cin >> item; Top = Top +1; Num [ Top ] =item; cout<<“Item Inserted “;
void pop ( int Num [ ] , int & Top ) { //Check underflow condition if ( Top == – 1 ) cout<<“underflow “; } else cout << Num[ Top ]<<“is to be deleted \n“; Top = Top -1; cout<<“Item deleted “;
void show ( int Num [ ] , int Top ) { //Check underflow condition if ( Top == – 1 ) cout<<“underflow “; } else cout << Num[ Top ]<<“Top \n“; for ( i= Top -1; i > = 0 ; i - - ) cout<< Num [ i ] << endl ;
char * msg [ ] = {“over flow”, “under flow”}; class Stack { int Top ; int DATA [10]; void error (int e_num) { cout<< msg [e_num ] ; } public: void init () { Top = -1 ; } void Push ( int ); void Pop ( ); }; Define Push which accept an item and insert into stack. Define Pop to delete an item from stack. ( Use error ( ) function for underflow and overflow condition
void push ( int Num [ ] , int & Top ) { int item ; //Check overflow condition if ( Top == max – 1 ) cout<<“Overflow “; } else cout << “Enter Item”; cin >> item; Top = Top +1; Num [ Top ] =item; cout<<“Item Inserted “; void Stack :: push ( int item ) { int item ; //Check overflow condition if ( Top == 10 – 1 ) error(0); } else cout << “Enter Item”; cin >> item; Top = Top +1; DATA [ Top ] =item; cout<<“Item Inserted “;
void Stack :: pop ( ) { //Check underflow condition if ( Top == – 1 ) cout<<“underflow “; } else cout << DATA[ Top ]<<“is to be deleted \n“; Top = Top -1; cout<<“Item deleted “;
Linked List Linked list is a data structure in which an element of list point to another element of list. It is implemented with the help of pointers and self referential structure. Each element ( also called node ) contain two fields : Information part and Pointer. Pointer struct Student { int RollNo; char Name[20] ; Student * link ; } InfoPart Link InfoPart Link InfoPart Link Null
Stack Implementation Using Linked List Stack is a data structure in which top pointer point to top element of stack. It is implemented with the help of pointers and self referential structure. Each element ( also called node ) contain two fields : Information part and Pointer. Top struct Student { int RollNo; char Name[20] ; Student * link ; } InfoPart Link InfoPart Link InfoPart Link Null
cout << “1.Push\n2.Pop\n3.Show\nExit Any other number\n”; struct Student { int RollNo; char Name[20] ; Student * link ; } ; student *Top = NULL; void main ( ) int c ; do clrscr( ); cout << “1.Push\n2.Pop\n3.Show\nExit Any other number\n”; cin>>c ; switch ( c ) case 1 : push ( ) ; break ; case 2 : pop ( ) ; break ; case 3 : show ( ) ; break ; } } while(c>=1 && c<= 3);
Top void push ( ) { //create node Student *ptr ; ptr = new Student ; if ( ptr == NULL ) // checking overflow { cout << “Overflow “ ; return ; } //assigning Values cout << “Enter RollNo “ ; cin >> ptr -> RollNo; cout << “Enter Name “ ; gets( ptr -> Name ) ; ptr - > link =NULL; // Insert Node (Element) if ( Top = = NULL ) { Top = ptr ; } else ptr - > link =Top ; top = ptr ; } Null InfoPart Link Ptr Null Top InfoPart Link InfoPart Link InfoPart Link Null Null Ptr
Top void Pop ( ) { student *ptr ; if ( Top = = NULL ) { cout<<“Underflow “ ; } else ptr=top; cout<<ptr->name<<“being deleted”; Top=Top - > link ; delete ptr ; } Null Top InfoPart Link InfoPart Link Null Ptr
void Display ( ) { student void Display ( ) { student *ptr ; if ( Top = = NULL ) { cout<<“Underflow “ ; } else ptr=top; while (ptr!=NULL) cout<<“Name”<<ptr->Name<<“,”<<“RollNo”<<ptr->RollNo; ptr=ptr - > link ; }
Convert Infix expression into Postfix expression Suppose expression X in infix notation and X is to be converted in equivalent postfix expression Y. Put X inside “(“ and “) “ Scan X from Left to Right and follow steps 3 to 6 until X is empty. If an operand is encountered , add it to Y. If an operator is encountered , then Repeatedly pop from STACK and add to Y each operator on the top of the stack which has the same precedence as or higher precedence than operator. Push operator to stack If a right “)” is encountered , then Repeatedly pop from STACK and add to Y each operator on the top of the stack until left “(” is encountered . Remove left “(“ from stack. If a left “)” is encountered , then Push to stack End
Convert Infix into Postfix Exp :- (( X + Y ) / ( Z * Y ) –R) No Ele Action Stack Postfix Exp Y 1 ( Push 2 ( , ( 3 X Add to Y 4 + ( , ( , + 5 Y XY 6 ) Pop + and add, Remove ( XY + 7 / ( , / 8 ( , / ,( 9 Z Add ( , / , ( XY+ Z 10 * ( , / , ( , * 11 ( , / , ( , * XY+ZY 12 Pop * ,Add to Exp , Remove ( ( , / XY+ZY * 13 - ( , - XY+ZY * / 14 R ( , - XY+ZY * / R 15 Pop - ,Add to Exp ,Remove ( Empty XY+ZY * / R -
Evaluation of Postfix expression Suppose expression X in postfix expression. Scan X from Left to Right and follow steps 2 to 4. If an operand is encountered , push on to stack. If an operator is encountered , then Pop two operand from stack (pop one operand for unary operator ) Evaluate expresseion formed by (Operand1) Operator (Operand2). Push the result on to stack If X is empty (no more element) , then pop the resul from stack End
Evaluation of Postfix Exp Ele Action Stack Calculation 1 10 Push 2 3 10 , 3 * Pop 10 , 3 / cal / push Result 30 10*3=30 4 7 30,7 5 30,7,1 6 - Pop 7, 1 / cal / push Result 30, 6 7-1=6 Pop 30 , 6 / cal / push Result 180 30*6=180 8 23 180,23 9 + Pop 180 , 23 / cal / push Result 203 180+23 Ans :- 203