1. A Brief Introduction to Stacks
Urjit Patel
MNNIT,Allahabad
August 30, 2015
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 1 / 26
2. Index
What is Stack?
Understanding LIFO
Basic operations used in stack
Implementation of stack using array and linked list
Advantages of stack
Application
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 2 / 26
3. What is Stack?
Figure: Basic model of stack
In computer science, 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, which adds an element to the collection, and pop, which removes the
last element that was added.
It is an ordered group of homogeneous items of elements.
Elements are added to and removed from the top of the stack (the most
recently added items are at the top of the stack). Refer Figure:1.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 3 / 26
4. What is Stack? (Contd.)
A stack is a restricted data structure, because only a small number of
operations are performed on it.
The nature of the pop and push operations also means that stack elements
have a natural order.
Elements are removed from the stack in the reverse order to the order of their
addition.
Now we will discuss Stack in detail but before that lets understand the term
LIFO.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 4 / 26
5. Understanding LIFO
Stack is also know as LIFO.
The term LIFO stands for LAST IN FIRST OUT.
The basic concept can be illustrated by thinking of your LIFO set as a stack
of plates or books where you can only take the top item off the stack in order
to remove things from it.
Your cannot remove a book placed in between without removing the books
placed on top of it.this is basic principle used behind stacks.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 5 / 26
6. Basic operations used in stack
Push
Push inserts an element at top of the stack.
Pop
Pop deletes the element placed at the top of the stack.
isEmpty
isEmpty checks whether the stack is empty or not.
isFull
isFull checks whether the stack is full or not.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 6 / 26
7. Implementation
Since stack is a linear list, it can be implemented using arrays or linked lists.
In the next two sections we will study the two implementations of stack.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 7 / 26
9. Push (Using array)
Function-Adds an element at the top of the stack.
Precondition-Stack has been initialized and is not full.
Post-condition-newItem is at the top of the stack.
void push(int item)
{
if isFull () then
{
printf (”StackOverflow n”);
return;
}
end if
top = top + 1;
stack [top] = item;
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 9 / 26
10. Pop (Using array)
Function-Removes topItem from stack and returns it in item.
Precondition-Stack has been initialized and is not empty.
Post-condition-Top element has been removed from stack and item is a
copy of the removed element.
int pop()
{
intitem;
if isEmpty () then
{
printf (”StackUnderflow n”);
exit (1) ;
}
end if
item = stack [arr] ;
top = top − 1;
return item;
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 10 / 26
11. isEmpty (Using array)
Function-Checks whether the stack is empty or not.
Post-condition-returns true if empty or else false.
int isEmpty()
{
if (top == −1) then
return 1;
else
return 0;
end if
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 11 / 26
12. isFull (Using array)
Function-Checks whether the stack is full or not.
Post-condition-returns true if full or else false.
int isFull()
{
if (top == MAX − 1) then
return 1;
else
return 0;
end if
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 12 / 26
14. Using Linked Lists
When the size of stack is not known in advance, it is better to implement
linked list.
We will take a single linked list so the structure of the node would be-
struct node {
int info;
struct node *link;
};
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 14 / 26
15. Push (Using Linked List)
Function-Adds an element at the top of the stack.
Precondition-Stack has been initialized and is not full.
Post-condition-newItem is at the top of the stack.
void push(int item)
{
struct node *tmp;
tmp= (stuct node ∗) malloc (struct node);
if tmp == NULL then
{
printf (”StackOverflow n”);
return;
}
end if
tmp → info = item;
tmp → link = top;
top = tmp;
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 15 / 26
16. Pop (Using Linked List)
Function-Removes topItem from stack and returns it in item.
Precondition-Stack has been initialized and is not empty.
Post-condition-Top element has been removed from stack and item is a
copy of the removed element.
int pop()
{
struct node *tmp;
if isEmpty () then
{
printf (”Stack Underflow n”);
exit (1)
}
end if
tmp = top;
item = tmp → info;
top = tmp → link;
free (tmp) ;
return item;Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 16 / 26
17. isEmpty (Using Linked List)
Function-Checks whether the stack is empty or not.
Post-condition-returns true if empty or else false.
int isEmpty()
{
if (top == NULL) then
return 1;
else
return 0;
end if
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 17 / 26
18. Array vs Linked List
Array Linked List
Accessing
Stack elements
can be randomly
Accessed using
Subscript Variable
While In Linked List
We have to Traverse
Through the Linked
List for Accessing El-
ement.
Memory Structure
The stack elements
are stored at con-
tiguous memory.
The stack elements
are stored at dif-
ferent memory lo-
cations but joined
through pointers.
Insertion deletion
As the Array ele-
ments are stored in
Consecutive memory
Locations so While
Inserting elements
we have to create
space for Insertion.
While in linked list
one has to only
change the pointers.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 18 / 26
19. Array vs Linked List (Contd.)
Array Linked List
Memory Allocation
Memory Should
be allocated at
Compile-Time in ar-
ray . i.e at the time
when Programmer
is Writing Program.
In Linked list mem-
ory can be allocated
at Run-Time , i.e
After executing Pro-
gram
Allocation type
Stack uses Static
Memory Allocation
While linked list uses
dynamic memory al-
location
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 19 / 26
20. Application of stack
The various application are
Reversing of string
Conversion of infix forms to postfix forms.
Check well-formed paranthesis.
Used in compilers to check the syntax whether is it correct or not.
We shall discuss some applications now.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 20 / 26
21. Reverse a string using stack
The basic procedure is simple.
First push each character of string into the stack.
Then pop each character out of the stack.
The resultant would be the reversed string.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 21 / 26
22. Pseudo Code for reversal
main()
{
char str[20];
int i;
printf(”Enter string:”);
gets(str);
while i < strlen (str) do
push(str[i]);
i++;
end while
while i < strlen (str) do
str[i]=pop();
i++;
end while
printf(”The reversed string is”);
puts(str);
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 22 / 26
23. Evaluation of postfix expression using stack
The steps involved to evaluate the postfix expression are-
Scan the symbol of array postfix one by one from left to right
If symbol is operand , we push it in stack.
If the symbol is operator , we pop two elements from stack and apply the
operator to these two elements.
push the result on the stack.
Lets understand with an example.
Example:-3 ∗ (4 + 5)/2 → 345 + ∗2 → 13
Remaining postfix string int stack(top)
345 + ∗2/ empty
45 + ∗2/ 3
5 + ∗2/ 3 4
+ ∗ 2/ 3 4 5
*2/ 3 9
2/ 27
/ 27 2
NULL 13 ← ans
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 23 / 26
24. Pseudo Code for evaluating post expressions
long int evalpost()
{
long int a,b,temp,result;
unsigned int i;
while i < strlen (postfix) do
if postfix [i] ≤ 9 then
push (postfix [i] − 0 );
else
a=pop();
b=pop();
switch(postfix[i])
case + : temp = b + a; break;
case − : temp = b − a; break;
case ∗ : temp = b ∗ a; break;
case / : temp = b/a; break;
case % : temp = b%a; break;
case : temp = pow(a, b); break;
}
end ifUrjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 24 / 26
25. Pseudo Code for evaluating post expressions
}
push(temp);
}
}
result=pop();
return result;
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 25 / 26