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: in00.txt, in01.txt, in02.txt, in03.txt, in04.txt.

Part 1: Super-simple linked list [part1.cpp all in one file!]

in00.txt:
flatulent
smelly
is
sounds
dog
cat
a
the
Your Part 1 program will read in strings 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:

You must use a linked list to store the input strings.

~/$ ./part1
Input file is: in00.txt
Count is 8
the a cat dog sounds is smelly flatulent
~/$ ./part1
Input file is: in01.txt
Count is 23
a let my ring of the fire truth way is that a better scary golden ring the left sight ring out highway alone 
~/$ ./part1
Input file is: asdf
Error! File 'asdf' not found!

submit:

~/bin/submit -c=IC210 -p=lab11 part1.cpp

Part 2: Linked lists are the same no matter what the data [part2.cpp all in one file!]

in02.txt:
(flatulent 5)
(smelly 3)
(is 1)
(sounds 2)
(dog 2)
(cat 2)
(a 1)
(the 1)
Your Part 2 program is just like your Part 1 solution. However,

Requirement:

Make a new struct for representing pairs and make that the type of data rather than mucking up Node with multiple data members representing "the data".

This reinforces the fundamental idea that linked lists are the same no matter what kind of data you put in them.
~/$ ./part2
Input file is: in02.txt
count is 8
(the 1) (a 1) (cat 2) (dog 2) (sounds 2) (is 1) (smelly 3) (flatulent 5) 
~/$ ./part2
Input file is: in03.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)

submit:

~/bin/submit -c=IC210 -p=lab11 part*.cpp

Part 2.5: Split into multiple files!

First, do this:

cp part2.cpp part25.cpp

In what follows, be careful to NOT modify part2.cpp.

Hopefully you have written a nice program with multiple structs and nicely chosen functions so that main() is simple and beautiful. Now break this into multiple files: part25.cpp for main, lab11.h for your structs and protptypes, and lab11.cpp for your function definitions.

It's a bit cheesy to force you to have only one .h/.cpp pair and to force you to name it "lab11.h" and "lab11.cpp", but it'll help with the submit system for the following parts.
Note: No submit for this part! BUT, make sure that you can actually *compile* and run your new version, before you move on.

Part 3: The user iterates through the list [part3.cpp, lab11.h, lab11.cpp]

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

At each step we print out:

  • the list of all word/value pairs before the current one,
  • the current word/value pair, and
  • the number of word/value pairs remaining after the current one.

The user is asked to either "accept" or "reject" the current word/value pair, though we don't actually act on that until Part 5 (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?

submit:
~/bin/submit -c=IC210 -p=lab11 part*.cpp lab11.*
Sample run:
~/$ ./part3
Input file is: in04.txt

Before is: 
Current is: (the 1)
Number after is: 3
[a]ccept or [r]eject: a

Before is: (the 1) 
Current is: (dog 2)
Number after is: 2
[a]ccept or [r]eject: a

Before is: (the 1) (dog 2) 
Current is: (is 1)
Number after is: 1
[a]ccept or [r]eject: a

Before is: (the 1) (dog 2) (is 1) 
Current is: (smelly 3)
Number after is: 0
[a]ccept or [r]eject: a

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

Part 4: Print pretty & count the score [part4.cpp, lab11.h, lab11.cpp]

Your Part 4 program should do:
  1. Do just what your Part 3 program did.
  2. At the very end, print out
    1. the words only (not the points) of the list, in list order
    2. the sum of all the point values in the list
Use functions to keep your code beautiful!
Warning: Your lab11.h and lab11.cpp should work both for Part 3 and Part 4.

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

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

submit:

~/bin/submit -c=IC210 -p=lab11 part*.cpp lab11.*
Sample run:
~/$ ./part4
Input file is: in04.txt

Before is: 
Current is: (the 1)
Number after is: 3
[a]ccept or [r]eject: a

Before is: (the 1) 
Current is: (dog 2)
Number after is: 2
[a]ccept or [r]eject: a

Before is: (the 1) (dog 2) 
Current is: (is 1)
Number after is: 1
[a]ccept or [r]eject: a

Before is: (the 1) (dog 2) (is 1) 
Current is: (smelly 3)
Number after is: 0
[a]ccept or [r]eject: a

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

Part 5: Going Further — Making it interesting [part5.cpp, lab11.h, lab11.cpp]

In Part 5 we finally acheive what the other stuff was really working toward: a really stupid word game! Now 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 "Before is:" message should print only the words [a]ccepted so far, in order. Just the words, mind you, not their point values.

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: Your lab11.h and lab11.cpp should work for Part 3, Part 4, and Part 5.

submit:

~/bin/submit -c=IC210 -p=lab11 part*.cpp lab11.*
Sample run:
~/$ ./part5
Input file is: in02.txt

Before is: 
Current is: (the 1)
Number after is: 7
[a]ccept or [r]eject: a

Before is: the 
Current is: (a 1)
Number after is: 6
[a]ccept or [r]eject: r

Before is: the 
Current is: (cat 2)
Number after is: 5
[a]ccept or [r]eject: r

Before is: the 
Current is: (dog 2)
Number after is: 4
[a]ccept or [r]eject: a

Before is: the dog 
Current is: (sounds 2)
Number after is: 3
[a]ccept or [r]eject: a

Before is: the dog sounds 
Current is: (is 1)
Number after is: 2
[a]ccept or [r]eject: r

Before is: the dog sounds 
Current is: (smelly 3)
Number after is: 1
[a]ccept or [r]eject: r

Before is: the dog sounds 
Current is: (flatulent 5)
Number after is: 0
[a]ccept or [r]eject: a

List 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)