Chapter Three - DS Stack and Queue
Chapter Three - DS Stack and Queue
PUSH Algorithm:
begin
if top = n then stack full
top = top + 1
stack (top) : = item;
end
POP Algorithm:
begin
if top = 0 then stack empty;
item := stack(top);
top = top - 1;
end;
• The end at which the insertion takes place is known as the rear end,
and the end at which the deletion takes place is known as front end.
• If the first three elements are deleted from the Queue, we cannot insert more
elements even though the space is available in a Linear Queue.
• In this case, the linear Queue shows the overflow condition as the rear is
pointing to the last element of the Queue.
Circular Queue
• In Circular Queue, all the nodes are represented as circular.
• It is similar to the linear Queue except that the last element of the queue is
connected to the first element.
• It is also known as Ring Buffer, as all the ends are connected to another end.
• The representation of circular queue is shown in the below image -
Circular Queue
• The drawback that occurs in a linear queue is overcome by using the
circular queue.
• If the empty space is available in a circular queue, the new element can
be added in an empty space by simply incrementing the value of rear.
• Output restricted deque - As the name implies, in output restricted queue, deletion
operation can be performed at only one end, while insertion can be performed from both
ends.
Operations performed on queue
• The fundamental operations that can be performed on queue are listed as follows -
• Enqueue: The Enqueue operation is used to insert the element at the rear end of the
queue. It returns void.
• Dequeue: It performs the deletion from the front-end of the queue. It also returns the
element which has been removed from the front-end. It returns an integer value.
• Peek: This is the third operation that returns the element, which is pointed by the
front pointer in the queue but does not delete it.
• Queue overflow (isfull): It shows the overflow condition when the queue is
completely full.
• Queue underflow (isempty): It shows the underflow condition when the Queue is
empty, i.e., no elements are in the Queue.
Ways to implement the queue
• There are two ways of implementing the Queue:
• The above figure shows how the memory space is wasted in the array representation of
queue. In the above figure, a queue of size 10 having 3 elements, is shown.
• The value of the front variable is 5, therefore, we can not reinsert the values in the place of
already deleted element before the position of front.
• That much space of the array is wasted and can not be used in the future (for this queue).
Drawback of array implementation (cont …)
• Deciding the array size
• On of the most common problem with array implementation is the size of the
array which requires to be declared in advance.
• Due to the fact that, the queue can be extended at runtime depending upon the
problem, the extension in the array size is a time taking process and almost
impossible to be performed at runtime since a lot of reallocations take place.
• Due to this reason, we can declare the array large enough so that we can store
queue elements as enough as possible but the main problem with this
declaration is that, most of the array slots (nearly half) can never be reused. It will
again lead to memory wastage.
•