Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void menu(){
  5. cout << "Choose one of options" << endl;
  6. cout << "1. New queue" << endl;
  7. cout << "2. Attach" << endl;
  8. cout << "3. Delete" << endl;
  9. cout << "4. Print" << endl;
  10. cout << "5. End" << endl;
  11. }
  12.  
  13. struct el_FIFO {
  14. int x;
  15. struct el_FIFO *nast;
  16. };
  17.  
  18. struct FIFO {
  19. struct el_FIFO *front;
  20. struct el_FIFO *rear;
  21. };
  22.  
  23. void attach(struct FIFO *k, int el){
  24. struct el_FIFO *nowy = new el_FIFO;
  25. nowy->x = el;
  26. nowy->nast = NULL;
  27. if (k->front == NULL)
  28. {
  29. k->front = nowy;
  30. k->rear = nowy;
  31. }
  32. else {
  33. k->rear->nast = nowy;
  34. k->rear = nowy;
  35. }
  36. }
  37.  
  38. void del(struct FIFO *k){
  39. struct el_FIFO *temp;
  40. if (k->front!= NULL){
  41. temp = k->front->nast;
  42. delete k->front;
  43. k->front = temp;
  44. }
  45. else{
  46. cout << "There is no element to delete";
  47. }
  48. }
  49.  
  50. void new_queue(struct FIFO *k){
  51. if (k->front == NULL){
  52. cout << "queue is already empty";
  53. }
  54. else{
  55. while (k->front != NULL){
  56. del(k);
  57. }
  58. cout << "queue is empty";
  59.  
  60. }
  61. }
  62.  
  63. void print(struct FIFO *k){
  64. struct FIFO *temp_FIFO;
  65. if(k->front){
  66. cout << "queue: " << endl;
  67. while(k->front){
  68. if (temp_FIFO->front == NULL){
  69. temp_FIFO->front = k->front;
  70. temp_FIFO->rear = k->front;
  71. }
  72. else{
  73. temp_FIFO->rear->nast = k->front;
  74. temp_FIFO->rear = k->front;
  75. }
  76. cout << k->front->x << endl;
  77. del(k);
  78. }
  79. while(temp_FIFO->front){
  80. if (k->front == NULL){
  81. k->front = temp_FIFO->front;
  82. k->rear = temp_FIFO->front;
  83. }
  84. else{
  85. k->rear->nast = temp_FIFO->front;
  86. k->rear = temp_FIFO->front;
  87. }
  88. del(temp_FIFO);
  89. }
  90. }
  91. else{
  92. cout << "queue is empty";
  93. }
  94. }
  95.  
  96. int rear(){
  97. cout << "The end";
  98. return 0;
  99. }
  100.  
  101. int main()
  102. {
  103. struct FIFO *queue = new FIFO;
  104. queue->front = NULL;
  105. queue->rear = NULL;
  106. int el=0;
  107. int c=0;
  108. do{
  109. menu();
  110. cin >> c;
  111. cout << endl;
  112. switch (c)
  113. {
  114. case 1: new_queue(queue);
  115. cout << endl << endl;
  116. break;
  117. case 2: cout << "Add element" << endl;
  118. cin >> el;
  119. attach (queue, el);
  120. cout << endl;
  121. break;
  122. case 3: cout << "Element will be deleted" << endl;
  123. del(queue);
  124. cout << endl << endl;
  125. break;
  126. case 4: print(queue);
  127. cout << endl;
  128. break;
  129. case 5: rear();
  130. break;
  131. default: cout << "You chose wrong" << endl << endl;
  132. break;
  133. }
  134. } while (c!=5);
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement