480 Programming and Data Structures
The va*?ue of m is initially zero. The sum obtained is again subtracted from m. This is because if
negative numbers are entered, they are to be considered in the sorting. For example, the value of
sum=20. In real execution, the sum may be different depending on integers entered.
Value of m would be m= - 20 (as pre m = m - 20 when m = 0).
Thus, in the while loop value of m varies from -20 to 20. In this range, all the entered elements are
covered. The value of m changes from -20 to 20, that is, -20, -19 upto +20 in ascending order. Thus,
the same order is applied while saving elements in array C[].
14.11 REPRESENTATION OF A STACK
Stack is an important tool and extensively used in programming languages. Stack is one of the most
essential linear data structures. Implementation of most of the system programs is based on stack
data structure. We can insert or delete an element from a list, which takes place from one end. A
subclass of lists permits the same operations. A linear list belonging to this subclass is called a stack.
The insertion operation of an element onto the stack is called as "push" and deletion operation is
called "pop." Due to the push operation from one end, elements are added in the stack, the stack is
also known as pushdown list. The most and least reachable elements in the stack are known as the
"top" and "bottom" of the stack, respectively. A stack is an arranged collection of elements into
which new elements can be inserted or from which new elements can be deleted at one end. Stack is
a set of elements in a last-in-first-out technique. The end where the insertion or deletion operation is
carried out is called top. In Fig. 14.6 a stack is shown.
Top of the stack
Fig. 14.6 Stack
As shown in Fig. 14.6, the stack is based on the rule last-in-first out. The element appended lastly is
deleted first. If we want to delete element 3, it is necessary to delete first top elements 5 and 4.
You can see in Fig. 14.7, a pot containing plates one above the other. Plates can be inserted or removed
from the top. If some plate is to be removed, the removal operation has to be carried out from the
top. Thus, placing or removing takes place from top, that is, from the same end.
Fig. 14.7 Stack