Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct Node
  5. {
  6. int data;
  7. struct Node* next;
  8. };
  9.  
  10. // Two glboal variables to store address of front and rear nodes.
  11. struct Node* front = NULL;
  12. struct Node* rear = NULL;
  13.  
  14. // To Enqueue an integer
  15. void Enqueue(int x)
  16. {
  17. struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
  18.  
  19. temp->data =x;
  20. temp->next = NULL;
  21.  
  22. if (front == NULL && rear == NULL)
  23. {
  24. front = rear = temp;
  25. return;
  26. }
  27.  
  28. rear->next = temp;
  29. rear = temp;
  30. }
  31.  
  32. // To Dequeue an integer.
  33. void Dequeue()
  34. {
  35. struct Node* temp = front;
  36.  
  37. if (front == NULL)
  38. {
  39. printf("Queue is Empty\n");
  40. return;
  41. }
  42.  
  43. if (front == rear)
  44. {
  45. front = rear = NULL;
  46. }
  47. else
  48. {
  49. front = front->next;
  50. }
  51.  
  52. free(temp);
  53. }
  54.  
  55. void Print()
  56. {
  57. struct Node* temp = front;
  58.  
  59. while (temp != NULL)
  60. {
  61. printf("%d ",temp->data);
  62. temp = temp->next;
  63. }
  64.  
  65. printf("\n");
  66. }
  67.  
  68. int main()
  69. {
  70. // Drive code to test the implementation.
  71. // Printing elements in Queue after each Enqueue or Dequeue
  72. Enqueue(2); Print();
  73. Enqueue(4); Print();
  74. Enqueue(6); Print();
  75. Dequeue(); Print();
  76. Enqueue(8); Print();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement