Guest User

Untitled

a guest
Nov 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void enqueue(int);
  5. void dequeue(void);
  6. void display_queue(void);
  7. void queue_size(void);
  8. void rear_and_front(void);
  9.  
  10. struct node{
  11. int data;
  12. struct node *next;
  13. }*start=NULL;
  14.  
  15. struct node *front, *rear;
  16.  
  17. int main(){
  18. int choice, ele;
  19. while(1){
  20. printf("Enter 1 to Enqueue\n");
  21. printf("Enter 2 to Dequeue\n");
  22. printf("Enter 3 to display Queue\n");
  23. printf("Enter 4 to show Queue size\n");
  24. printf("Enter 5 to display Front and Rear elements\n");
  25. scanf("%d",&choice);
  26. switch(choice){
  27. case 1: printf("Enter the element\n");
  28. scanf("%d",&ele);
  29. enqueue(ele);
  30. break;
  31. case 2: dequeue();
  32. break;
  33. case 3: display_queue();
  34. break;
  35. case 4: queue_size();
  36. break;
  37. case 5: rear_and_front();
  38. break;
  39. default: printf("Invalid choice\n");
  40. }
  41. }
  42. return 0;
  43. }
  44.  
  45. void enqueue(int ele){
  46. struct node *temp, *p;
  47. temp = (struct node*)malloc(sizeof(struct node));
  48. if(start == NULL){
  49. temp -> data = ele;
  50. temp -> next = NULL;
  51. start = temp;
  52. rear = front = temp;
  53. printf("Added %d to Queue!!\n",start->data);
  54. }
  55. else{
  56. p = start;
  57. while(p->next != NULL){
  58. p = p->next;
  59. }
  60. temp = (struct node*)malloc(sizeof(struct node));
  61. temp -> data = ele;
  62. temp -> next = NULL;
  63. p -> next = temp;
  64. rear = temp;
  65. printf("Added %d to Queue!!\n",temp->data);
  66. }
  67. }
  68.  
  69. void dequeue(){
  70. struct node *front;
  71. if (start == NULL){
  72. printf("No element to remove\n");
  73. }
  74. else{
  75. start = start -> next;
  76. front = start;
  77. printf("Item successfully removed!!\n");
  78. }
  79. }
  80.  
  81. void display_queue(){
  82. struct node *p;
  83. p = start;
  84. if (p == NULL){
  85. printf("List is empty\n");
  86. }
  87. else{
  88. while(p != NULL){
  89. printf("%d\t",p->data);
  90. p = p->next;
  91. }
  92. printf("\n");
  93. }
  94. }
  95.  
  96. void queue_size(){
  97. int count = 1;
  98. if (start == NULL){
  99. printf("Queue is empty!!\n");
  100. }
  101. else{
  102. while(start -> next != NULL){
  103. count++;
  104. start = start->next;
  105. }
  106. printf("Size of queue : %d\n", count);
  107. }
  108. }
  109.  
  110. void rear_and_front(){
  111. if(start == NULL){
  112. printf("Queue is empty!\n");
  113. }
  114. else{
  115. printf("FRONT -> %d\t",front -> data);
  116. printf("REAR -> %d\n",rear -> data );
  117. }
  118. }
Add Comment
Please, Sign In to add comment