Implemented linear single-linked list. How can it be sorted by inserts? How to implement the function void Sort ()? For example, you need to put everything in reverse order?

using namespace std; struct element { int x; element *Next; }; class List { element *Head; public: List() {Head=NULL;} ~List(); void Add(int x); void Show(); void Sort(); }; List::~List() { while (Head!=NULL) { element *temp=Head->Next; delete Head; Head=temp; } } void List::Add(int x) { element *temp=new element; temp->x=x; temp->Next=Head; Head=temp; } void List::Show() { element *temp=Head; while (temp!=NULL) { cout<<temp->x<<" "; temp=temp->Next; } } int main() { int N; int x; List lst; cout<<"N = ";cin>>N; for (int i=0;i<N;i++) { cout<<i+1<<". x = "; cin>>x; lst.Add(x); } lst.Show(); } 
  • How can I double the list items? - Maryna Said
  • one
    Algorithm sorting inserts normally described even on Wikipedia . You will have to search for a place to insert from the first element, since the list is simply connected and there is no possibility to go to the previous element. What does double list items mean? - Ternvein

0