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; } 
  • Since you still read the entire sequence into memory - just go through it and make sure that each following number is not less than the previous one. If you meet strictly less - it means there is no orderliness in non- ... - Harry
  • @harry how to do it better? - Alexander
  • Do you have a list? Well, write a loop with a check like if (q->next && q->data <= q->next->data) - Harry
  • Always start sorting, it will be on average more efficient if it is assumed that the probability of occurrence of an ordered sequence at the input is no greater than the probability for an unordered one. - 0andriy

0