Advertisement
Guest User

Untitled

a guest
May 27th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //Queue with List
  6.  
  7. struct node
  8. {
  9. int data;
  10. struct node *link;
  11. };
  12.  
  13. struct node *front=NULL;
  14. struct node *rear=NULL;
  15.  
  16. void enqueue(int x)
  17. {
  18. struct node *N = new node;
  19. N->data=x;
  20. N->link=NULL;
  21.  
  22. if(rear==NULL)
  23. {
  24. rear=N;
  25. front=N;
  26. }
  27.  
  28. else
  29. {
  30. rear->link=N;
  31. rear=rear->link;
  32. rear->link=front;
  33. }
  34.  
  35. }
  36.  
  37. void dequeue()
  38. {
  39. if(front==NULL)
  40. {
  41. cout<<"No values in queue"<<endl;
  42. }
  43.  
  44. else
  45. {
  46. if (front==rear)
  47. {
  48. front=NULL;
  49. rear=NULL;
  50. }
  51.  
  52. else
  53. {
  54. front=front->link;
  55. }
  56.  
  57. }
  58. }
  59.  
  60. void display()
  61. {
  62. struct node *t =front;
  63. while(t!=rear)
  64. {
  65. cout<<t->data<<"->";
  66. t=t->link;
  67. }
  68. cout<<t->data<<endl;
  69. }
  70.  
  71. int main()
  72. {
  73. enqueue(10);
  74. enqueue(20);
  75. enqueue(30);
  76. enqueue(40);
  77. display();
  78. dequeue();
  79. display();
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement