Task: Given a sequence of real numbers a1, a2, ..., an (n> = 2 and unknown in advance). If the sequence is ordered not to decrease, then leave it unchanged, otherwise get the sequence an, a (n-1), ...., a1.
In general, you need to read from the file and check. The only question is how to check what is in the file and understand the sequence in non-decreasing order or not?
Here is what I did:
#include <stdio.h> #include <conio.h> #include <string.h> #include <locale.h> struct node { int data; node* next; }; int main() { setlocale(LC_ALL, "Russian"); FILE *hello; node *p, *q; fopen_s(&hello, "hello.txt", "r"); if (hello == NULL) { printf_s("Ошибка открытия файла"); _getch(); return 0; } int ch; p = new node; fscanf_s(hello, "%d", &ch); p->data = ch; int k = 1, j = 0; q = p; while (fscanf_s(hello, "%d", &ch) != EOF) { q -> next = new node; q = q->next; q->data = ch; } q->next = NULL; fclose(hello); } Subroutine to rotate the list too
void reverse(node*& head) { if (head == NULL || head->next == NULL) return; node* prev = nullptr; node* next = nullptr; while (head) { next = head->next; head->next = prev; prev = head; head = next; } head = prev; }
if (q->next && q->data <= q->next->data)- Harry