How would you implement a queue using doubly linked list?

Implementation of Deque using doubly linked list

Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends. In previous post Implementation of Deque using circular array has been discussed. Now in this post we see how we implement Deque using Doubly Linked List.

Operations on Deque :

Mainly the following four basic operations are performed on queue :

insertFront[] : Adds an item at the front of Deque. insertRear[] : Adds an item at the rear of Deque. deleteFront[] : Deletes an item from front of Deque. deleteRear[] : Deletes an item from rear of Deque.

In addition to above operations, following operations are also supported :

getFront[] : Gets the front item from queue. getRear[] : Gets the last item from queue. isEmpty[] : Checks whether Deque is empty or not. size[] : Gets number of elements in Deque. erase[] : Deletes all the elements from Deque.



Priority Queue using Doubly Linked List

Given Nodes with their priority, implement a priority queue using doubly linked list.

Prerequisite : Priority Queue

  • push[]: This function is used to insert a new data into the queue.
  • pop[]: This function removes the element with the lowest priority value form the queue.
  • peek[] / top[]: This function is used to get the lowest priority element in the queue without removing it from the queue.
Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :

1. Create a doubly linked list having fields info[hold the information of the Node], priority[hold the priority of the Node], prev[point to previous Node], next[point to next Node].
2. Insert the element and priority in the Node.
3. Arrange the Nodes in the increasing order of priority.

Below is the implementation of above steps :



C++




// C++ code to implement priority
// queue using doubly linked list
#include
using namespace std;
// Linked List Node
struct Node {
int info;
int priority;
struct Node *prev, *next;
};
// Function to insert a new Node
void push[Node** fr, Node** rr, int n, int p]
{
Node* news = [Node*]malloc[sizeof[Node]];
news->info = n;
news->priority = p;
// If linked list is empty
if [*fr == NULL] {
*fr = news;
*rr = news;
news->next = NULL;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if [p priority] {
news->next = *fr;
[*fr]->prev = news->next;
*fr = news;
}
// If p is more rear node's priority,
// then insert after the rear.
else if [p > [*rr]->priority] {
news->next = NULL;
[*rr]->next = news;
news->prev = [*rr]->next;
*rr = news;
}
// Handle other cases
else {
// Find position where we need to
// insert.
Node* start = [*fr]->next;
while [start->priority > p]
start = start->next;
[start->prev]->next = news;
news->next = start->prev;
news->prev = [start->prev]->next;
start->prev = news->next;
}
}
}
// Return the value at rear
int peek[Node* fr] { return fr->info; }
bool isEmpty[Node* fr] { return [fr == NULL]; }
// Removes the element with the
// least priority value form the list
int pop[Node** fr, Node** rr]
{
Node* temp = *fr;
int res = temp->info;
[*fr] = [*fr]->next;
free[temp];
if [*fr == NULL]
*rr = NULL;
return res;
}
// Driver code
int main[]
{
Node *front = NULL, *rear = NULL;
push[&front, &rear, 2, 3];
push[&front, &rear, 3, 4];
push[&front, &rear, 4, 5];
push[&front, &rear, 5, 6];
push[&front, &rear, 6, 7];
push[&front, &rear, 1, 2];
cout next = NULL;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if [p priority] {
news->next = *fr;
[*fr]->prev = news->next;
*fr = news;
}
// If p is more rear node's priority,
// then insert after the rear.
else if [p > [*rr]->priority] {
news->next = NULL;
[*rr]->next = news;
news->prev = [*rr]->next;
*rr = news;
}
// Handle other cases
else {
// Find position where we need to
// insert.
struct Node* start = [*fr]->next;
while [start->priority > p]
start = start->next;
[start->prev]->next = news;
news->next = start->prev;
news->prev = [start->prev]->next;
start->prev = news->next;
}
}
}
// Return the value at rear
int peek[struct Node* fr] { return fr->info; }
int isEmpty[struct Node* fr] { return [fr == NULL]; }
// Removes the element with the
// least priority value form the list
int pop[struct Node** fr, struct Node** rr]
{
struct Node* temp = *fr;
int res = temp->info;
[*fr] = [*fr]->next;
free[temp];
if [*fr == NULL]
*rr = NULL;
return res;
}
// Driver code
int main[]
{
struct Node *front = NULL, *rear = NULL;
push[&front, &rear, 2, 3];
push[&front, &rear, 3, 4];
push[&front, &rear, 4, 5];
push[&front, &rear, 5, 6];
push[&front, &rear, 6, 7];
push[&front, &rear, 1, 2];
printf["%d\n", pop[&front, &rear]];
printf["%d\n", peek[front]];
return 0;
}
Java




// Java code to implement priority
// queue using doubly linked list
import java.util.*;
class Solution {
static Node front, rear;
// Linked List Node
static class Node {
int info;
int priority;
Node prev, next;
}
// Function to insert a new Node
static void push[Node fr, Node rr, int n, int p]
{
Node news = new Node[];
news.info = n;
news.priority = p;
// If linked list is empty
if [fr == null] {
fr = news;
rr = news;
news.next = null;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if [p

Bài mới nhất

Chủ Đề