Advertisement
codegod313

18_2

Jun 28th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. struct Node
  2. {
  3.     int x;
  4.     Node* next;
  5. };
  6.  
  7. Node* pop_back(Node* &head, Node* &tail)
  8. {
  9.     Node* current = head;
  10.     while (current->next != tail)
  11.         current = current->next;
  12.     Node* tmp = tail;
  13.     tail = current;
  14.     return tmp;
  15. }
  16.  
  17. int main(char fileName[244])
  18. {
  19.     Node* head;
  20.     Node* tail;
  21.     head = nullptr;
  22.     tail = nullptr;
  23.     printf("Введите количество элементов очереди\n");
  24.     int n,x;
  25.     while (!scanf("%d", &n) || n<=0)
  26.     {
  27.         rewind(stdin);
  28.     }
  29.     Node*current = nullptr;
  30.     int xpre;
  31.     for (int i = 0; i < n; i++)
  32.     {
  33.         printf("Введите элемент очереди\n");
  34.         if (i == 0)
  35.             while (!scanf("%d", &x)) rewind(stdin);
  36.         else while (!scanf("%d", &x) || x < xpre) rewind(stdin);
  37.         xpre = x;
  38.         if (head == nullptr)
  39.         {
  40.             head = (Node*)malloc(sizeof(Node));
  41.             head->x = x;
  42.             head->next = nullptr;
  43.             current = head;
  44.         }
  45.         else
  46.         {
  47.             current->next = (Node*)malloc(sizeof(Node));
  48.             current = current->next;
  49.             current->x = x;
  50.             current->next = nullptr;
  51.         }
  52.     }
  53.     FILE*f = fopen(fileName, "wb");
  54.     Node* c;
  55.     while (head != tail)
  56.     {
  57.         c = pop_back(head, tail);
  58.         fwrite(c, sizeof(Node), 1, f);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement