Stack and Queue
Stack and Queue
Step 1 – Allocate memory to create a newNode with given value and name it as NEW_NODE.
Step 2 – Check whether TOP == NULL of Stack
Step 3 – If TOP == NULL means empty, then set newNode → next = NULL and TOP = NEW_NODE.
Step 4 – If TOP != NULL, then set newNode → next = top and TOP = NEW_NODE.
Step 5 – END
Priority Queue
A priority queue is a special type of queue in which each element is associated with a priority and is
served according to its priority.
If there is a condition when two elements have a same number of a priority then they will be served
according to their order.
Types of Queue
Simple Queue or Linear Queue
In Linear Queue, an insertion takes place from one end while the deletion occurs from another 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. It strictly
follows the FIFO rule.
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.
Priority Queue
It is a special type of queue in which the elements are arranged based on the priority. It is a special type of queue data
structure in which every element has a priority associated with it. Suppose some elements occur with the same priority, they
will be arranged according to the FIFO principle.
There are two types of priority queue: 1. Ascending priority queue
2. Descending priority queue
Deque (or, Double Ended Queue)
In Deque or Double Ended Queue, insertion and deletion can be done from both ends of the queue either from the front or
rear. It means that we can insert and delete elements from both front and rear ends of the queue. Deque can be used as a
palindrome checker means that if we read the string from both ends, then the string would be the same.