Guest User

Untitled

a guest
Oct 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 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. int count = 0;
  11.  
  12. struct Node* front = NULL;
  13. struct Node* rear = NULL;
  14. struct Node* temp;
  15.  
  16. void display()
  17. {
  18. temp = front;
  19. if((front == NULL) && (rear) == NULL)
  20. {
  21. printf("\nQueue is Empty!");
  22. return;
  23. }
  24. printf("\nThere are currently %d items:-\n", count);
  25. while(temp != rear)
  26. {
  27. printf("%d ", temp->data);
  28. temp = temp->next;
  29. }
  30. if(temp == rear)
  31. {
  32. printf("%d", temp->data);
  33. }
  34. printf("\n");
  35. }
  36.  
  37. void insert(int el)
  38. {
  39. if(rear == NULL)
  40. {
  41. rear = (struct Node*)malloc(sizeof(struct Node));
  42. rear->next = NULL;
  43. rear->data = el;
  44. front = rear;
  45. }
  46. else
  47. {
  48. temp = (struct Node*)malloc(sizeof(struct Node));
  49. rear->next = temp;
  50. temp->data = el;
  51. temp->next = NULL;
  52. rear = temp;
  53. }
  54. count++;
  55. display();
  56. }
  57.  
  58. void delete()
  59. {
  60. temp = front;
  61. if(temp == NULL)
  62. {
  63. printf("\nQueue is Empty!\n");
  64. exit(1);
  65. }
  66. else
  67. {
  68. if(temp->next != NULL)
  69. {
  70. temp = temp->next;
  71. printf("\nElement removed is %d\n", front->data);
  72. free(front);
  73. front = temp;
  74. }
  75. else
  76. {
  77. printf("\nElement removed is %d\n", front->data);
  78. free(front);
  79. front = rear = NULL;
  80. }
  81. }
  82. count--;
  83. display();
  84. }
  85.  
  86. int main()
  87. {
  88. int choice, el;
  89. while(1)
  90. {
  91. printf("\n1.Insert\n2.Delete\n3.Display\nChoice: ");
  92. scanf("%d", &choice);
  93. switch(choice)
  94. {
  95. case 1:
  96. printf("\nElement to insert: ");
  97. scanf("%d", &el);
  98. insert(el);
  99. break;
  100. case 2:
  101. delete();
  102. break;
  103. case 3:
  104. display();
  105. break;
  106. default:
  107. printf("\nInvalid Choice!");
  108. }
  109. }
  110. return 0;
  111. }
Add Comment
Please, Sign In to add comment