Page 147 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 147
Fundamentals of Data Structures
Notes top—; // decrementing top to hold the index of next element
}
Line 1 marks the beginning of the pop function and it does not accept any argument. Line 3
checks a condition if the stack is empty or not, it is essential to check it because if there are no
elements in the stack it does not make any sense to perform deletion operation.
Did u know? The stack empty condition is even known as stack underflow.
Hence the condition top==–1 states if the value of top is –1 than logically the stack does not has
any element. Immediately line 3 executes the exit() function which does not allow to go further
in the function.
Figure 9.11: TOP = –1, Stack is Empty
Source: http://newtonapples.com/operations-on-stack-c-language/
Line 8 states the declaration of a variable ele which is responsible to hold the value of the top
most element. Line 9 states that the top most element in the stack is assigned to the variable ele
and then line 10 prints the elements that will be logically deleted. Line 11 decrements the top
variable so that it hold the index value of the next element in the stack.
Figure 9.12: Line 10 Prints the Element
Source: http://newtonapples.com/operations-on-stack-c-language/
140 LOVELY PROFESSIONAL UNIVERSITY