fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define size 6
  4. int fill = 0;
  5. struct q* BACK;
  6. struct q* FRONT;
  7. struct q* temp_back;
  8. struct q
  9. {
  10. int data;
  11. struct q* next;
  12. struct q* previous;
  13. };
  14.  
  15. struct q* generateQueue()
  16. {
  17. int count = 0;
  18. BACK = (struct q*)malloc(sizeof(struct q));
  19. BACK->previous = NULL;
  20. struct q* initial;
  21. initial = BACK;
  22. while(count<size)
  23. {
  24. struct q*temp;
  25. temp = (struct q*)malloc(sizeof(struct q));
  26. temp->previous = initial;
  27. initial->next = temp;
  28. initial = temp;
  29. count++;
  30. }
  31. FRONT = initial;
  32. FRONT->next = NULL;
  33. }
  34.  
  35. void enqueue(int input)
  36. {
  37. struct q* temp = BACK;
  38. int count = 0;
  39. if(size - fill >0)
  40. {
  41. while(count<(size - fill))
  42. {
  43. temp = temp->next;
  44. count++;
  45. }
  46. temp->data = input;
  47. }
  48. fill++;
  49. }
  50.  
  51. void dequeue()
  52. {
  53. struct q* temp = FRONT;
  54. int count = 0;
  55. while(count<fill-1)
  56. {
  57. struct q* temp_prev;
  58. temp_prev = temp->previous;
  59. (temp->data) = (temp_prev)->data;
  60. temp = temp_prev;
  61. count++;
  62. }
  63. temp_back = temp;
  64. fill--;
  65. }
  66.  
  67. void peek()
  68. {
  69. printf("This is the data in front of the q: %d\n", FRONT->data);
  70. }
  71.  
  72. void printQueue()
  73. {
  74. struct q* initial = FRONT;
  75. while(initial!=temp_back)
  76. {
  77. printf("%d\n", initial->data);
  78. initial = initial->previous;
  79. }
  80. }
  81. int main(void)
  82. {
  83. generateQueue();
  84. int count = size;
  85. temp_back = BACK;
  86. while (count>0)
  87. {
  88. int input;
  89. scanf("%d", &input);
  90. enqueue(input);
  91. count--;
  92. }
  93. printQueue();
  94. peek();
  95. dequeue();
  96. printf("Queue after dequeuing\n");
  97. printQueue();
  98. peek();
  99. return 0;
  100. }
Success #stdin #stdout 0s 9424KB
stdin
1
2
3
4
5
6
stdout
1
2
3
4
5
6
This is the data in front of the q: 1
Queue after dequeuing
2
3
4
5
6
This is the data in front of the q: 2