5 Stack 29 05 2024
5 Stack 29 05 2024
5 Stack 29 05 2024
Stack
● Top: Open end of the stack is called Top, From this end item can
be inserted.
● Push: To insert an item from Top of stack is called push operation.
The push operation change the position of Top in stack.
● POP: To put-off, get or remove some item from top of the stack is
the pop operation, We can POP only only from top of the stack.
● IsEmpty: Stack considered empty when there is no item on Top.
IsEmpty operation return true when no item in stack else false.
● IsFull: Stack considered full if no other element can be inserted on
top of the stack. This condition normally occur when stack
implement ed through array.
Stack Abstract Data Type
ADT/Class Stack {
Data/Attributes/Values:
int size; Top
Type items;
Functions/Operations:
CreateStack (int size); --create stack of size
Void Push(int n); - - if stack is not full
Type Pop(); - - if stack is not empty return top item
Int isEmpty(); - - return true if empty otherwise false
Int isFull(); - - return true if full otherwise false }
Stack Example
Stack Example ...
Stack Implementation using Array
# include <iostream.h>
#include <conio.h>
template <class Type>
Class intstack { private:
int size, top;
Type* Items;
// Functions & Operations
public:
Intstack ( ) { //default constructor
top=0; size=3;
Items = new Type[size]; }
Stack Implementation using Array
~intstack( ) {
delete[] Items; }
Void Push (Type a) {
if(!IsFul( ))
Items[top++ ] = a;
else
Cout <<”\n stack is full.... push failed”; }
Type POP(void) {
if(!isEmpty( ))
return Items [ --top ];
else
Count <<\n stack is empty .. POP failed”; }
Stack Implementation using Array
int IsEmpty ( ) { return (top == 0); }
int IsFull( ) { return (top == size); }
Void Display ( ) {
If( isEmpty( ))
Cout<<”Empty”;
Else for (int i=0; i<top; i++)
cout<<Item[i] <<” “;
}
} ; // end stack class
Stack Implementation using Array
Void main( ) s.Display( );
{ intstack s; s.pop( ); s.pop( ); s.push(45);
s.Display(); s.Display ()
s.POP(); getch();
s.push(6); s.push(5); s.push(2); }
s.push(3);
s.Display( );
s.push(8);
s.pop( ); s.push(8);
s.Display( );
s.pop( ); s.pop ( ); s.push(40);
Stack Implementation using Array
1. s.Display(); 1. Empty
2. s.POP(); 2. Stack is empty
3. s.push(6); s.push(5); s.push(2); 3.
s.push(3);
4. 6 5 2 3
4. s.Display( );
5. Stack is full
5. s.push(8);
6.
6. s.pop( ); s.push(8);
7. 6 5 2 8
7. s.Display( );
8.
8. s.pop( ); s.pop ( ); s.push(40);
9. 6 5 40
9. s.Display( );
10.s.pop( ); s.pop( ); s.push(45);
10.s.pop( ); s.pop( ); s.push(45);
11. 6 45
11.s.Display( );
Stack Implementation using Array ...
Stack Application
String is a b c d e f
PUSH to SACK
Reverse String...
Reversed String: f e d c b a
POP from SACK
Infix, Postfix and Prefix Expression
Exercise:
1. Infix ( (A * B) + (C / D) ) to Postfix
2. Infix ((A * (B + C) ) / D) to Postfix
3. Infix (A * (B + (C / D) ) ) to Postfix
Infix to Prefix Step by Step Conversion
Exercise:
1. Infix ( (A * B) + (C / D) ) to Prefix
2.Infix ((A * (B + C) ) / D) to Prefix
3.Infix (A * (B + (C / D) ) ) to Prefix
Infix to Postfix using stack
● Given an expression in the Infix form.
● Create and initialize a stack to hold operators.
● While expression has more token (operator and operands) Do.
● If the next token is an operand, add it to postfix string.
● Else if the next token is end parenthesis ')', then
– Do until the start parenthesis '(' is opened
Pop operator from the stack
●
– END Do
– Discard the Popped start parenthesis '('.
● End If
Infix to Postfix using stack ...
● If the next token is start parenthesis '(', push it on the stack.
● Else If the next token is an operator, Then
– While (Stack is not empty) AND (precedence of operator on top
of the stack is more then the current operator) Do
●Pop operator from the stack
● Add to postfix string
– End While
– Push operator to stack
● End if
● End While
Infix to Postfix using stack ...
● While stack is not empty
– Pop operator from the stack
– Add to postfix string
● End While
● Postfix is Created
Infix to Postfix using stack
● Example A*B+C become AB*C+
Infix to Postfix using stack ...
● Example A * (B + C * D) + E becomes A B C D * + * E +
Infix to Postfix using Stack..