Advertisement
sherry_ahmos

Untitled

Dec 14th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class node{
  5.     public:
  6.     int data;
  7.     node *next;
  8.     node(int d){
  9.         data = d;
  10.         next=NULL;
  11.     }
  12. };
  13. class queue{
  14.  
  15. public:
  16.     node*front;
  17.     node*rear;
  18.  
  19. queue(){
  20.   front=NULL;
  21.   rear=NULL;
  22.   }
  23.   void enqueue(int x);
  24.   void dequeue();
  25.   void print();
  26. };
  27. void queue :: enqueue(int x){
  28.     node* p = new node(x);
  29.      //p->data=x;
  30.  if(rear==NULL){
  31.    rear=p;
  32.    front=rear;
  33.    rear->next=NULL;
  34.    front->next=NULL;
  35.  }else{
  36.      rear->next=rear;
  37.      rear=p;
  38.  }
  39. }
  40. void queue :: dequeue(){
  41.     if(front == NULL){
  42.        cout<<"List is empty"<<endl;
  43.     }else{
  44.        front=front->next;
  45.     }
  46. }
  47. void queue :: print (){
  48. if(front==NULL){
  49.     cout<<"List is empty"<<endl;
  50. }else{
  51.  node *p=front->next;
  52.  while(p != NULL){
  53.     cout<<p->data;
  54.     p=p->next;
  55.  }
  56.   cout<<rear->data;
  57. }
  58. }
  59. int main()
  60. {
  61.     queue q;
  62.     q.enqueue(1);
  63.     q.enqueue(2);
  64.     q.enqueue(3);
  65.     q.enqueue(4);
  66.     q.enqueue(5);
  67.     q.enqueue(6);
  68.     q.enqueue(7);
  69.     q.enqueue(8);
  70.     q.print();
  71.     /*cout<<"After delete"<<endl;
  72.     q.dequeue();
  73.     q.print();*/
  74.     return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement