Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Node
- {
- int x;
- Node* next;
- };
- Node* pop_back(Node* &head, Node* &tail)
- {
- Node* current = head;
- while (current->next != tail)
- current = current->next;
- Node* tmp = tail;
- tail = current;
- return tmp;
- }
- int main(char fileName[244])
- {
- Node* head;
- Node* tail;
- head = nullptr;
- tail = nullptr;
- printf("Введите количество элементов очереди\n");
- int n,x;
- while (!scanf("%d", &n) || n<=0)
- {
- rewind(stdin);
- }
- Node*current = nullptr;
- int xpre;
- for (int i = 0; i < n; i++)
- {
- printf("Введите элемент очереди\n");
- if (i == 0)
- while (!scanf("%d", &x)) rewind(stdin);
- else while (!scanf("%d", &x) || x < xpre) rewind(stdin);
- xpre = x;
- if (head == nullptr)
- {
- head = (Node*)malloc(sizeof(Node));
- head->x = x;
- head->next = nullptr;
- current = head;
- }
- else
- {
- current->next = (Node*)malloc(sizeof(Node));
- current = current->next;
- current->x = x;
- current->next = nullptr;
- }
- }
- FILE*f = fopen(fileName, "wb");
- Node* c;
- while (head != tail)
- {
- c = pop_back(head, tail);
- fwrite(c, sizeof(Node), 1, f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement