my code stock.com

Reham ALblihed

Queue Using Linked List
by Reham ALblihed

Opration
-  isempty
-  enqueue
- dequeue
- size
- traverse
- max
- front

Snippet options

Download: Download snippet as queue-using-linked-list.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#include<iostream>
using namespace std;

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

class queue{
private:
	node *front;
	node *rear;
public:
	queue(){
		front = rear = NULL;
	}

	bool isempty();
	void enqueue(int);
	int dequeue();
	int size();
	void traverse();
	void max();
	void fron();

};

void queue::max(){
	int max = front->info;
	for (node *curr = front; curr != NULL; curr = curr->next)
	{
		if (curr->info > max)
			max = curr->info;
	}
	cout << "max is  " << max;
}

bool queue::isempty(){
	if (front == NULL && rear == NULL)
		return true;
	else
		return false;
}

void queue::enqueue(int item)
{
	node *nw = new node;
	nw->info = item;
	nw->next = NULL;
	if (rear == NULL)
		rear = front = nw;
	else
	{
		rear->next = nw;
		rear = nw;
	}

}

int queue::dequeue(){
	if (isempty()){
		cout << "empty";
		return 0;
	}
	else {
		int item = front->info;
		node *p = front;
		if (front == rear)
			front = rear = NULL;
		else
			front = front->next;
		delete p;
		return item;
	}
}

void queue::traverse(){
	if (isempty()){
		cout << "empty";
		return;
	}
	else{
		for (node *curr = front; curr != NULL; curr = curr->next)
			cout << curr->info << "\t";
	}
}

void queue::fron(){
	cout << endl;
	cout << front->info;
}

int main(){
	int item;
	int size;
	queue x;
	cout << "enter size ";
	cin >> size;
	for (int i = 0; i < size; i++){
		cin >> item;
		x.enqueue(item);
	}

	x.traverse();
	cout << "\n";
	x.dequeue();
	x.traverse();
	x.max();
	x.fron();
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.