Question: Sorting with Linked-List Write a c++ program to read a series of integers from the user. When End-of-File (Ctrl + D) is encountered, the program

Sorting with Linked-List

Write a c++ program to read a series of integers from the user. When End-of-File (Ctrl + D) is encountered, the program output those numbers which have been sorted in ascending order.

For example, the input is 1 3 5 7 2 4 6, the output will be 1 2 3 4 5 6 7.

Hint: When you insert a value into a linked-list, you need not always add in the front or at the back of the list. You may insert in a proper position.

main.cpp is given as

#include #include "listElement.h" using std::cin;

ListElement* insertToList(ListElement* pList, int v); void printList(ListElement* p);

int main() { ListElement* pList = NULL; int n; while (cin >> n) { if (n <= 0) break; pList = insertToList(pList, n); } printList(pList); return 0; }

printList.cpp is given as

#include #include "listElement.h" void printList(ListElement* p) { while (p != NULL) { std::cout << p->value << ' '; p = p->pNext; } std::cout << std::endl; }

listElement.h is given as

struct ListElement { int value; // value of an element ListElement* pNext; // Pointer to a list element };

Note:

You should not modify the contents of either main.cpp or printList.cpp.

All you need to do is preparing a .cpp file which contains the definition of the insertToList() function.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!