Computer Science 235

Linked Lists


Note: Projects are to be completed by each student individually (not by groups of students).

In this project you will implement the List abstract data type using a linked list.

The program is given a list of commands as input. The program runs each command on a linked list and reports the result of each command to the output.

Example Input

clear
insert 0 bob
insert 1 joe
insert 0 jim
print
find joe
remove 0
remove 1
remove 0
print
find joe

Example Output

clear
insert 0 bob
insert 1 joe
insert 0 jim
print
node 0: jim
node 1: bob
node 2: joe
find joe 2
remove 0 jim
remove 1 joe
remove 0 bob
print
find joe -1

Testing

Here are some ideas for tests.

  1. Insert at the front/middle/end of the list.
  2. Remove at the front/middle/end of the list.
  3. Find at front/middle/end the list.

Commands

The following commands can be given in the input file. Each command is given on one line in the input file. Each command has an output that is written to the output file.

Clear

The clear command has the form:

clear

The clear command removes all the items from the list. The list is empty after this operation completes.

The output for the clear command has the form: (The output is the same as the input.)

clear

Insert

The insert command has the form:

insert index item

The insert command inserts 'item' into the list at the position given by 'index'. The item currently at 'index' (if any) and any subsequent items are shifted one position to the right. The 'index' parameter is an integer number between 0 and size where size is the number of items on the list. If the given 'index' is not valid, the item is not inserted into the list. However the command is still output and processing continues with the next command. The 'item' parameter is a string of non-whitespace characters. The 'item' parameter cannot contain any whitespace characters (space, tab, newline) because these characters are used to separate parameters.

The output for the insert command has the form: (The output is the same as the input.)

insert index item

Remove

The remove command has the form:

remove index

The remove command removes the item stored at the position given by 'index'. Any subsequent items are shifted one position to the left. The 'index' parameter is an integer number between 0 and size-1 where size is the number of items on the list. If the given 'index' is not valid, no item is removed from the list. However the command is still output and processing continues with the next command.

The output for the remove command has the form:

remove index item

where 'index' is the index given in the input and 'item' is the item that was removed.

If the remove command is given an invalid index the output for the remove command has the form:

remove index

Find

The find command has the form:

find item

The find command finds the position of the first occurrence of 'item' in the list. The 'item' parameter is a string of non-whitespace characters.

The output for the find command has the form:

find item index

where 'item' is the item given in the input and 'index' is the first position where 'item' is found or -1 if 'item' is not found.

Print

The print command has the form:

print

The print command outputs the items stored on the list.

The output for the print command has the form:

print
node 0: item1
node 1: item2
...

Each line of output gives the item stored in a single node on the list. Each line of output has the form:

node x: item

where 'x' is the number of the node. The first node on the list has number 0. The second node is number 1, etc. The item stored in 'node x' is output following 'node x:'.

The Nodes

Each node on the list contains:

  1. A pointer to the previous node.
  2. A pointer to the next node.
  3. The item stored in the node.

The List

The list is a double-linked list of nodes. At a minimum, a list should contain:

  1. A pointer to the first node on the list.
  2. A pointer to the last node on the list.
  3. A count of how many items are stored on the list.

Indexes

The combination of all the nodes in the list form an abstract list of items. For example, suppose a list has three nodes as shown:

node 0: jim
node 1: bob
node 2: joe
These nodes combined together form an abstract list that contains the items jim, bob, and joe.

Each item on the list has an index. An index is an integer number. The index of the first item on the list is 0. The index of the last item on the list is size-1 where size is the number of items on the list. In the list above, jim is at index 0, bob is at index 1, and joe is at index 2.

Given an index, you need to locate the node that holds the item at the given index. When locating the node that corresponds to a given index, start at the end of the list that is closest to the given index. If the index is smaller than half the size of the list, start at the beginning of the list. If the index is larger than half the size of the list, start at the end of the list. This will allow for constant time insert and remove operations at either end of the list.

Insert and remove on the ends of the list must be fast. Finding a node with a given index must start at the closest end.

Inserting

Locate the node that corresponds to the index given in the insert command. Create a new node for the item to be inserted and link it into the list in front of the node at the given index.

For example, consider the command:

insert 3 ned

applied to the list:

node 0: jim
node 1: bob
node 2: bob
node 3: joe
node 4: jim
node 5: joe

The index 3 refers to the item 'joe' in node 3. Create a new node for 'ned' and link it into the list in front of 'joe'. The resulting list is:

node 0: jim
node 1: bob
node 2: bob
node 3: ned
node 4: joe
node 5: jim
node 6: joe

The insert operation must run in constant time at either end of the list and in linear time in the middle of the list.

Inserting at the End

When the insert is positioned at the end of the list, create a new node for the item to be inserted and link it into the list after the last node on the list.

For example, consider the command:

insert 4 bob

applied to the list:

node 0: jim
node 1: bob
node 2: joe
node 3: ned

The index 4 refers to the position after node 3. Create a new node for 'bob' and link it into the list after node 3. The resulting list is:

node 0: jim
node 1: bob
node 2: joe
node 3: ned
node 4: bob

Removing

Locate the node that corresponds to the index given in the remove command. Unlink the node from the linked list of nodes.

For example, consider the command:

remove 2

applied to the list:

node 0: jim
node 1: joe
node 2: ted
node 3: zed
node 4: jim

The index 2 refers to the item 'ted' in node 2. Unlink node 2 from the list. The resulting list is:

node 0: jim
node 1: joe
node 2: zed
node 3: jim

The remove operation must run in constant time at either end of the list and in linear time in the middle of the list.

Finding

The find operation searches sequentially through the list from the first item to the last item.

The find operation gives the position of the first occurrence of the item in the list.

The find operation must run in linear time.

Using the Standard Library

You must implement the linked list using pointers and nodes. You are not allowed to use data types such as vector, list, map, set, etc from the standard library.

Implementation Requirements

  1. Use a double-linked list of nodes.
  2. When locating the node that corresponds to a given index, start at the end of the list that is closest to the given index.
  3. The insert operation must run in constant time at either end of the list and in linear time in the middle of the list.
  4. The remove operation must run in constant time at either end of the list and in linear time in the middle of the list.
  5. The find operation must run in linear time.
  6. You are not allowed to use data types such as vector, list, map, set, etc from the standard library.
  7. Operations that remove nodes from the list such as 'clear' and 'remove' must 'delete' the nodes that are removed.
  8. The program must pass a memory leak test such as valgrind.
  9. The implementation of the list must use a C++ template so that the list is able to store objects of any valid data type.
  10. Write your own code for the list. Do not use code from the book, the internet, or class notes.

Command Line

The program is run with the names of the 'Command' and 'Output' files given on the command-line. For example the program might be run like this:

lab5 command.txt output.txt

When the program is run this way the program runs the commands given in the 'command.txt' file and writes the output from the commands to the 'output.txt' file. Note that the names given on the command line may not always be 'command.txt' and 'output.txt'.