Page 144 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 144
Unit 9: Stacks
Notes
Figure 9.6: Contents of the Stack after the PUSH Operation in Figure 9.5
Source: http://www.tup.tsinghua.edu.cn/Resource/tsyz/034111-01.pdf
Suppose if the POP operation is specified as POP(st). Then the content of the stack can be visualized
as in Figure 9.7.
Figure 9.7: Contents of the Stack after Applying Pop Operation in Figure 9.6
Source: http://www.tup.tsinghua.edu.cn/Resource/tsyz/034111-01.pdf
That is, the element pointed out by Top will be removed and then Top will be decremented by
1. So it points the item placed below the removed item. It is also possible to verify the item
placed at the top of the stack without removing it. This operation is called PEEP. The syntax used
for this operation is:
PEEP(st)
This operation is the combination of POP and PUSH. It is equivalent to the following statements:
i = POP(st)
PUSH(st,i)
POP removes the item from the position pointed out by Top. It is in variable i. Then using the
PUSH instruction we are inserting the same item i into the stack. Now the variable i has the
value in the topmost position of the stack.
Task Compare and contrast push and pop operation.
9.2.1 Implementation of Push Operation
The push operation can be implemented as a function and this can be shown below:
push(int element)
{
++top; //Incrementing the variable
if(top==SIZE-1)
{
LOVELY PROFESSIONAL UNIVERSITY 137