Chapter 4
Stack
Outlines
❑ Properties of Stack.
❑ Array Implementation of Stack.
❑ Linked List Implementation of Stack.
❑ Application of Stack.
❑ Evaluation of Algebraic Expression.
❑ Infix and Post fix (RPN) conversion.
❑ Function calls.
2
Stack
▪ Stack is a linear data structure in which the insertion and deletion
operations are performed at only one end. In a stack, adding and
removing of elements are performed at single position which is
known as "top". That means, new element is added at top of the stack
and an element is removed from the top of the stack. In stack, the
insertion and deletion operations are performed based on LIFO (Last
In First Out) principle.
3
Cont.…
▪ In a stack, the insertion operation is performed using a function
called "push" and deletion operation is performed using a function
called "pop".
In the figure, PUSH and POP operations are performed at top
position in the stack. That means, both the insertion and deletion
operations are performed at one end (i.e., at Top)
A stack data structure can be defined as follows...
▪ Stack is a linear data structure in which the operations are
performed based on LIFO principle.
4
Cont.…
▪ Stack can also be defined as
▪ "A Collection of similar data items in which both insertion and
deletion operations are performed based on LIFO principle".
▪ Example
▪ If we want to create a stack by inserting 10,45,12,16,35 and 50. Then
10 becomes the bottom most element and 50 is the top most element.
Top is at 50 as shown in the image below.
5
Cont.…
▪ The following operations are performed on the stack.
▪ Push (To insert an element on to the stack).
▪ Pop (To delete an element from the stack).
▪ Display (To display elements of the stack).
▪ Stack data structure can be implement in two ways. They are as follows.
▪ Using Array
▪ Using Linked List
▪ When stack is implemented using array, that stack can organize only
limited number of elements. When stack is implemented using linked list,
6that stack can organize unlimited number of elements.
Stack Using Array
▪ A stack data structure can be implemented using one dimensional
array. But stack implemented using array, can store only fixed number
of data values.
▪ This implementation is very simple, just define a one dimensional
array of specific size and insert or delete the values into that array by
using LIFO principle with the help of a variable 'top'. Initially top is set
to -1.
▪ Whenever we want to insert a value into the stack, increment the top
value by one and then insert. Whenever we want to delete a value from
the stack, then delete the top value and decrement the top value by one.
7
Stack Operations using Array
▪ Operations of Stack are Push, Pop and Display
// Define Size of Array and top of stack
#define SIZE 10
int stack[SIZE], top = -1;
void push(int value){
if(top == SIZE-1){
printf("\nStack is Full!!! Insertion is not
possible!!!");
}
else{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
8
Cont.…
void pop(){
if(top == -1){
printf("\nStack is Empty!!! Deletion is not
possible!!!");
}
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
9
Cont.…
void display(){
if(top == -1){
printf("\nStack is Empty!!!");
}
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--) printf("%d\n",stack[i]);
}
}
10
Stack Implementation using Linked List
▪ The major problem with the stack implemented using array is, it
works only for fixed number of data values. That means the amount of
data must be specified at the beginning of the implementation itself.
Stack implemented using array is not suitable, when we don't know
the size of data which we are going to use. A stack data structure can
be implemented by using linked list data structure. The stack
implemented using linked list can work for unlimited number of
values. That means, stack implemented using linked list works for
variable size of data. So, there is no need to fix the size at the
beginning of the implementation. The Stack implemented using linked
11
list can organize as many data values as we want.
Cont.…
▪ In linked list implementation of a stack, every new element is inserted
as 'top' element. That means every newly inserted element is pointed
by 'top'. Whenever we want to remove an element from the stack,
simply remove the node which is pointed by 'top' by moving 'top' to
its next node in the list. The next field of the first element must be
always NULL.
▪ Example:- ▪ In the example, the last inserted
node is 99 and the first inserted
node is 25. The order of elements
inserted is 25, 32,50 and 99.
12
Cont.…
▪ To Create Node We use the following Code.
struct Node
{
int data;
struct Node *next;
}
*top = NULL;
13
Linked List Implementation of Push
void push(int value){
struct Node *newNode;
newNode->data = value;
if(top == NULL){
newNode->next = NULL;
}
else{
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
}
14
Linked List Implementation of Pop
void pop(){
if(top == NULL){
printf("\nStack is Empty!!!\n");
}
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
15
Linked List Implementation of Display Stack Data
void display(){
if(top == NULL){
printf("\nStack is Empty!!!\n");
}
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
16
Expressions
▪ What is an Expression?
▪ In any programming language, if we want to perform any calculation
or to frame a condition etc., we use a set of symbols to perform the
task. These set of symbols makes an expression. An expression can be
defined as follows...
▪ An expression is a collection of operators and operands that
represents a specific value.
▪ In above definition, operator is a symbol which performs a particular
task like arithmetic operation or logical operation or conditional
operation etc.
17
Cont.…
▪ Operands are the values on which the operators can perform the
task. Here operand can be a direct value or variable or address of
memory location.
Types of Expression
▪ Based on the operator position, expressions are divided into three
types. They are as follows.
1. Infix Expression.
2. Postfix Expression.
3. Prefix Expression.
18
Infix Expression
▪ In infix expression, operator is used in between operands. The
general structure of an Infix expression is as follows...
▪ Operand1 Operator Operand2
▪ Example:-
▪ Postfix Expression:-
▪ In postfix expression, operator is used after operands. We can say that
"Operator follows the Operands". The general structure of Postfix
expression is as follows...
▪ Operand1 Operand2 Operator
▪ Example
19
Prefix Expression
▪ In prefix expression, operator is used before operands. We can say that
"Operands follows the Operator". The general structure of Prefix
expression is as follows.
▪ Operator Operand1 Operand2
▪ Example
▪ Any expression can be represented using the above three different
types of expressions. And we can convert an expression from one
form to another form like Infix to Postfix, Infix to Prefix, Prefix to
Postfix and vice versa.
20
Expression Conversion
▪ Any expression can be represented using three types of expressions
(Infix, Postfix and Prefix). We can also convert one type of expression
to another type of expression like Infix to Postfix, Infix to Prefix,
Postfix to Prefix and vice versa.
▪ To convert any Infix expression into Postfix or Prefix expression we
can use the following procedure...
▪ Find all the operators in the given Infix Expression.
▪ Find the order of operators evaluated according to their Operator
precedence.
21
Cont.…
▪ Convert each operator into required type of expression (Postfix or Prefix) in
the same order.
▪ Example
▪ Consider the following Infix Expression to be converted into Postfix
Expression.
D=A+B*C
• Step 1: The Operators in the given Infix Expression : = , + , *
• Step 2: The Order of Operators according to their preference : * , + , =
• Step 3: Now, convert the first operator * ----- D = A + B C *
• Step 4: Convert the next operator + ----- D = A BC* +
•22 Step 5: Convert the next operator = ----- D ABC*+ =
Cont.…
▪ Finally, given Infix Expression is converted into Postfix Expression as
follows.
DABC*+=
▪ Infix to Postfix Conversion using Stack Data Structure
▪ To convert Infix Expression into Postfix Expression using a stack data
structure, We can use the following steps...
▪ Read all the symbols one by one from left to right in the given Infix
Expression.
▪ If the reading symbol is operand, then directly print it to the result
(Output).
▪ 23
If the reading symbol is left parenthesis '(', then Push it on to the Stack.
Cont.…
▪ If the reading symbol is right parenthesis ')', then Pop all the
contents of stack until respective left parenthesis is poped and
print each popped symbol to the result.
▪ If the reading symbol is operator (+ , - , * , / etc.,), then Push it on
to the Stack. However, first pop the operators which are already
on the stack that have higher or equal precedence than current
operator and print them to the result.
Example Consider the following Infix Expression. ( A + B ) * ( C - D )
The given infix expression can be converted into postfix expression
using Stack data Structure as follows.
24
Cont.…
25
Cont.…
26
Cont.…
27
Cont.…
28
▪ The final Postfix Expression is as follows. A B + C D - *
Postfix Expression Evaluation
▪ A postfix expression is a collection of operators and operands in
which the operator is placed after the operands. That means, in a
postfix expression the operator follows the operands.
▪ Postfix Expression has following general structure.
▪ Operand1 Operand2 Operator
▪ Example:-
29
Postfix Expression Evaluation using Stack Data Structure
▪ A postfix expression can be evaluated using the Stack data structure.
To evaluate a postfix expression using Stack data structure we can use
the following steps...
▪ Read all the symbols one by one from left to right in the given
Postfix Expression
▪ If the reading symbol is operand, then push it on to the Stack.
▪ If the reading symbol is operator (+ , - , * , / etc.,), then perform
TWO pop operations and store the two popped operands in two
different variables (operand1 and operand2). Then perform
reading symbol operation using operand1 and operand2 and push
30
result back on to the Stack..
Cont.…
▪ Finally! perform a pop operation and display the popped value as
final result.
▪ Example:-
▪ Consider the following Expression.
31
Cont.…
32
Cont.…
33
Cont.…
34
Cont.…
35
End.
36