Write a program that creates a simple linked list of N integers taken from the INPUT file. The last number entered is one. These numbers will be ordered (simultaneously with the reading) in descending order by operations on the elements and then will be displayed.
I wrote the program but i can't figure out how to take the input from the file and
order the numbers.
std::ifstream ifs("INPUT.txt");
if (!ifs)
return (std::cout << "Cannot open file\n"), 1;
Then instead of using cin, you use ifs. Obviously the cout statement is also not needed. This is usually coded like:
for (int x{}; ifs >> x; Add(head, NULL, x));
However:
L8 - you don't need to use struct here
L13 Add() - why are you having p as an argument? Don't you just need head and the item to insert?
L23/24 v can be defined in the for rather than before.
L9/10 global variables are not good practice. These should be defined as part of main and passed as params to functions if required.
To insert in sorted order, you need to traverse the existing list to ascertain the position in which to insert the new item. You need to take care with a) empty list b) insertion at head c) insertion at end