Linear Data Structure 483
14.12 STACK-RELATEDTERMS
Stack: It is an array of size N, where N is an unsigned integer.
Storage: A function contains local variables and constants. These are stored in a stack. Only global
variables are stored in a stack frame.
Stack frames: This data structure holds all formal arguments, return address and local variables on
the stack at the time when a function is invoked.
TOP: The top of the stack indicates its door. The stack top is used to verify the stack's current position,
that is, underflow and overflow. Some programmers refer to -1 assigned to top as initial value. This
is because when an element is added the top is incremented and it would become zero. It is easy to
count elements because the array element counting also begins from zero. Hence, it is convenient to
assign -1 to top as initial value.
Stack underflow: When there is no element in the stack or the stack holds elements less that its
capacity, then this status of stack is known as stack underflow. In this situation, the TOP is present at
the bottom of the stack. When an element is pushed, it will be the first element of the stack and top
will be moved to one step upper.
Stack overflow: When the stack contains equal number of elements as per its capacity and no more
elements can be added such status of stack is known as stack overflow. In such a position, the top
rests at the highest position.
14.13 OPERATION ON A STACK
The following fundamental operations on a stack can be carried out through static implementation
called array implementation and the pointer implementation is known as dynamic implementation.
1) Creating a stack
2) Checking if the stack is empty or full
3) Initializing a stack
4) Insert (push) an element in the stack
5) Delete (pop) an element from the stack
6) Access the top element
7) Display elements of the stack
8) Status: to identify the present position of the stack
Push: The procedure of inserting a new element to the top of the stack is known as push operation.
After every push operation the top is incremented by one. When the array is full (if static
implementation is applied) then the status of stack is FULL and this condition is called as stack
overflow. In such a case, no element is inserted. After ever push operation the new element rests at
the top. The push operation is described is Fig. 14.11(a).
Pop: The procedure of removing an element from the top of the stack is called as POP operation.
After every pop operation, the top is decremented by one. If there is no element in the stack, this
status of stack is called as empty stack or stack underflow. In such a case, pop operation cannot be
applicable.
The stack can be supposed as a one-dimensional array. The various operations listed above can
be carried out with a one-dimensional array. The following programs explain operation listed as
given in Fig. 14.11(b).
As said earlier, an one-dimensional array can be used as a stack. We can add elements in the one
dimensional array. Using if statement we can check whether the stack is empty or full.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21