Professional Documents
Culture Documents
Stack 3
32
Chapter 3- Stack
Objectives
To understand the concept of stack
To learn how to implement the stack
To understand the various applications of stack
A tree can be defined in several ways. One natural way to define a tree is recursively. A
tree is a collection of nodes. The collection can be empty, which is sometimes denoted as A.
Otherwise, a tree consists of a distinguished node r, called the root, and zero or more (sub) trees
T1, T2, . . . ,Tk, each of whose roots are connected by a directed edge to r. The root of each
subtree is said to be a child of r, and r is the parent of each subtree root.
A stack is a linear structure in which item may be added or removed only at one end.
There are certain frequent situations in computer science when one wants to restrict
insertions and deletions so that they can take place only at the beginning or the end of the end of
the list, not in the middle. Two of the Data Structures that are useful in such situations are Stacks
and queues. A stack is a list of elements in which elements may be inserted or deleted only at one
end, called the Top. This means, in particular, the elements are removed from a stack in the
reverse order of that which they are inserted in to the stack. The stack also called "last-in first -
out (LIFO)” list.
Top
3
2
6
Figure 3.1 - Stack
33
Chapter 3- Stack
Example
1. Practical daily life: A pile of heavy books kept in a vertical box, dishes kept one on
top of another.
Stack data structure can be implemented using arrays. All the insertion (push) and deletion
operation (pop) can be performed in LIFO (Last In First Out) manner. Here, the elements can be
inserted up to the size of the array. The operations can be performed under stack is push(), pop()
and top ( ).
Operations
Push
Pop
Top
(i) Push
This is used to push an element into the stack using an array. Whenever the stack is full, we
cannot insert any item into the stack. Otherwise, we push an item by increment the top pointer by
1 and assign the item into the top position.
Algorithm
34
Chapter 3- Stack
Example
2. Insert 23 into the stack. Move the top pointer one step ahead and assign an item into the top
position.
35
Chapter 3- Stack
3. Similarly, insert the elements -8, 16, 27, 14, 20 into the stack. Now the stack is FULL, since it
has 7 elements.
(ii) Pop
This is used to delete or pop an element from the stack. Initially check whether the stack
is empty or not. If it is empty then return FALSE. Otherwise decrement the top pointer by 1.
Then the last inserted item will be deleted automatically.
Example
1. Pop an from from the stack. Stack contains 11, 23, -8, 16, 27, 14 and 20. From this, the last
inserted tiem 20 will be deleted and the top pointer will be decremented.
36
Chapter 3- Stack
Algorithm
(iii) Top
This is used to return the last inserted item. First we check whether the stack is empty or not. If
the stack is empty then return NULL. Otherwise it returns the last inserted item i.e., the top
element.
Algorithm
Top(array)
{
if(top<=0)
print “stack is empty”
else
return array[top];
}
(i) push()
This is the function which is for insertion(pushing)of an element into stack. It is similar to the
insertion of an element at the beginning of a single linked list. The steps to be followed,
37
Chapter 3- Stack
(ii) pop()
This is the function which is for deletion (popping up) of an element from the stack. It is
similar to the deletion of an element at the beginning of a single linked list. Before deleting the
element, check whether the stack(linked list) is empty or not. If it is empty then return error
otherwise delete the first element from the linked list by rearranging the pointer.
top 5 9 12
38
Chapter 3- Stack
Algorithm
(iii) top()
This function is used to return he top element in the stack which means that the last inserted
element.
Algorithm
39
Chapter 3- Stack
struct stack
{
int data;
struct stack *next;
};
typedef struct stack node;
node *top = NULL;
void main()
{
int choice;
void push();
void pop();
void display();
clrscr();
while(1)
{
printf(“\t\t\t Stack \n\n\n”);
printf(“\t 1. Push \n”);
printf(“\t 2. Pop \n”);
printf(“\t 3. Display \n”);
printf(“\t 4. Exit \n”);
printf(“\t Enter Your Choice:\n”);
scanf(“%d”,&choice);
switch (choice)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4: exit(0);
}/* switch end*/
} /*while end*/
}/*main end*/
40
Chapter 3- Stack
41
Chapter 3- Stack
{
printf(“%d---------- >”, temp->data);
temp = temp->next;
}
return;
}
1. Balancing symbols
2. Evaluating postfix expression
3. Conversion from infix to postfix form
4. Recursive function call
Step 1: Read symbols in the expression one by one and do steps 2-3 till the
end of the expression.
Step 2: If symbol is opening parenthesis then
Push the symbol onto a stack
Step 3: If symbol is closing parenthesis then
Pop the top element (corresponding open parenthesis) from the stack
If pop returns Null (i.e. stack empty) then
Print “Excess closing parenthesis”
Return
Step 4: If stack is not empty then
Print “Excess opening parenthesis”
Else
Print “ Parenthesis balanced”
Step 5: End
42
Chapter 3- Stack
Algorithm
Step 1: Read the characters of postfix expression one by one and do steps 2-3 till
the end of the expression.
Step 2: If the current character is an operand then
Push it onto Stack.
Step 3: If the current character is an operator then
a. Pop an element and store as operand2.
b. Pop an element and store as operand1.
c. Evaluate the expression result = Operand1 Operator Operand2
d. Push result onto the Stack.
Step 4: End.
Example
2 5
5 5
6 6
5 40
5 5
6 6
43
Chapter 3- Stack
45 45
6 6
7. + is read; Pop 3, 45; result = 3+45=48; 8. * is read; Pop 48, 6; result = 48*6= 288;
Push 48 Push 288. This is the result of the postfix
expression
48
6 288
44
Chapter 3- Stack
b. While operator at top of stack has high precedence over the current
Character and stack is not empty
(i) Pop the stack
(ii) Add the popped character to the postfix string
(iii) Push the operator onto the stack
Step 6: Repeat steps 3-5 till all the characters are read.
Step 7: While stack is not empty
a. Add operator in top of stack to postfix string.
b. Pop the stack.
Step 8: Return the postfix string.
Step 9: End
Example
Convert the Infix Expression: a + b * c + (d * e + f) * g into the postfix expression
1. Read a. It is a character. Write ‘a’ into 2. Read ‘*’. The precedence of ‘*’ is the
postfix. Read ‘+’. The stack is empty. greater than ‘+’. So, push ‘*’ into the
Push ‘+’ into the stack. Read ‘b’. Write ‘b’. Stack. Read ‘c’. Write ‘c’.
ab output abc
Postfix Postfix
*
+
+
Stack Stack
3. Read ‘+’. The precedence of ‘+’ is not greater 4. Read ‘(‘. Push it into the stack. Next
than the top element ‘*’ and the next element ‘+’. read ‘d’. Write it into the postfix.
So pop the element ‘*’ , ‘+’ and write it into the
postfix. Now push the input character ‘+’ into
the stack.
45
Chapter 3- Stack
abc*+ abc*+d
Postfix Postfix
+ +
Stack Stack
5. Read ‘*’. Push it to the stack. Next, read 6. Read ‘+’. The precedence of ‘+’ is
‘e’. Write ‘e’ into the postfix. smaller than the top element ‘*’. Pop ‘*’ and
write it into the postfix. Push ‘+’ into the
stack. Next, read ‘f’. Write it into the
postfix.
abc*+de abc*+de*f
Postfix Postfix
* +
( (
+ +
Stack Stack
7. Read ‘)’. Pop the operators from the stack 8. Read ‘*’. Push it into the stack. Read
upto ‘(‘. Stack contains only one character ‘+’ ‘g’. Write it into the postfix.
after ‘(‘. So, pop ‘+’ from stack and write it
into the postfix.
abc*+de*f+
abc*+de*f+g
Postfix Postfix
*
+
+
46
Chapter 3- Stack
9. Finally, the stack contains two operators ‘*’, ‘+’. Pop those operators and write it into the
postfix. Now the postfix expression is abc*+de*f+g*+
abc*+de*f+g*+
Postfix string
47
Chapter 3- Stack
For the above code, the stack can store the details of the factorial function is shown in figure 3.6.
Initially we start with n=5. Then call the factorial function recursively with 5*fact (4). The figure
shows the partial implementation of the factorial function.
3.6 Summary
Stack is a linear data structure. LIFO list. It has two operations namely push and pop. The
operations are done at top of the stack. It can be implemented using arrays and also in linked
list.
48