CIS 22B
Assignment F

Use the style guide, including Additonal styles beginning with Assignment D.

In problem F1 we will build a linked list.

Problem F1

Copy the solution from problem E3 and make the name F1.

Keep the same order for the functions as in problem E3.
Note that the Node class is added with a private default constructor.

  1. Car constructors and destructor within the Car class definition
    1. default constructor
    2. copy constructor
    3. other constructors
    4. destructor
  2. FreightCar constructors and destructor within the FreightCar class definition
    1. default constructor
    2. copy constructor
    3. other constructors
    4. destructor
  3. PassengerCar constructors and destructor within the PassengerCar class definition
    1. default constructor
    2. copy constructor
    3. other constructors
    4. destructor
  4. Node default constructor within private area of the Node class definition
    1. default constructor (private)
  5. StringOfCars constructors and destructor within the StringOfCar class definition
    1. default constructor
    2. copy constructor (always copies only Car objects in this implementation - do not use)
    3. other constructors
    4. destructor
  6. main
  7. Car member functions declared within the Car class but defined later
    1. setup
    2. output
    3. operator=
    4. setKind
  8. FreightCar member functions declared within the FreightCar class but defined later
    1. operator=
    2. setKind
  9. PassengerCar member functions declared within the PassengerCar class but defined later
    1. operator=
    2. setKind
  10. operator== friend of Car
  11. StringOfCars member functions declared within the StringOfCars class but defined later
    1. output
    2. push for Car objects
    3. push for FreightCar objects
    4. push for PassengerCar objects
    5. pop (always returns only Car objects in this implemntation - do not use)
  12. input

Again we will change the implementation of the StringOfCars class, but keep the public interface. This implementation will use a linked list, rather than an array or vector to keep the cars in.
So keep all the public function prototypes in the StringOfCars.
Discard all the private data and the implementation of the functions in the StringOfCars class; we will rebuild all of these.

Do not change anything outside the StringOfCars class.

Build a new class called Node:

We will build our link list using Node objects, linked together to make a list.

In the private data of the Node class put two pointers:
One with type Node * and name next, which will point to the next node in the linked list.
The other with type Car* and name data, which will point to the Car data associated with this node.

Also in the private area create a default constructor that sets the next and data pointers to zero. Because the constructor is private, only friends can use this class.

In the public area of the Node class, make StringOfCars a friend class.

The order of the following three things is important:

  1. Declare the StringOfCars class with:   class StringOfCars;
  2. The Node class
  3. The StringOfCars class

This is needed because the Node class uses the StringOfCars and the StringOfCars class uses the Node class.

In the StringOfCars class implementation:

Replace the private data with two pointers of type Node *, and nothing else. Name these two pointers head and tail.

Change the StringOfCars default constructor to set the head and tail pointers to zero.

Rebuild the three push functions, with the same function headings as in problem E3.
Build each of the three push functions to include the following:
Declare a local pointer variable of type Car * named currentCarPtr.
Declare a local pointer variable of type Node * named currentNodePtr.
Use new to get space in the heap for a Node and save the address of the space in the heap in currentNodePtr
Use new get space in the heap for a new car that is a copy of the car prameter of the push function as you did in problem E3 and save the address of the space in the heap in currentCarPtr (The type will be Car for the push function with a Car parameter, FreightCar for the function with a FreightCar parameter, and PassengerCar for the function with a PassengerCar parameter)
Set the data pointer in this new Node object to point to the new car object.
If the head pointer is zero
  set the head and the tail pointer to the value of currentNodePtr
else
  set the next pointer of the Node object pointed to by the tail pointer to the value of   currentNodePtr
  set the tail pointer to the value of the currentNodePtr

Do not write a pop function. This function gets into difficulties that I do not want us to worry about in this course.

Rebuild the output function, with the same function heading.
Declare a local pointer variable of type Node * named currentNodePtr - it will point to the Node we are currently working on.
if the head pointer is zero
  print: NO cars
else
  set the currentNodePointer to the value of the head pointer
  while the currentNodePointer is not zero
      use a counter and print the car sequence number, as done previously.
      print the Car pointed to by the currentNodePointer
      set the currentNodePtr to the next pointer in the Node pointed to by currentNodePtr, which now makes the next Node the current Node

Rebuild the copy construtor.
Declare a local pointer variable of type Node * named currentNodePtr - it will point to the Node in the oldStringOfCars that we are currently working on.
Set the head and tail pointers in the StringOfCars being constructed to zero.
If the oldStringOfCars head pointer is not zero:
  set the currentNodePointer to the value in the oldStringOfCars head pointer.
  loop while the currentNodePointer is not zero,
      push the Car pointed to by the data pointer in the current Node, which is pointed to by the currentNodePointer.
      set the currentNodePtr to the next pointer in the currentNodePtr so we now make the next Node the current Node

Rebuild the destructor.
Declare two pointers of type Node * named currentNodePtr and previousNodePtr.
Set the currentNodePtr to the value of the head pointer.
Loop while the currentNodePtr is not zero,
  Delete the car pointed to by the current Node.
  Set the previousNodePtr to the value of the currentNodePtr.
  Set the currentNodePtr to the value of the next field of the current Node.
  Delete the Node pointed to by the previousNodePtr.

Use the same tests as in problem E3.