Computer Science II Lab 5
Lists, Stacks, and Deep Pondering
Introduction
============
Now that you've had some fun writing a game, let's continue the
challenge by using data structures to solve complex problems. In
this lab, we'll take a look at a linked list, class templates, and
doubly linked lists. Finally, we will create a stack class which
you will then use to solve a complex computer science problem.
Crafting your Data Structures (Guided)
======================================
Many problems are best solved using the proper data structures. In
this section, we will take a simple linked list and turn it into a
doubly linked list. The first step will be to copy the list.h file
from the gopher directory. This is a simple singly linked list with
an iterator. For convenience, I am listing the list.h file here,
but you can go ahead and copy my file.
Review your notes about this list class and look it over. Make sure
you understand everything in the file before proceeding. Now we are
going to convert list.h into a doubly linked list. Recall that a
doubly linked list contains pointers in both directions. The first
step is to add this previous pointer to our internal node class.
Modify your node class so that it looks like this:
class Node {
public:
int data; //the payload
Node *next; //the link
Node *prev; // the previous node
Node() { next=0x00; }
};
Now that we have the previous link established, we have to make a
few changes in the functions dealing with the links. Modify your
append function so that it looks like this:
Finally, we no longer need the private function "findPrevious" so go
ahead and remove it.
Now, before we continue, look over your modified list.h and take
think about how the whole thing works. Make sure you can get a
clear mental image of how your linked list now works!
Now, let's test our doubly linked list. Enter and compile the
following program. If it works, it should display 1 and 2 on
separate lines:
Make sure this program works properly, and that you fully understand
what it is doing before you proceed!
Now, we have a really awesome container which can store as many
integers as we want. The problem is, there are more things in
heaven and earth than are dreamt of by whole numbers! So, let's
generalize our class!
We are going to modify our list by turning it into a template.
Basically, the only thing that has to change each time we want to
store a new type is the declared type of the data element. This is
such a trivial change that we can leave that to the compiler.
Basically, the compiler will generate a new version of the class for
each data type we want to store.
The first step is to tell the compiler that the list class is not
really a class, but it is a template class. To do this, add the
following before the class List definition:
template <typename T>
Now, we have the ability to pass in a type name as a template
variable. For instance, to make a list of integers, we could do:
List<int> lst;
Now, all that remains is to modify the class so it uses the template
type instead of int. This involves modifying 6 lines of code. The
modified versions of these lines appears as follows:
void append(T data) {
void prepend(T data) {
T &operator*() {
void insertAfter(iterator &itr, T data) {
T data; //the payload
Node * createNode(T data) {
I'm not going to tell you where they are though. Finding them will
be a good learning experience for you!
Now, the last order of business before we move on to more
interesting data structures is to get your listTest.cpp working with
the template list. There is a hint on how to do this earlier in
this section. You'll have to make a couple of changes, just let the
compiler be your guide!
All right! Now that you have a templated list, let's make a stack
out of it. As we discussed in class, a stack is just a special kind
of list, but we do not want to expose all the list functions to the
users of the stack. For this reason, we use the (admittedly
controversial) private inheritance feature of C++. The template
stack.h file is listed below:
That's it! Verify that your stackTest works, and then you're on
your own!
Solving Difficult Problems with Stacks (Challenge)
==================================================
That's a nice stack class you have there! Let's see if you can put
it to good use. Remember that you can use stacks to simulate
recursion, do back tracking, and directly solve inherently stack
based problems. Take a look at our examples section on gopher to
get a good feel for how that works. After you have done that, pick
one of the problems from below and solve it using our stack class!
Problems (choose one)
---------------------
1.) Write a program which can solve sudoku puzzles. Sudoku puzzles
are a 9x9 grid where each row and column contains each number
1-9 only once. Also, each 3x3 segment of the grid can only
contain each number once. Write your program so that it can
take in a file containing a sudoku representation and prints
out the completed puzzle.
2.) Write a program which can solve the N-queens problem. The
N-queens puzzle is to place N queens on an NxN chess board so
the queens do not attack each other. Keep in mind that a queen
can move any number of squares in a straight line or in a
diagonal line. Your program should take in the number of queens
and print out a representation of the chess board with your
queens in position.
3.) Write a program which solves the knight's tour problem. The
knight's tour is a puzzle where a knight is placed on a chess
board and then moved, using only legal knight moves, so that it
visits each square exactly once. It's OK for the knight to pass
over a visited square, but it must not land on a visited
square. The program should take as input a letter and number
indicating the initial position on a chess board. It should
then print out a series of squares that the knight lands on.
It should print out 63 squares, with no revisited squares.
4.) Write a program which can convert between RPN and fully
parenthesized expressions. The program should ask the user
which they intend to enter, and then convert the input into the
other type of expression.
5.) Write a program which can solve those triangle peg puzzles
featured at certain country themed restaurants. The basic idea
is this. You start out with a triangle of pegs:
0
* *
* * *
* * * *
* * * * *
Where a * is a peg and 0 is an empty space. The goal is to
remove pegs by jumping them with other pegs and end up with only
1 peg remaining. You can only jump along the diagonal paths in
the puzzle.
For added challenge, let's make the program so that you can
enter the location of the hole. Your program should display
what the puzzle looks like after each jump.
Extra Credit
============
You get full credit for solving just one of these problems. I will
give you 25 points extra credit for other solutions. So if you do
all 5, you will get a 200 on the lab!