Linear Queue in C++ using Linked List

Here you will find out about a straight line in C++.

What is the Queue?

The line is a direct information structure where tasks of inclusion and erasure are performed at

discrete finishes that are known as front and back. The line pursues FIFO (First in First Out) idea.

The first component added to the line will be the initial one to be expelled.

In this program, we will actualize a direct line utilizing connected rundown. It is a menu driven

program that contains four choices embed, erase, show and exit. The program will request

that the client enter the decision and afterwards proper capacities are conjured to perform the explicit activity as per the client’s decision.

Program for Linear Queue in C++

#include<iostream>
#include<stdlib.h>
 
using namespace std;
 
struct node
{
	int data;
	struct node *next;
}*front=NULL,*rear,*temp;
 
void ins()
{
	temp=new node;
	cout<<"Enter data:";
	cin>>temp->data;
	temp->next=NULL;
	
	if(front==NULL)
		front=rear=temp;
	else
	{
		rear->next=temp;
		rear=temp;
	}
}
 
void del()
{
	if(front==NULL)
		cout<<"Queue is empty\n";
	else
	{
		temp=front;
		front=front->next;
		cout<<"Deleted node is "<<temp->data<<"\n";
		delete(temp);
	}
}
 
void dis()
{
	if(front==NULL)
		cout<<"Queue is empty\n";
	else
	{
		temp=front;
		while(temp!=NULL)
		{
			cout<<temp->data<<"->";
			temp=temp->next;
		}		
	}
}
 
int main()
{
	int ch;
	while(1)
	{
		cout<<"\n\n*** Menu ***"<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
		cout<<"\n\nEnter your choice(1-4):";
		cin>>ch;
		cout<<"\n";
		
		switch(ch)
		{
			case 1: ins();
					break;
			case 2: del();
					break;
			case 3: dis();
					break;
			case 4: exit(0);
					break;
			default: cout<<"Wrong Choice!!!";
		}
	}
	
	return 0;
}

Output

*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
 
Enter your choice(1-4):1
 
Enter data:8
 
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
 
Enter your choice(1-4):1
 
Enter data:12
 
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
 
Enter your choice(1-4):3
8->12->

*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit

Enter your choice(1-4):4

Insertion

In the insertion activity, temp focuses on the new hub. In the event that this is the first hub to be

embedded, at that point front will be NULL and now both front and back focus on this new hub.

On the off chance that front isn’t NULL, at that point insertion is like including the hub toward

the finish of connected rundown. The following pointer of back focuses on temp and back becomes temp.

Deletion

For deletion reason, it is first checked whether the front is NULL, on the off chance that it is NULL, we display the message “Line is unfilled“. In the event that the line isn’t unfilled, deletion is done so that temp pointer focuses to front and front pointer focuses to its next hub. Subsequent to displaying information for the hub to be erased, the hub is erased by delete(temp) work.

Display

For display, it is first checked whether the front is NULL, in the event that it is NULL, we display the message “Line is vacant”. On the off chance that line isn’t unfilled, the front pointer is doled out to temp and information for every one of the hubs are displayed till temp doesn’t get NULL.

In the event that you have any questions identified with an above direct line in C++ program then you can ask it by remarking underneath.

Leave a Comment

error: Alert: Content is protected!!