Overview

This lab is calisthenics for linked lists. These exercises are just there to get you comfortable with creating and traversing lists.
You will need the following files: in01.txt. in02.txt, in03.txt,

Makefile and Submit

Download and use Makefile and add appropriate compilation details so that your source code files will compile correctly. Use the following commands to submit your code:
~/bin/submit -c=IC210 -p=lab12 *.cpp *.h Makefile

Part 1: Linked Lists -- Printing and Counting

in01.txt:
(flatulent 5)
(smelly 3)
(is 1)
(sounds 2)
(dog 2)
(cat 2)
(a 1)
(the 1)
Your program part1 will read in each entry from a file and print them in reverse order (which happens naturally if you use our good friend add2front) along with the count of the number of elements in the list. The filename is input by the user, and you must print an error message and exit if the file isn't found.

Requirement:
This reinforces the fundamental idea that linked lists are the same no matter what kind of data you put in them.
~/$ ./part1
Input file is: in01.txt
count is 8
(the 1) (a 1) (cat 2) (dog 2) (sounds 2) (is 1) (smelly 3) (flatulent 5) 
~/$ ./part1
Input file is: in02.txt
count is 23
(a 1) (let 3) (my 2) (ring 3) (of 1) (the 1) (fire 3) (truth 3) (way 2) (is 1) (that 1) (a 1) (better 3) (scary 4) (golden 4) (ring 3) (the 1) (left 2) (sight 3) (ring 3) (out 2) (highway 4) (alone 3)

Part 2: The user iterates through the list

In the Part 2 program, after reading in the pairs as with Part 1, we let the user move through the list.

At each step we print out:

  • the list of word/value pairs before the current one,
  • the current word/value pair, and
  • how many pairs after the current one the list has.

The user is asked to either "accept" or "reject" the current word/value pair, though we don't actually act on that until Part 4 (yes, that means you do the same thing regardless of what the user enters: do nothing).

After the user has gone through the whole list, we print the entire list all at once.

Tip: Do you remember how to compute the length of a list?

Sample run:
~/$ ./part2
Input file is: in03.txt

The current node: (the 1)
Nodes before the current:
#nodes after the current: 3
[a]ccept or [r]eject: a

The current node: (dog 2)
Nodes before the current: (the 1)
#nodes after the current: 2
[a]ccept or [r]eject: a

The current node: (is 1)
Nodes before the current: (the 1) (dog 2)
#nodes after the current: 1
[a]ccept or [r]eject: a

The current node: (smelly 3)
Nodes before the current: (the 1) (dog 2) (is 1)
#nodes after the current: 0
[a]ccept or [r]eject: a

List is: (the 1) (dog 2) (is 1) (smelly 3)
in03.txt:
(smelly 3)
(is 1)
(dog 2)
(the 1) 

Part 3: Print pretty & count the score

Your Part 3 program should do:
  1. Do just what your Part 2 program did.
  2. When the list is printed in the end, print out only the words of the list,
  3. Print out the sum of all the point values in the list.
Use functions to keep your code beautiful!
Warning: Your linked list library should work both for Part 2 and Part 3.

For example, suppose you wrote some function in your library for Part 3. You realize you need to slightly modify how that function works for Part 3, so you changed it. Part 3 may work fine, but you have a problem: Part 2 won't work anymore!!

So, for this case, you should write a new (slightly different) function for Part 3 instead of modifying the already-existing function for Part 2.

Sample run:
~/$ ./part3
Input file is: in03.txt

The current node: (the 1)
Nodes before the current:
#nodes after the current: 3
[a]ccept or [r]eject: a

The current node: (dog 2)
Nodes before the current: (the 1)
#nodes after the current: 2
[a]ccept or [r]eject: a

The current node: (is 1)
Nodes before the current: (the 1) (dog 2)
#nodes after the current: 1
[a]ccept or [r]eject: a

The current node: (smelly 3)
Nodes before the current: (the 1) (dog 2) (is 1)
#nodes after the current: 0
[a]ccept or [r]eject: a

List is: the dog is smelly 
Score is: 7
in03.txt:
(smelly 3)
(is 1)
(dog 2)
(the 1) 

Part 4: Accept or Reject Words

In Part 4 we finally achieve what the other stuff was really working toward: a really stupid word game!
  • Whatever the user [a]ccepts is retained and what the user [r]ejects is thrown out.
  • The goal is, once you're through the list, to have a sentence that makes sense and maximizes the score.
  • As you go along, the "Nodes before the current: " message should print only the words [a]ccepted so far, in order. Just the words, mind you, not their point values.
  • Instead of "Nodes before the current", print "Sentence you made so far".
  • Instead of "#nodes after the current", print "#words left".
  • Instead of "List is:", print "Sentence is:".

Tips: you have two choices for making this work. The first is to actually remove the node for the [r]ejected word from the list. The second is to keep a separate list consisting of the [a]ccepted words. Both have their challenges!

Warning: You should have only one linked list library should work for Part 2, Part 3, and Part 4. That is, all parts should include the same linked list header file(s).
Sample run:
~/$ ./part4
Input file is: in01.txt

The current node: (the 1)
Sentence you made so far:
#words left: 7
[a]ccept or [r]eject: a

The current node: (a 1)
Sentence you made so far: the
#words left: 6
[a]ccept or [r]eject: r

The current node: (cat 2)
Sentence you made so far: the
#words left: 5
[a]ccept or [r]eject: r

The current node: (dog 2)
Sentence you made so far: the
#words left: 4
[a]ccept or [r]eject: a

The current node: (sounds 2)
Sentence you made so far: the dog
#words left: 3
[a]ccept or [r]eject: a

The current node: (is 1)
Sentence you made so far: the dog sounds
#words left: 2
[a]ccept or [r]eject: r

The current node: (smelly 3)
Sentence you made so far: the dog sounds
#words left: 1
[a]ccept or [r]eject: r

The current node: (flatulent 5)
Sentence you made so far: the dog sounds
#words left: 0
[a]ccept or [r]eject: a

Sentence is: the dog sounds flatulent
Score is: 10
in02.txt:
(flatulent 5)
(smelly 3)
(is 1)
(sounds 2)
(dog 2)
(cat 2)
(a 1)
(the 1)