Question: Linked-List Write a program to read a series of positive integers from the user. The total number of input is unknown. Stop when the user

Linked-List

Write a program to read a series of positive integers from the user. The total number of input is unknown. Stop when the user supplies 0 or a negative number. Then output the series of numbers in reserve order.

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

Hint: Store the input numbers in a linked-list.

main.cpp is given as

#include #include "listElement.h" using std::cin; ListElement* addFront(ListElement* pList, int v); void printList(ListElement* p);

int main() { ListElement* pList = NULL; int n; while (true) { cin >> n; if (n <= 0) break; pList = addFront(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 addFront() 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!