Skip to main content

Section 1.9 Homework 9 -- Linked Lists

Exercises Exercises

1.

In this question you are given a standard linked list setup, with a Node class with members variables value and next, and a List class with a variable head. You are asked to implement a function sumAll() that will add up all the values in the list and return the result.

2.

In this question you are given a standard linked list setup that holds double values, with a Node class with members variables value and next, and a List class with a variable head. You are asked to implement a "convert constructor" that will convert a string to a list. The string is assumed to contain numbers separated by spaces, so you can read them if you turn the string into an istringstream. The sstream header has already been included, so you have access to the istringstream. For the purposes of this program, there are no insert functions available to you. You will need to do the work to create the needed nodes in your constructor.

3.

In this question you are given a standard linked list setup, with a Node class with members variables value and next, and a List class with a variable head. You are asked to implement a function insertAt(value, index) that will insert a new node with the provided value at the requested index, if that is a valid spot. On a list of size 6 for example, the valid spots are at indexes 0 through 6, 0 being the beginning of the list, 1 meaning immediately after the first element, and so on, with 6 being the spot after the last element. If you are given an invalid index (either negative or out of bounds) you should simply return without changing the list.

4.

In this question you are given a standard linked list setup, with a Node class with members variables value and next, and a List class with a variable head. You are asked to implement a function keepOddNumbered() that will change the list as follows: It will remove every second node, starting with the second node. See the examples for how this might look. You can do this in two ways: One is using a helper index variable to keep track of the index (or a boolean variable to keep track of if you are odd or even, alternating each time). There is also a way to do this that does not use any helper variables other than one-two Node* variables. This last version is a bit trickier but more interesting.
(Although you should make sure you delete every removed node, the system will not check for that).

5.

In this question you are given a standard linked list setup, with a Node class with members variables value and next, and a List class with a variable head. You are asked to implement a function reverse() that will reverse the order of all the nodes (i.e. the last node will end up being the first). This is not long, but it is not an easy function to write, make sure you have a plan and draw some pictures before starting to code. Your instructor’s solution is about 10 lines and uses 3 local variables to help keep track of stuff.
You have attempted of activities on this page.