STACKS :
General Representation
and Operations
Kiranpal Singh Virk
Assistant Professor, Guru Nanak Khalsa College
Yamuna Nagar
(kiranpal.virk@yahoo.com)
TOPICS
• Introduction
• Representation
–Arrays
–Linked List
• Operations
PUSH operation
POP operation
STACK
• Generally speaking:
–“a neat pile of objects”
–“rectangular or cylindrical pile of hay,
straw etc”
BOOK STACK
FOOD STACK
CD STACK
STACK
• A stack is an ordered collection of items into
which new items may be inserted and from
which items may be deleted at one end,
called the top of the stack
STACK
• A stack is a linear data structure which
satisfies the following properties at any time:
– Allocations and de-allocations are performed in
a last-in-first-out (LIFO) manner.
– i.e. amongst all existing entries at any time, the
last entry to have been allocated is the first
entry to be de-allocated
– only the last entry is accessible
STACK
STACK
3 2 1
IN OUT
Last-in First-Out
STACK
3
2
1
IN OUT
Last-in First-Out
STACK
3
2
1
IN OUT
Last-in First-Out
STACK APPLICATIONS
• Applications of Stacks
– Page-visited history in a Web browser
– Undo sequence in a text editor
– Saving local variables when one function calls
another, and this one calls another, and so on.
REPRESENTATION OF
STACK
• Stack can be represented in computer in the
following two ways:
–Array Representation
–Linked Representation
ARRAY-BASED STACK
• A simple way of implementing the Stack
uses an array
• A variable TOP contains the location of the
top element of the stack
TOP
S …..
ARRAY-BASED STACK
D
C
B
A
A B C D
1 2 3 4 ….. ….. n-1 n
n
n-1
…..
…..
4
3
2
1
top
top
“a stack is an ordered collection of items
into which new items may be inserted and
from which items may be deleted at one
end, called the top of the stack”
D
C
B
A
top
(a)
E
D
C
B
A
top
(b)
F
E
D
C
B
A
top
(c)
G
F
E
D
C
B
A
top
(d)
H
G
F
E
D
C
B
A
top
(e)
ARRAY-BASED STACK
D
C
B
A
top
(i)
E
D
C
B
A
top
(h)
F
E
D
C
B
A
top
(g)
G
F
E
D
C
B
A
top
(f)
H
G
F
E
D
C
B
A
top
(e)
ARRAY-BASED STACK
D
C
B
A
top
(a)
E
D
C
B
A
(b)
F
E
D
C
B
A
(c)
G
F
E
D
C
B
A
(d)
H
G
F
E
D
C
B
A
(e)
D
C
B
A
(i)
E
D
C
B
A
(h)
F
E
D
C
B
A
(g)
G
F
E
D
C
B
A
(f)
ARRAY-BASED STACK
D
C
B
A
E
D
C
B
A
top
ARRAY-BASED STACK
D
C
B
A
E
D
C
B
A
F
E
D
C
B
A
top
ARRAY-BASED STACK
D
C
B
A
E
D
C
B
A
F
E
D
C
B
A
G
F
E
D
C
B
A
top
ARRAY-BASED STACK
D
C
B
A
E
D
C
B
A
F
E
D
C
B
A
G
F
E
D
C
B
A
H
G
F
E
D
C
B
A
top
ARRAY-BASED STACK
ARRAY-BASED STACK
• A variable MAXSTK which gives the
maximum number of elements that can be
held by the stack
TOP
S …..
MAXSTK
ARRAY-BASED STACK
• The condition TOP=0 or TOP=NULL
indicates that the stack is empty.
TOP=NULL
STACK
MAXSTK
1 2 3 4 5 6 7 8
ARRAY-BASED STACK
• The condition TOP=MAXSTK indicates that
stack is full.
TOP
STACK
MAXSTK
A B C D
1 2 3 4 5 6 7 8
E F G H
ARRAY-BASED STACK
• MAXSTK=8
• TOP=4
• 4 more items can be added on the stack
TOP
STACK
MAXSTK
A B C D
1 2 3 4 5 6 7 8
OPERATIONS ON
STACK
• Two basic operations on stacks
–PUSH is the term used to insert an
element into stack
–POP is the term used to delete an
element from a stack
PUSH OPERATION
• Before executing the PUSH operation, one
must test if there is a room in the stack for
new item or not
• Stack-full condition is called “overflow”
TOP
STACK
MAXSTK
A B C D
1 2 3 4 5 6 7 8
PUSH OPERATION
PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a stack
(1) If TOP = MAXSTR, then: Print: OVERFLOW,
and Return [stack already filled ?]
(2) Set TOP = TOP+1 [increases TOP by 1]
(3) Set STACK[TOP] = ITEM [Inserts ITEM in new
TOP position]
(4) Return
PUSH OPERATION
START
IF TOP =
MAXSTR
YES
NO
PRINT
“OVERFLOW”
TOP = TOP + 1
STACK[TOP] = ITEM
STOP
PUSH OPERATION
START
IF TOP =
MAXSTR
YES
NO
PRINT
“OVERFLOW”
TOP = TOP + 1
STACK[TOP] = ITEM
STOP
POP OPERATION
• Before executing the POP operation, one
must test whether there is an element in the
stack to be deleted or not
• Empty stack condition is called “underflow”
TOP
STACK
MAXSTK
A B C D
1 2 3 4 5 6 7 8
POP OPERATION
POP(STACK, TOP, ITEM)
This procedure deletes the top element of stack and
assigns it to the variable ITEM
(1) If TOP = 0, then: Print: UNDERFLOW, and
Return [stack empty ?]
(2) Set ITEM = STACK[TOP] [Assign TOP
element to ITEM]
(3) Set TOP = TOP-1 [Decreases TOP by 1]
(4) Return
POP OPERATION
START
IF TOP = 0
YES
NO
PRINT
“UNDERFLOW”
ITEM = STACK[TOP]
TOP = TOP - 1
STOP
POP OPERATION
START
IF TOP = 0
YES
NO
PRINT
“UNDERFLOW”
ITEM = STACK[TOP]
TOP = TOP - 1
STOP
LIMITATIONS OF
ARRAY-BASED STACK
• The maximum size of the stack must be
defined a priori , and cannot be changed
• Trying to push a new element into a full
stack causes an implementation-specific
exception
LINKED LIST BASED
STACK
PUSH OPERATION
• X
•
3 1
•
TOP
2
LINKED LIST BASED
STACK
• X
•
3 1
•
TOP
2
•
4
PUSH OPERATION
LINKED LIST BASED
STACK
• X
•
3 1
•
TOP
2
•
4
POP OPERATION
LINKED LIST BASED
STACK
• X
•
3 1
•
TOP
2
POP OPERATION
FUNCTION
CALL STACK
main( )
{
int a = 5, b = 2, c ;
c = add ( a, b ) ;
printf ( "sum = %d", c ) ;
}
add ( int i, int j )
{
int sum ;
sum = i + j ;
return sum ;
}
5
2 copy of b
copy of a
when call
to add() is
met
FUNCTION
CALL STACK
main( )
{
int a = 5, b = 2, c ;
c = add ( a, b ) ;
printf ( "sum = %d", c ) ;
}
add ( int i, int j )
{
int sum ;
sum = i + j ;
return sum ;
}
xxx
5
2 copy of b
copy of a
before
transferring
control to
add()
address of printf()
FUNCTION
CALL STACK
main( )
{
int a = 5, b = 2, c ;
c = add ( a, b ) ;
printf ( "sum = %d", c ) ;
}
add ( int i, int j )
{
int sum ;
sum = i + j ;
return sum ;
}
7
xxx
5
2 copy of b
copy of a
after control
reaches add()
address of printf()
copy of sum
FUNCTION
CALL STACK
main( )
{
int a = 5, b = 2, c ;
c = add ( a, b ) ;
printf ( "sum = %d", c ) ;
}
add ( int i, int j )
{
int sum ;
sum = i + j ;
return sum ;
}
xxx
5
2 copy of b
copy of a
while
returning
control from
add()
address of printf()
FUNCTION
CALL STACK
main( )
{
int a = 5, b = 2, c ;
c = add ( a, b ) ;
printf ( "sum = %d", c ) ;
}
add ( int i, int j )
{
int sum ;
sum = i + j ;
return sum ;
}
on returning
control from
add()
SUGGESTED READING
• BOOKS:
– A.M. Tenenbaum, “Data Structures using C”,
Prentice Hall
– S. Lipschutz, “Theory and Problems of Data
Structures”, McGraw Hill
• ONLINE RESOURCES
– Microsoft MSDN library
Thank You

Stack & Queue presentation for educational purpose