4505 Stack
4505 Stack
Stack Overview
Stack
Basic operations of stack
Pushing, popping etc.
Implementations of stacks using
array
linked list
Stack and Queue / Slide 3
Stack Overview
Stack : Linear data structure
It’s list of elements in which items may be added or
removed only at 1 end called TOP.
Stack
Stacks are less flexible
but are more efficient and easy to implement
Stacks are known as LIFO (Last In, First Out)
lists.
The last element inserted will be the first to be
retrieved
Or elements are removed from stack in reverse
order of that in which they were inserted into stack.
Basic operations of stack
Stack and Queue / Slide 6
top
B
top top A
A A
top
Stack and Queue / Slide 7
Implementation of Stacks
Any list implementation could be used to
implement a stack
Arrays (static: the size of stack is given initially)
Linked lists (dynamic: never become full)
Stack and Queue / Slide 9
Array Implementation
Stack can be represented in memory using linear
array and are maintained in memory using
1. STACK (LINEAR ARRAY)
2.TOP (contains loc of top element of stack.)
3. MAXSTK ( which gives max. no. of elements that can be held by stack.
Need to declare an array size ahead of time
TOP=0 Or TOP=NULL
indicate that stack is empty.
TOP=3 means stack has 3 elements
TOP=MAXSTK means stack already filled.
Stack and Queue / Slide 12
Push
(1) Increment TopOfStack by 1.
(2) Set Stack[TopOfStack] = X
Pop
(1) Set return value to Stack[TopOfStack]
(2) Decrement TopOfStack by 1
These operations are performed in very fast
constant time
Stack and Queue / Slide 13
Stack class
Attributes of Stack
MAXSTK: the max size of stack
TOP: the index of the top element of stack
values: point to an array which stores elements of stack
Operations of Stack
IsEmpty: return true if stack is empty, return false otherwise
IsFull: return true if stack is full, return false otherwise
Top: return the element at the top of stack
Push: add an element to the top of stack
Pop: delete the element at the top of stack
DisplayStack: print all the data in the stack
Stack and Queue / Slide 14
Push Stack
PUSH(STACK,TOP,MAXSTK,ITEM)
This Procedure pushes an item onto stack
1. [Stack already filled?]
If TOP=MAXSTK,then print OVERFLOW and
return.
2. Set TOP:=TOP+1 [Increment Top by 1]
3. Set STACK[TOP]:=ITEM.
[Insert ITEM in new top position]
4. Return
Stack and Queue / Slide 16
Pop Stack
Check UNDERFLOW CONDITION,whether Stack has an item to
be removed?
double Pop()
Pop and return the element at the top of the stack
If the stack is empty, print the error information.
(In this case, the return value is useless.)
Don’t forgot to decrement top
double StackPop() {
if (IsEmpty()) {
printf("Error: the stack is empty.“);
return -1;
}
else {
return values[top--];
}
}
Stack and Queue / Slide 17
Pop(STACK,TOP,ITEM)
This Procedure deletes top element of stack and assigns it to
variable ITEM.
1. [Check whether Stack has an item to be removed?]
If TOP=0,then print UNDERFLOW and return.
2. Set ITEM:=STACK[TOP] [Assigns TOP element to ITEM]
3. Set TOP:=Top-1.
[Decrease TOP by 1]
4. Return
Implementation based on Linked List
Stack and Queue / Slide 18
(Linked Stack)
Now let us implement a stack based on a linked list.
Push and pop at the head of the list
New nodes should be inserted at the front of the list, so that they become the top of the stack
Nodes are removed from the front (top) of the list
PUSH operation into stack is accomplished by inserting node into
front or start of list.
POP operation is undertaken by deleting node pointed to by START
pointer.
Stack and Queue / Slide 19
push(6)
top
NULL
6
info link
top
TOP of the stack
1
6
Stack and Queue / Slide 21
1
6
Stack and Queue / Slide 22
1
6
Stack and Queue / Slide 23
1
6
Stack and Queue / Slide 24
Procedure: Push-link-stack(INFO,LINK,TOP,AVAIL,ITEM)
This procedure pushes an ITEM into linked stack.
1. [Available space?]
If AVAIL=NULL, then write: OVERFLOW and exit.
2. [Remove 1st node from AVAIL list]
Set NEW:=AVAIL and
AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM [Copies ITEM into new node]
4. Set LINK[NEW]:=TOP
[New node points to original top node in stack]
5. Set TOP=NEW [reset TOP to point to mew node at top of
stack]
6. Exit
Stack and Queue / Slide 25
Procedure: Pop-link-stack(INFO,LINK,TOP,AVAIL,ITEM)
This procedure deletes top element of linked stack and assigns
it to variable ITEM.
1. [Check stack has an item to be removed?]
If TOP=NULL, then write: UNDERFLOW and exit.
2. [copies top element of stack into item]
Set ITEM:=INFO[TOP]
3. Set TEMP:=TOP and TOP:=LINK[TOP]
[Save old value of TOP ptr. In TEMP and Reset TOP to
point to next node in stack]
4. [Return deleted node to AVAIL LIST]
Set LINK[TEMP]:=AVAIL and
AVAIL=TEMP
5. Exit
Stack and Queue / Slide 26
POLISH NOTATIONS
Infix, Prefix(Polish), and Postfix(Reverse Polish)
If the operator is placed in between operand, it is called infix
notation Ex. A+B (A+B)*C A+(B*C)
prefix notations – operators preceded the operands
Ex. +AB
(A+B)*C = [+AB]*C = *+ABC
A+(B*C) = A+[*BC) = +A*BC
(A+B)/(C-D)= [+AB]/[-CD] = /+AB-CD
Postfix- Operator symbol is placed after two operands
Ex. A+B= AB+
(A+B)*C= [AB+]*C= AB+C*
A+(B*C) = A+[BC*]=ABC*+
Stack and Queue / Slide 27
Postfix Expressions
Calculate 4.99 * 1.06 + 5.99 + 6.99 * 1.06
Need to know the precedence rules
Postfix (reverse Polish) expression
4.99 1.06 * 5.99 + 6.99 1.06 * +
Use stack to evaluate postfix expressions
When a number is seen, it is pushed onto the stack
When an operator is seen, the operator is applied to the 2
numbers that are popped from the stack. The result is
pushed onto the stack
Example
evaluate 6 5 2 3 + 8 * + 3 + *
The time to evaluate a postfix expression is O(N)
processing each element in the input consists of stack
operations and thus takes constant time