Queue using linked list

 

Queue using linked list






OURSHOPKEEPER








#include <iostream>
using namespace std;
struct Node
{
   int data;
   struct Node *next;
}*front = NULL, *rear = NULL;

void enQueue(int value)
{
  struct Node *newNode;
  newNode = (struct Node*)malloc(sizeof(struct Node));
  newNode -> data = value;
  newNode -> next = NULL;
  if (front == NULL)
    front = rear = newNode;
  else {
    rear -> next = newNode;
    rear = newNode;
  }
  cout<<"\nInsertion is Successful\n";
}

void deQueue()
{
  if (front == NULL)
    cout<<"\nUnderflow. Queue is Empty\n";
  else
  {
    struct Node *temp = front;
    front = front -> next;
    cout<<"\nDeleted element: \n";
    cout<< temp -> data;
    free(temp);
  }
}

void display()
{
  if (front == NULL)
  cout<<"\nQueue is Empty!\n";
  else {
    struct Node *temp = front;
    while (temp != NULL) {
    cout<<"--->";
    cout<< temp -> data;
      temp = temp -> next;
    }
  cout<<"-->NULL\n";
  }
}

int main()
{
  int choice, value;
  while(1) {

    cout<<"1. Insert\n2. Delete\n3. Display\n4. Exit\n";
  cout<<"Enter your choice: ";
 cin>>choice;
    switch(choice) {
      case 1: cout<<"Enter the value to be inserted: ";
             cin>>value;
              enQueue(value);
              break;
      case 2: deQueue();
              break;
      case 3: display();
              break;
      case 4: exit(0);
      default: cout<<"\nWrong choice. Please try again\n";
    }
  }
}






    


Click on the above button to download the code.



Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post