Stack
Data Structures
Stacks
Stacks in real life: stack of books, stack of
plates
Add new items at the top
Remove an item at the top
Stack data structure similar to real life:
collection of elements arranged in a linear
order.
Can only access element at the top
What are Stacks?
In array insertion and deletion can take place at
both end, i.e. start and end,
But if insertion and deletion are restricted from one
end, must used STACKS and QUEUES.
Stack can be defined as:
A stack is a linear data structure which can
be accessed only at one of its ends for
storing and retrieving data
In which item may be inserted or deleted only from
one end called top of the stack.
There are two ways of implementing a stack:
Array (Static & Dynamic)
Link List (Dynamic)
Stack terminology
“Top”: where insertion and
deletion take place
“Push”: is the term used to insert an
element into an stack
“Pop”: is the term used to delete an
element from a stack.
Basic operations of a Stack
New Push
?
Pop Peek
Empty
?
Stack Operations
Push(X) – insert X as the top element of
the stack
Pop() – remove the top element of the
stack and return it.
Top() – return the top element without
removing it from the stack.
Stack Operations
top 1
top 7 7
top 5 5 5
top 2 2 2 2
push(2) push(5) push(7) push(1)
top 21
top 7 7 top 7
5 5 5 top 5
2 2 2 2 top 2
1 pop() push(21) 21 pop() 7 pop() 5 pop()
Operations on Stack
A stack is generally implemented
with only two principle operations:
Push: adds an item to a stack
Pop: extracts the most recently
pushed item from the stack
Other methods such as
top: returns the item at the top
without removing it
IsEmpty: determines whether the
stack has anything in it
IsFull: determines whether the stack
is full or stacksize is reached to
MAXST anything in it
Stack Operation
The last element to go into the stack is
the first to come out: LIFO – Last In First
Out.
What happens if we call pop() and there is
no element?
Have IsEmpty() boolean function that
returns true if stack is empty, false
otherwise.
Algorithms of Push Operation
Push (STACK , TOP, MAXST, ITEM)
Algorithm for push element into the Stack
1.[stack already filled]
If TOP= MAXSTK, then: print:
“OVERFLOW”, and return.
2.[increases TOP by 1] Set TOP:= TOP + 1
3.[insert ITEM in new TOP position ]
Set STACK[TOP]: =ITEM
4. RETRUN
Algorithms of Pop Operation
Pop (STACK, TOP, ITEM)
Algorithm for pop element from the Stack
1.[Stack has an item to be removed]
If TOP= NUL, then
Print: “UNDERFLOW” and return
2. [assign TOP element to ITEM]
Set ITEM := STACK[TOP]
3. [Decrease TOP by 1]
Set TOP:= TOP -1
4. Return
Algorithm for Display Stack elements
Display_Stack ()
Algorithm for display stack elements
1.Start
2.Repeat step 3 For i = 1 to TOP by 1
3.Print S[i]
4.End
Algorithm for isempty Stack
isempty ()
Algorithm for return the Stack
status
1.Start
2.if TOP = -1 then
Return True
else
Return False
3.End
Algorithm for return the top of stack value
top ()
Algorithm for return the top of Stack
value
1.Start
2.Return TOP
3.End
Stack Implementation: Array
Worst case for insertion and deletion from
an array when insert and delete from the
beginning: shift elements to the left.
Best case for insert and delete is at the
end of the array – no need to shift any
elements.
Implement push() and pop() by inserting
and deleting at the end of an array.
Stack using an Array
top 1
2 5 7 1
7
5 0 1 2 3 4
2 top = 3
Stack using an Array
In case of an array, it is possible that the
array may “fill-up” if we push enough
elements.
Have a boolean function IsFull() which
returns true is stack (array) is full, false
otherwise.
We would call this function before calling
push(x).
Stack Operations with Array
int pop()
{
return A[current--];
}
void push(int x)
{
A[++current] = x;
}
Stack Operations with Array
int top()
{
return A[current];
}
int IsEmpty()
{
return ( current == -1 );
}
int IsFull()
{
return ( current == size-1);
}
A quick examination shows that all five
operations take constant time.
Stack Using Linked List
We can avoid the size limitation of a stack
implemented with an array by using a
linked list to hold the stack elements.
As with array, however, we need to decide
where to insert elements in the list and
where to delete them so that push and
pop will run the fastest.