Create a node structure initialize the number variable to


The task for this problem is:

1) Create a Node structure. Initialize the number variable to -1 and the nextNode pointer to nullptr.

2) Add the even numbers between 0 and 40 to the list. Your list will therefore have 20 numbers in it at that point.

3) Display the list by using the displayList() function. 4) Go through the list and insert the odd numbers into their appropriate places by using the insertAfter() function. 5) Display the list again. 6) Delete the list.

These are my functions for the task:

1) Node * addNodeToEnd (Node * lastNode, int num); This function will add a node to the end of the list and return a pointer to it. lastNode will hold a pointer to the current last node in the list. The new node should be dynamically allocated inside the function (which means, of course, that all nodes will have to be deleted later) and inserted after lastNode.

The variable number should be set to num. The nextNode pointer should be set to nullptr.

2) void displayList (Node * firstNode); This function will display the elements of the list, ten numbers per line, each one in a five byte field. It makes no change to the list.

3) int getNumOfElements (Node * firstNode); This function should traverse the list, count how many elements are in it, and return that number. (Note that the end of the list can be detected by the presence of the nullptr.) You should not count the dummy node at the beginning as an element in the list. (If the dummy node is all that is there, then the size of the list is 0.)

4) At least one of the following functions. Either one or both will suffice. You can choose as needed.

a) Node * getElemAtPos (int pos); This function should return a pointer to the element at position pos. The function should call the getNumOfElem() function before it accesses the list. If pos is out of range, it should print an error message and return nullptr. If pos is within range, the function should return a pointer to the correct element.

b) Node * getNodeWithValue (int value); This function should traverse the list and return a pointer to the first element that has the value value. If value is not in the list, it should return a nullptr.

5) Node * insertAfter (Node * currNode, int num); This function will insert a new node into the list immediately after the node pointed to by currNode.

The new node should be dynamically allocated. The function will set the number value of the next node to num and return a pointer to the new node. That pointer can be used for the next insertion.

6) void deleteList (Node * firstNode); This function will traverse the entire list, deleting each element in turn. It is an essential cleanup function that will be executed when everything else is done. function node is this: struct Node { int number; Node * nextNode; };

Solution Preview :

Prepared by a verified Expert
Data Structure & Algorithms: Create a node structure initialize the number variable to
Reference No:- TGS01528689

Now Priced at $10 (50% Discount)

Recommended (97%)

Rated (4.9/5)