Queue using linked list in C++

Hai Teman-Teman semua disini saya akan sharing sedikit tentang contoh program dari Queue using Linked List,ini dia code nya sob:



QUEUE LINKED LIST IN C++
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
typedef struct node
         { char data;
           struct node *next;
           } *tipequeue;
typedef struct
         { tipequeue front,rear;
           } queue;
queue Q1;
void initqueue(queue &Q)
{ Q.front = NULL; Q.rear=NULL;
}
void enqueue(queue &Q,char x)
{
 tipequeue b;
   b=new node;
   b->next=NULL;
   b->data=x;
   if (Q.rear==NULL) {Q.front=b;Q.rear=b;}
   else { Q.rear->next=b;
          Q.rear = b;
   }      
}
void dequeue(queue &Q,char &x)
{tipequeue t;
  if (Q.front!=NULL) {   
    x=Q.front->data;
    t=Q.front;
    Q.front=t->next;
    free(t);
    if(Q.front==NULL) Q.rear=NULL;
  } else cout<<"Empty\n";           
}
void PrintQueue(queue Q)
{ tipequeue t;
   if(Q.front!=NULL) {
      t=Q.front;
      while(t!=NULL) {
         cout<<t->data<<" ";
         t=t->next;
      } 
   cout<<endl; 
  }
}
    
main()
{
 int i,n;
 char x;
  cout<<"The Number of data : ";cin>>n;
  initqueue(Q1);
  for (i=1;i<=n;i++) {
    x=(65+rand()%26);
 //   cout<<x<<" ";  
    enqueue(Q1,x);
  }
  cout<<endl;
  PrintQueue(Q1);
  dequeue(Q1,x);
  PrintQueue(Q1);
  x='X';
  enqueue(Q1,x);
  PrintQueue(Q1);
  dequeue(Q1,x);
  PrintQueue(Q1);
  dequeue(Q1,x);
  PrintQueue(Q1);
  x='Y';
  enqueue(Q1,x);
  PrintQueue(Q1);
  dequeue(Q1,x);
  PrintQueue(Q1);
  dequeue(Q1,x);
  PrintQueue(Q1);
  dequeue(Q1,x);
  PrintQueue(Q1);
  dequeue(Q1,x);
  PrintQueue(Q1);
  x='L';
  enqueue(Q1,x);
  PrintQueue(Q1);
  getch();
}
Author image
About the Author :

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum.

Connect with him on :