2. UNIT II
• Stacks: Introduction- Stack operations- Stack implementations. Applications:
Recursive Programming-Evaluation of Expressions.
• Queue: Introduction- Queue operations- Queue implementations- Limitation of
Linear queue.
• Circular queues: Operations on a circular queue- implementation of insertion and
deletion in circular queue- Other types of queues- Applications.
3. Stacks
• A stack is an ordered list with the restriction that elements are added
or deleted from only one end of the list termed top of stack. The
other end of the list which lies ‘
inactive’is termed bottom of stack
4. • Thus if S is a stack with three elements a, b, c where c occupies the
top of stack position, and if d were to be added, the resultant stack
contents would be a, b, c, d. Note that d occupies the top of stack
position. Again, initiating a delete or remove operation would
automatically throw out the element occupying the top of stack, viz.,
d
5.
6. • It needs to be observed that during insertion of elements into the
stack it is essential that their identities are specified, where as for
removal no identity need be specified since by virtue of its
functionality, the element which occupies the top of stack position is
automatically removed.
7. • The stack data structure therefore obeys the principle of Last In First
Out (LIFO). In other words, elements inserted or added into the stack
join last and those that joined last are the first to be removed.
8. Stack Operations
• The two operations which stack data structure supports are
• (i) Insertion or addition of elements known as Push
• (ii) Deletion or removal of elements known as Pop
9. Stack implementation
• A common and a basic method of implementing stacks is to make use
of another fundamental data structure viz., arrays. While arrays are
sequential data structures the other alternative of employing linked
data structures have been successfully attempted and applied.
10.
11.
12. • stack of four elements R, S, V, G represented by an array STACK[1:7].
In general, if a stack is represented as an array STACK[1 : n] then n
elements and not one more can be stored in the stack. It therefore
becomes essential to issue a signal or warning termed STACK_FULL
when elements whose number is over and above n are attempted to
be pushed into the stack.
13. • Again, during a pop operation, it is essential to ensure that one does
not delete an empty stack! Hence the necessity for a signal or a
warning termed STACK_EMPTY during the implementation of the pop
operation. While implementation of stacks using arrays necessitates
checking for STACK_FULL/STACK_EMPTY conditions during push/pop
operations respectively, the implementation of stacks with linked data
structures dispenses with these testing conditions.
14. Implementation of push and pop operations
• Let STACK [1:n] be an array implementation of a stack and top be a
variable recording the current top of stack position. top is initialized
to 0. item is the element to be pushed into the stack. n is the
maximum capacity of the stack.
15.
16. In the case of pop operation, as said earlier, no element
identity need be specified since by default the element
occupying the top of stack position is deleted
17. Queues
• A Queue is a linear list in which all insertions are made at one end of
the list known as rear or tail of the queue and all deletions are made
at the other end known as front or head of the queue.
• An insertion operation is also referred to as enqueuing a queue and
• A deletion operation is referred to as dequeuing a queue
18. • Q is a queue of three elements a, b, c (Fig. 5.1(a)).
• When an element d is to join the queue, it is inserted at the rear end
of the queue (Fig. 5.1(b)) and when an element is to be deleted, the
one at the front end of the queue, viz, a, is deleted automatically (Fig.
5.1(c)).
• Thus a queue data structure obeys the principle of first in first out
(FIFO) or first come first served (FCFS).
19.
20. Operations on Queues
• The queue data structure supports two operations, viz.,
• (i) Insertion or addition of elements to a queue
• (ii) Deletion or removal of elements from a queue
22. Implementation of insert and delete
operations on a queue
• Let Q[1 : n] be an array implementation of a queue.
• Let FRONT and REAR be variables recording the front and rear positions of
the queue.
• The FRONT variable points to a position which is physically one less than
the actual front of the queue.
• ITEM is the element to be inserted into the queue. n is the maximum
capacity of the queue.
• Both FRONT and REAR are initialized to 0.
23.
24.
25. Circular Queues
• As the name indicates a circular queue is not linear in structure but
instead it is circular. In other words, the FRONT and REAR variables
which displayed a linear (left to right) movement over a queue,
display a circular movement (clock wise) over the queue data
structure
26. Operations on a circular queue
• Let CIRC_Q be a circular queue with a capacity of three elements as
shown in Fig. 5.4(a). The queue is obviously full with FRONT pointing
to the element at the head of the queue and REAR pointing to the
element at the tail end of the queue. Let us now perform two
deletions and then attempt insertions of ‘d ’and ‘
e ’into the queue.
27.
28. Implementation of insertion and deletion
operations in a circular queue
• The circular movement of FRONT and REAR variables is implemented using
the mod function which is cyclical in nature. Also the array data structure
CIRC_Q to implement the queue is declared to be CIRC_Q [0: n –1] to
facilitate the circular operation of FRONT and REAR variables. As in linear
queues, FRONT points to a position which is one less then the actual front
of the circular queue. Both FRONT and REAR are initialized to 0. Note that
(n –1) is the actual physical capacity of the queue in spite of the array
declaration as [0 : n –
1]