0% found this document useful (0 votes)
27 views

Stack and Queue

The document discusses different queue data structures and their operations. It describes implementing a stack using a singly linked list and the push and pop algorithms. Priority queues are defined as queues where each element has an associated priority and are served according to that priority. The operations for a linked queue are presented, including the insert and deletion algorithms. Finally, different types of queues are defined such as linear, circular, priority, and deque (double ended) queues.

Uploaded by

aritra ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Stack and Queue

The document discusses different queue data structures and their operations. It describes implementing a stack using a singly linked list and the push and pop algorithms. Priority queues are defined as queues where each element has an associated priority and are served according to that priority. The operations for a linked queue are presented, including the insert and deletion algorithms. Finally, different types of queues are defined such as linear, circular, priority, and deque (double ended) queues.

Uploaded by

aritra ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

 Implement stack using singly linked list

Push Operation algorithm

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

POP Operation algorithm

Step 1 – Check whether TOP == NULL of Stack.


Step 2 – If TOP == NULL then print “Stack is Empty” and terminate the function
Step 3 – If TOP != NULL, then define a Node pointer ‘temp’ and set it to ‘top’.
Step 4 – Then set ‘top = top → next’.
Step 5 – Finally, delete ‘temp’. (free(temp)).

Display Operation Algorithm

Step 1 – Check whether TOP == NULL of Stack.


Step 2 – If TOP == NULL then print “Stack is Empty” and terminate the function
Step 3 – If TOP != NULL, then define a Node pointer ‘ptr’ and initialize with top.
Step 4 – Display ‘temp → data’ util temp → next != NULL.
Step 5 – Finally Display ‘temp → data’.

 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.

 Operation on Linked Queue


Insert Operation algorithm

Step 1: Allocate the space for the new node PTR


Step 2: SET PTR -> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
Step 4: END

Deletion Operation algorithm

Step 1: IF FRONT = NULL


Write " Underflow "
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END

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.

You might also like