xxxxxxxxxx
// A C program to demonstrate linked list based implementation of queue
#include <stdio.h>
#include <stdlib.h>
// A linked list (LL) node to store a queue entry
struct QNode {
int key;
struct QNode* next;
};
// The queue, front stores the front node of LL and rear stores the
// last node of LL
struct Queue {
struct QNode *front, *rear;
};
// A utility function to create a new linked list node.
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
// A utility function to create an empty queue
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
// The function to add a key k to q
void enQueue(struct Queue* q, int k)
{
// Create a new LL node
struct QNode* temp = newNode(k);
// If queue is empty, then new node is front and rear both
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}
// Function to remove a key from given queue q
void deQueue(struct Queue* q)
{
// If queue is empty, return NULL.
if (q->front == NULL)
return;
// Store previous front and move front one node ahead
struct QNode* temp = q->front;
q->front = q->front->next;
// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
// Driver Program to test above functions
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d \n", q->front->key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
// Structure to create a node with data and next pointer
struct node {
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
// Enqueue() operation on a queue
void enqueue(int value) {
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = value;
ptr->next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear->next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}
// Dequeue() operation on a queue
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node *temp = front;
int temp_data = front->data;
front = front->next;
free(temp);
return temp_data;
}
}
// Display all elements of queue
void display() {
struct node *temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("NULL\n\n");
}
}
int main() {
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
printf("Popped element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
return 0;
}
xxxxxxxxxx
class LinkedListQueue
{
private Node front, rear;
private int queueSize; // queue size
//linked list node
private class Node {
int data;
Node next;
}
//default constructor - initially front & rear are null; size=0; queue is empty
public LinkedListQueue()
{
front = null;
rear = null;
queueSize = 0;
}
//check if the queue is empty
public boolean isEmpty()
{
return (queueSize == 0);
}
//Remove item from the front of the queue.
public int dequeue()
{
int data = front.data;
front = front.next;
if (isEmpty())
{
rear = null;
}
queueSize--;
System.out.println("Element " + data+ " removed from the queue");
return data;
}
//Add data at the rear of the queue.
public void enqueue(int data)
{
Node oldRear = rear;
rear = new Node();
rear.data = data;
rear.next = null;
if (isEmpty())
{
front = rear;
}
else {
oldRear.next = rear;
}
queueSize++;
System.out.println("Element " + data+ " added to the queue");
}
//print front and rear of the queue
public void print_frontRear() {
System.out.println("Front of the queue:" + front.data
+ " Rear of the queue:" + rear.data);
}
}
class Main{
public static void main(String a[]){
LinkedListQueue queue = new LinkedListQueue();
queue.enqueue(6);
queue.enqueue(3);
queue.print_frontRear();
queue.enqueue(12);
queue.enqueue(24);
queue.dequeue();
queue.dequeue();
queue.enqueue(9);
queue.print_frontRear();
}
}
xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
// Structure to create a node with data and next pointer
struct node {
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
// Enqueue() operation on a queue
void enqueue(int value) {
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = value;
ptr->next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear->next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}
// Dequeue() operation on a queue
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node *temp = front;
int temp_data = front->data;
front = front->next;
free(temp);
return temp_data;
}
}
// Display all elements of queue
void display() {
struct node *temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("NULL\n\n");
}
}
int main() {
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
printf("Popped element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
return 0;
}
xxxxxxxxxx
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
void EnQueue();
void DeQueue();
void display();
};
node *rear=NULL;
void node::EnQueue()
{
if(rear==NULL)
{
rear=new node();
cout<<"Enter data : ";
cin>>rear->data;
}
else
{
static node *temp=rear;
node *newnode;
newnode=new node();
cout<<"Enter data : ";
cin>>newnode->data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
void node::DeQueue()
{
if(rear==NULL)
{
cout<<"The Queue is Underflow!";
}
else
{
cout<<"Dequeued element is : "<<rear->data;
rear=rear->next;
}
}
void node::display()
{
node* temp=rear;
if(rear==NULL)
{
cout<<"\nThe Queue is Empty!\n";
}
else
{
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
}
int main()
{
node obj;
int ch;
while(ch!=4){
cout<<"\n1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
obj.EnQueue();
break;
}
case 2: {
obj.DeQueue();
break;
}
case 3: {
obj.display();
break;
}
case 4: {
exit(0);
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}
}