2. INTRODUCTION
● Array allows to insert and delete an element at any place in the list
● STACK is a list of elements
● It is a linear data structure
● Insertion and deletion operation is performed only at one end –
known as top of stack
● Also known as Last-in-First-out (LIFO), entries are removed in
reverse order of insertion
www.eshikshak.co.in
3. BASIC OPERATIONS OF STACKS
● push -- insert item on stack
● pop -- remove item from stack
● peek -- examine, but don’t remove item
● isEmpty – reports whether or not stack is empty
● isFull- reports whether or not stack is full
● size – returns # of items in stack
www.eshikshak.co.in
4. PUSH OPERATION
● Inserting element in a stack is known as PUSH
● When elements are inserted continuously to the stack it grows at one
end i.e. top
Top 9
=>
Top 6 8
=>
Top -7 -7 -7
=>
Top 8 8 8 8
=>
Top 5 5 5 5 5
=>
Top =
NULL
www.eshikshak.co.in
5. POP OPERATION
● Removing of element from a stack is known as pop
● When elements are removed continuously from a stack, it shrinks at
the same end i.e. top
Top 9
=>
8 Top 6
=>
-7 -7 Top -7
=>
8 8 8 Top 8
=>
5 5 5 5 Top 5
=>
Top =
NULL
www.eshikshak.co.in
6. STACKS CONDITIONS
● Depending on implementation,
● Important to know if stack is
may be necessary to check if
empty -- attempt to remove an
stack is full -- attempt to add
item from an empty stack is an
item to a full stack is an overflow
underflow error.
error
Top 9
=>
8
-7
8
5
Top =
NULL
www.eshikshak.co.in
7. PUSH(STACK, TOP, MAXSTK, ITEM)
STACK - Linear Array, TOP – location of top element in stack,
MAXSTK – Numbers of elements that be stored in Stack,
ITEM – value that has to be inserted in stack
STEP 1. if top = MaxStk, then
print “Overflow” and return
STEP 2. set top=top + 1
STEP 3. set STACK[top] = item
STEP 4. Return
www.eshikshak.co.in
8. PUSH(STACK, TOP)
STACK - Linear Array, TOP – location of top element in stack,
ITEM – value that has to be inserted in stack
1. [check for underflow of the stack]
if TOP=-1 then
Write(“stack underflow on pop)
end if
2. [ Assign the top element of the stack to ITEM]
ITEM = STACK[TOP]
2. [decrement pointer]
TOP=TOP-1
3. [return former top element of the stack]
Return(ITEM)
4. Exit
www.eshikshak.co.in
9. PEEP(STACK,TOP)
1. [check for stack underflow]
if TOP-I+1<=0 then
Write (“stack underflow on peep”)
end if
2. [Return the top element of the stack]
Return(stack[TOP-MAX+1])
3. Exit
www.eshikshak.co.in
10. APPLICATIONS OF STACK
● Undo
● Redo
● Function Call
● Recursive Function Call
● Convert infix to Postfix expression
● Convert infix to Prefix expression
● Evaluate infix expression
● Tower of hanoi
● Reverse string or list or file
www.eshikshak.co.in