Lab - DS - Stack and Queue (Reference)
Lab - DS - Stack and Queue (Reference)
Definition of a Stack
Operations with a Stack
Array and Stack
Implementation of a Stack
Application of Stack (Arithmetic Expression Conversion and Evaluation)
Definition of a Queue
Operations with a Queue
Array, Stack and Queue
Implementation of a Queue
STACK
A stack or LIFO (last in, first out) is an abstract data type that serves as a collection of
elements, with two principal operations:
push adds an element to the collection on top;
pop removes the last (top of the stack) element that was added.
Bounded capacity
If the stack is full and does not contain enough space to accept an entity to be pushed,
the stack is then considered to be in an overflow state.
A pop either reveals previously concealed items or results in an empty stack – which
means no items are present in stack to be removed.
Non-Bounded capacity
Dynamically allocate memory for stack. No overflow.
7 Considering MaxSize = 7
There are 3 elements inside Stack
6
So next element will be pushed at index 3
5
Top 4
Top 3 D
2 C
1 B
0 A
Dr. Ashraf Uddin Data Structures Stack & Queue 5
OPERATION ON STACK - POP
Algorithm:
7 Considering MaxSize = 7
There are 4 elements inside Stack
6
So element will be popped from index 3
5
Top 4
Top 3 D
2 C
1 B
0 A
Dr. Ashraf Uddin Data Structures Stack & Queue 7
bool IsEmpty( void ){
// returns True if stack has no element
return (Top == 0);
}
7 Considering MaxSize = 7
6
Top 0
Dr. Ashraf Uddin Data Structures Stack & Queue 8
bool IsFull( void ){
// returns True if stack full
return ( Top == MaxSize );
}
5 F
4 E
3 D
2 C
1 B
0 A
Dr. Ashraf Uddin Data Structures Stack & Queue 9
bool TopElement( int &Element ){
// gives the top element in Element
if( IsEmpty() ) { cout << "Stack empty\n"; return false; }
Element = Stack[ Top - 1 ];
return true;
}
7 Considering MaxSize = 7
There are 4 elements inside Stack
6
So top element will be at index 3
5
Top 4
3 D
2 C
1 B
0 A
Dr. Ashraf Uddin Data Structures Stack & Queue 10
void Show( void ){
// prints the whole stack
if( IsEmpty() ) { cout << "Stack empty\n"; return; }
for( int i=Top-1; i>=0; i-- ) cout << Stack[i] << endl;
}
7 Considering MaxSize = 7
There are 4 elements inside Stack
6
So element will be shown from index 3 down
5 to index 0.
Top 4
3 D
2 C
1 B
0 A
Dr. Ashraf Uddin Data Structures Stack & Queue 11
ARRAY AND STACK
Differences…
Insertion/deletion of an element
Accessing an element
Infix: a + ( b + c ) * d – e * f
+ e f _
Postfix: a b c + d * *
Stack: + ( + * - *
Infix 2 * 6 / ( 4 - 1 ) + 5 * 3 )
Postfix 2 6 * 4 1 - / 5 3 * +
Stack ( +
*
/ (
* -
End of
OPERATOR
OPERAND
Expression
/
+ StackTop(
* <
- = )
( +
(
* )
/
2) Scan the given expression and do following for every scanned element.
…..b) If the element is a operator, pop operands for the operator from stack. Evaluate the operator
and push the result back to the stack
3) When the expression is ended, the number in the stack is the final answer
OPERATOR
OPERAND
Evaluate(
Evaluate(
Evaluate(
Expression
End of
4,
2,
5,
12,
4,
Expression
‘)‘
'+',
Result
'*',
'-',
'/',15
613=
3 )))19
===15
12
19
3
4
Convert the infix operation into postfix expression and then evaluate the postfix
expression using stacks.
0 1 2 3 4
QUEUE - OPERATION Queue 3 6 2 5
Check IsEmpty Initialize( ) front=rear=0
Check IsFull EnQueue( 3 )
rear rearrearrearrear
EnQueue (add element to back EnQueue( 6 )
i.e. at the rear) EnQueue( 2 )
DeQueue (remove element from EnQueue( 5 )
the front)
EnQueue( 9 ) Queue Full (rear==MaxSize)
FrontValue (retrieve value of
element from front) DeQueue( &v ) 3
ShowQueue (print all the values DeQueue( &v ) 6
of stack from front to rear) DeQueue( &v ) 2
DeQueue( &v ) 5
DeQueue( &v ) Queue Empty (front==rear)
0 1 2 3 4
CIRCULAR QUEUE -
OPERATION Queue 3
9 6 2 5
Check IsEmpty Initialize( ) front=rear=0
Check IsFull EnQueue( 3 ) rear rearrearrearrear
EnQueue (add element to back EnQueue( 6 )
i.e. at the rear) EnQueue( 2 )
DeQueue (remove element from
DeQueue( &v ) 3
the front)
DeQueue( &v ) 6
FrontValue (retrieve value of
element from front) EnQueue( 5 ) (rear==MaxSize)rear=0
ShowQueue (print all the values EnQueue( 9 )
of stack from front to rear) DeQueue( &v ) 2
DeQueue( &v ) 5 (front==MaxSize) front=0
DeQueue( &v ) 9
DeQueue( &v ) Queue Empty (front==rear)
Dr. Ashraf Uddin Data Structures Stack & Queue 28
QUEUE
Differences….