-->





Translate

queue data structure implementation c++ code using linked list

Leave a Comment
C++ program to implement queue data structure using linked list

It uses struct, pointers, switch and functions concept. Every queue operation has its own function. User selects its desired operation from the menu.
It has the following functions

  1. Enqueue
  2. Dequeue
  3. Show front 
  4. Show All


    #include<windows.h>
    #include<iostream>
    using namespace std;

    // Queue for Linklist
    struct list
    {
     int data;
     list * next;
    };
    list *f, *c, *p, *temp;
    int linklist_counter = 0;
    void linklist_insert();
    void linklist_call();
    void linklist_dequeu();
    void linklist_show();
    void linklist_front();
    int linklist_isempty();

    int main()
    {
     linklist_call();
     return 0;
    }
    //function of LINK LIST
    void linklist_call()
    {
    linklist_start:
     system("cls");
     cout << "\t\t\t\t Welcome in linklist Queue";
     int input;
     cout << "\n 1- Enqueue \n 2- Dequeue \n 3- show list \n 4- Front\n 5- Exit\n";
     cin >> input;
     switch (input)
     {
     case 1:
      linklist_insert();
      cout << " Number entered \n";
      system("pause");
      goto linklist_start;
     case 2:
      linklist_dequeu();
      goto linklist_start;
     case 3:
      linklist_show();
      goto linklist_start;
     case 4:
      linklist_front();
      goto linklist_start;
     case 5:
      break;
     default:
      cout << " You enter invalid number ";
      system("pause");
      goto linklist_start;
     }
    }
    void linklist_insert()
    {
     c = new list;
     if (linklist_counter == 0)
     {
      f = c;
      p = c;
      cout << " Enter data ";
      cin >> c->data;
     }
     else
     {
      p->next = c;
      p = c;
      cout << " Enter data";
      cin >> c->data;
     }
     c->next = NULL;
     linklist_counter++;
    }
    void linklist_dequeu()
    {
     if (linklist_counter == 0)
     {
      cout << " Queue is empty";
      system("pause");
     }
     else
     {
      f = f->next;
      linklist_counter--;
      cout << "Number deleted \n ";
      system("pause");
     }
    }
    void linklist_show()
    {
     int emp = linklist_isempty();
     if (emp)
     {
      temp = f;
      while (temp->next != NULL)
      {
       cout << " " << temp->data;
       temp = temp->next;
      }
      cout << " " << temp->data;
     }
     else
     {
      cout << " Queue is empty";
     }
     system("pause");
    }
    void linklist_front()
    {
     int emp = linklist_isempty();
     if (emp)
     {
      cout << " " << f->data;
     }
     else
     {
      cout << " Queue is empty";
     }
     system("pause");
    }
    int linklist_isempty()
    {
     return(linklist_counter != 0);
    }


Read Also: Array based queue c++ code example
program output:



c++ program for implementing queue using linked list.
linked list queue c++


As a beginner, this code helps the student to clear the concept of the queue using a linked list. It can be used as started code for beginners. find a full  c++ array based queue project.

also read other projects here: c++ simple projects

0 comments:

Post a Comment