“Implementacja kolejki za pomocą linkowanej listy w CPP” Kod odpowiedzi

Implementacja kolejki za pomocą linkowanej listy w 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!\n";
			return;
		}
		else if (front == rear)
		{
			front = rear = NULL;
		}
		else
		{
			front = front->next;
		}

		delete temp;
	}
	int getFront()
	{
		if (front == NULL)
		{
			cout << "Queue is empty!\n";
		}
		else
		{
			return front->value;
		}
	}
	void Display()
	{
		node* temp = front;
		cout << "Queue Item: ";
		while (temp != NULL)
		{
			cout << temp->value << " ";
			temp = temp->next;
		}
		cout << "\n";
	}
};
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() << "\n";  //should print [1]

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

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

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

	return 0;
}
coder

Implementacja kolejki za pomocą linkowanej listy w 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!\n";
			return;
		}
		else if (front == rear)
		{
			front = rear = NULL;
		}
		else
		{
			front = front->next;
		}

		delete temp;
	}
	int getFront()
	{
		if (front == NULL)
		{
			cout << "Queue is empty!\n";
		}
		else
		{
			return front->value;
		}
	}
	void Display()
	{
		node* temp = front;
		cout << "Queue Item: ";
		while (temp != NULL)
		{
			cout << temp->value << " ";
			temp = temp->next;
		}
		cout << "\n";
	}
};
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() << "\n";  //should print [1]

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

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

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

	return 0;
}
coder

Odpowiedzi podobne do “Implementacja kolejki za pomocą linkowanej listy w CPP”

Pytania podobne do “Implementacja kolejki za pomocą linkowanej listy w CPP”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu