-->




c++ code queue implementation using linked list example

Leave a Comment
C++ program Linked list based queue implementation.  It uses struct, pointers, switch and functions concept. Every queue operation has its own function. User select its desired operation from menu.
It has 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);
    }


See 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 help the student to clear the concept of queue using linked list. It can be use as started code for beginners. find a full  c++ array based queue project.



0 comments:

Post a Comment