Solving a Programming Problem using Queue

1 0
Read Time:57 Second

Solving a Programming Problem using Queue

#include<iostream>
using namespace std;
/*
Author: Gaurav Dixit
*/
class Node
{
	public: 
	  int info;
	  Node* next;
	  Node(int d,Node* n=NULL)
	  {
	  	this->info=d;
	  	this->next=n;
	  	
	  }
};

class Queue
{
	Node *FRONT,*REAR;
	public:
		Queue();
		void insert(int newData);
		void remove();
		void display();
};

Queue::Queue()
{
	FRONT=REAR=NULL;
}

void Queue::insert(int newData)
{
  Node* newNode;
  newNode=new Node(newData,NULL);
  
  if(FRONT==NULL)
  {
  	FRONT=newNode;
  	REAR=newNode;
  }	
  else
  {
  	REAR->next=newNode;
  	REAR=newNode;
  }
  cout<<newData<<" Number is inserted in queue"<<endl;
}
void Queue::remove()
{
	if(FRONT==NULL)
	{
		cout<<"Queue is empty:"<<endl;
		return;
	}
	else
	{
		cout<<FRONT->info<<" Number is deleted from  queue"<<endl;
		FRONT=FRONT->next;
	}
}

void Queue::display(){
	Node *curr;
	if(FRONT==NULL)
	{
		cout<<"Queue is empty:"<<endl;
		return;
	}
	else
	{
		cout<<"The queue list is..."<<endl;
		for(curr=FRONT;curr!=NULL;curr=curr->next)
		{
			cout<<curr->info<<" ";
		}
	}
	
	
}




int main()
{
	Queue q;
	q.insert(78);
	q.insert(25);
	q.insert(95);
	q.remove();
	q.display();
	return 0;
};
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment