Search
 
SCRIPT & CODE EXAMPLE
 

CPP

implementation of queue using linked list in c

#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

");
}

// Dequeue() operation on a queue
int dequeue() {
	if (front == NULL) {
		printf("
Underflow
");
		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("
Queue is Empty
");
	} else {
		printf("The queue is 
");
		temp = front;
		while (temp) {
			printf("%d--->", temp->data);
			temp = temp->next;
		}
		printf("NULL

");
	}
}

int main() {
	int choice, value;
	printf("
Implementation of Queue using Linked List
");
	while (choice != 4) {
		printf("1.Enqueue
2.Dequeue
3.Display
4.Exit
");
		printf("
Enter your choice : ");
		scanf("%d", &choice);
		switch (choice) {
		case 1:
			printf("
Enter the value to insert: ");
			scanf("%d", &value);
			enqueue(value);
			break;
		case 2:
			printf("Popped element is :%d
", dequeue());
			break;
		case 3:
			display();
			break;
		case 4:
			exit(0);
			break;
		default:
			printf("
Wrong Choice
");
		}
	}
	return 0;
}
Comment

queue implementation using linked list in cpp

#include<iostream>
using namespace std;

struct node
{
	int value;
	node* next;
};

class Queue
{
private:
	node* front;
	node* rear;
public:
	Queue()
	{
		front = rear = NULL;
	}
	void EnQueue(int n)
	{
		node* temp = new node();
		temp->value = n;
		temp->next = NULL;

		if (front == NULL && rear == NULL)
		{
			front = rear = temp;
			return;
		}
		rear->next = temp;
		rear = temp;
	}
	void Dequeue()
	{
		node* temp = front;

		if (front == NULL)
		{
			cout << "Queue is empty!
";
			return;
		}
		else if (front == rear)
		{
			front = rear = NULL;
		}
		else
		{
			front = front->next;
		}

		delete temp;
	}
	int getFront()
	{
		if (front == NULL)
		{
			cout << "Queue is empty!
";
		}
		else
		{
			return front->value;
		}
	}
	void Display()
	{
		node* temp = front;
		cout << "Queue Item: ";
		while (temp != NULL)
		{
			cout << temp->value << " ";
			temp = temp->next;
		}
		cout << "
";
	}
};
int main()
{
	Queue q;

	q.EnQueue(1);
	q.EnQueue(2);
	q.EnQueue(3);
	q.EnQueue(4);
	q.EnQueue(5);

	q.Display();  //should print [1 2 3 4 5]

	cout << q.getFront() << "
";  //should print [1]

	q.Dequeue();   //should delete [1]
	q.Dequeue();  //should delete [2]

	q.Display();  //should print [3 4 5]

	cout << q.getFront() << "
";  //should print [3]

	return 0;
}
Comment

implementation of queue using linked list in c

#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

");
}

// Dequeue() operation on a queue
int dequeue() {
	if (front == NULL) {
		printf("
Underflow
");
		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("
Queue is Empty
");
	} else {
		printf("The queue is 
");
		temp = front;
		while (temp) {
			printf("%d--->", temp->data);
			temp = temp->next;
		}
		printf("NULL

");
	}
}

int main() {
	int choice, value;
	printf("
Implementation of Queue using Linked List
");
	while (choice != 4) {
		printf("1.Enqueue
2.Dequeue
3.Display
4.Exit
");
		printf("
Enter your choice : ");
		scanf("%d", &choice);
		switch (choice) {
		case 1:
			printf("
Enter the value to insert: ");
			scanf("%d", &value);
			enqueue(value);
			break;
		case 2:
			printf("Popped element is :%d
", dequeue());
			break;
		case 3:
			display();
			break;
		case 4:
			exit(0);
			break;
		default:
			printf("
Wrong Choice
");
		}
	}
	return 0;
}
Comment

queue implementation using linked list in cpp

#include<iostream>
using namespace std;

struct node
{
	int value;
	node* next;
};

class Queue
{
private:
	node* front;
	node* rear;
public:
	Queue()
	{
		front = rear = NULL;
	}
	void EnQueue(int n)
	{
		node* temp = new node();
		temp->value = n;
		temp->next = NULL;

		if (front == NULL && rear == NULL)
		{
			front = rear = temp;
			return;
		}
		rear->next = temp;
		rear = temp;
	}
	void Dequeue()
	{
		node* temp = front;

		if (front == NULL)
		{
			cout << "Queue is empty!
";
			return;
		}
		else if (front == rear)
		{
			front = rear = NULL;
		}
		else
		{
			front = front->next;
		}

		delete temp;
	}
	int getFront()
	{
		if (front == NULL)
		{
			cout << "Queue is empty!
";
		}
		else
		{
			return front->value;
		}
	}
	void Display()
	{
		node* temp = front;
		cout << "Queue Item: ";
		while (temp != NULL)
		{
			cout << temp->value << " ";
			temp = temp->next;
		}
		cout << "
";
	}
};
int main()
{
	Queue q;

	q.EnQueue(1);
	q.EnQueue(2);
	q.EnQueue(3);
	q.EnQueue(4);
	q.EnQueue(5);

	q.Display();  //should print [1 2 3 4 5]

	cout << q.getFront() << "
";  //should print [1]

	q.Dequeue();   //should delete [1]
	q.Dequeue();  //should delete [2]

	q.Display();  //should print [3 4 5]

	cout << q.getFront() << "
";  //should print [3]

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to traverse a linked list in c++ 
Cpp :: pow in c++ 
Cpp :: c++ looping 
Cpp :: C++ Program to Reverse an Integer 
Cpp :: count word accurances in a string c++ 
Cpp :: conditional operator in cpp 
Cpp :: vector to string c++ 
Cpp :: copy array c++ 
Cpp :: C++ do...while Loop 
Cpp :: c++ switch case break 
Cpp :: time function c++ 
Cpp :: set precision with fixed c++ 
Cpp :: c++ 20 struct initialization 
Cpp :: vector of strings initialization c++ 
Cpp :: two pointer in c++ 
Cpp :: convert binary string to int c++ 
Cpp :: how to pass function as a parameter in c++ 
Cpp :: c++ sieve of eratosthenes 
Cpp :: C++ press enter to continue function 
Cpp :: double to int c++ 
Cpp :: get value of enum cpp 
Cpp :: sort 0 1 2 leetcode 
Cpp :: hello world program in c++ 
Cpp :: c++ string element access 
Cpp :: for loop c++ 
Cpp :: filling 2d array with 0 c++ 
Cpp :: push local branch to another remote branch 
Cpp :: bubblesort c++ 
Cpp :: how to know datatype of something in c++ 
Cpp :: stack overflow c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =