Advertisement
shimul07

queue using linked list

Mar 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1.  
  2.  
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.     int data;
  9.     node *next;
  10. }*front = NULL,*rear = NULL,*p = NULL,*np = NULL;
  11.  
  12. void push(int x)
  13. {
  14.     np = new node;
  15.     np->data = x;
  16.     np->next = NULL;
  17.     if(front == NULL)
  18.     {
  19.         front = rear = np;
  20.         rear->next = NULL;
  21.     }
  22.     else
  23.     {
  24.         rear->next = np;
  25.         rear = np;
  26.         rear->next = NULL;
  27.     }
  28. }
  29. int remove()
  30. {
  31.     int x;
  32.     if(front == NULL)
  33.     {
  34.         cout<<"empty queuen";
  35.     }
  36.     else
  37.     {
  38.         p = front;
  39.         x = p->data;
  40.         front = front->next;
  41.         delete(p);
  42.         return(x);
  43.     }
  44. }
  45. int main()
  46. {
  47.     int n,c = 0,x;
  48.     cout<<"Enter the number of values to be pushed into queuen";
  49.     cin>>n;
  50.     while (c < n)
  51.     {
  52.     cout<<"Enter the value to be entered into queuen";
  53.     cin>>x;
  54.         push(x);
  55.         c++;
  56.      }
  57.      cout<<"nnRemoved Valuesnn";
  58.      while(true)
  59.      {
  60.         if (front != NULL)
  61.             cout<<remove()<<endl;
  62.         else
  63.             break;
  64.      }
  65.      return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement