fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct part {
  5. int value;
  6. struct part* next;
  7. };
  8.  
  9. struct part* tail;
  10. int count;
  11.  
  12. void initialze() {
  13. tail = NULL;
  14. count = 0;
  15. }
  16.  
  17. void enterq(int val) {
  18. struct part* p;
  19. p = malloc(sizeof(struct part));
  20. p->value = val;
  21. if (tail == NULL) {
  22. tail = p;
  23. tail->next = p;
  24. } else {
  25. p->next = tail->next;
  26. tail->next = p;
  27. tail = p;
  28. }
  29. count++;
  30. }
  31.  
  32. int removeq(void) {
  33. int val;
  34. struct part* p;
  35. if (tail == NULL) {
  36. printf("Queue empty\n");
  37. return -1;
  38. } else {
  39. p = tail->next;
  40. if (p == tail) {
  41. tail = NULL;
  42. } else {
  43. tail->next = p->next;
  44. }
  45. val = p->value;
  46. free(p);
  47. count--;
  48. return val;
  49. }
  50. }
  51.  
  52. void display() {
  53. struct part* p;
  54. p = tail;
  55. if (p != NULL) {
  56. do {
  57. p = p->next;
  58. printf("\n%d\n", p->value);
  59. } while (p != tail);
  60. }
  61. }
  62.  
  63. int main(void) {
  64. int mode, id;
  65. initialze();
  66. mode = 1;
  67. while (mode) {
  68. printf("queue process ? enter(1) or remove(0) =");
  69. scanf("%d", &mode);
  70. if (mode == 1) {
  71. printf("? id = ");
  72. scanf("%d", &id);
  73. enterq(id);
  74. } else if (mode == 0) {
  75. id = removeq();
  76. if (id > 0)
  77. printf("id = %d was removed\n", id);
  78. }
  79. display();
  80. printf("?continue(1) or quit(0) = ");
  81. scanf("%d", &mode);
  82. }
  83. return 0;
  84. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty