Make changes to push function

Hi everybody, I am now doing a lab assignment that uses this push funcion, and I'm having trouble understanding the instructions that the teacher provided.

This is what I came up with so far, and I know that the last two lines of codes does not match with the instruction, but I really don't know how to write that.

1
2
3
4
5
6
7
void StringOfCars::push(Car car)
{
    Car *currentCarPtr;
    Node *currentNodePtr;
    new Node = currentNodePtr;
    new Car(car) = currentCarPtr;
}


Here is the lab instruction:
1) Declare a local pointer variable of type Car * named currentCarPtr.
2) Declare a local pointer variable of type Node * named currentNodePtr.
3) Use new to get space in the heap for a Node and save the address of the space in the heap in currentNodePtr
4) Use new get space in the heap for a new Car that is a copy of the car
5) parameter of the push function and save the address of the space in the heap in currentCarPtr

6) 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 next pointer to the value of the currentNodePtr

Sorry, I forgot to put the declaration of the StringOfCars class that goes before the push function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Node
{
    private:
        Node *next;  // point to the next node
        Car *data;   // point to the Car data

    Node() // default constructor
    {
        next = 0;
        data = 0;
    }

    public:
        friend class StringOfCars;  // declare the StringOfCars as a friend
};

StringOfCars::StringOfCars() // default constructor
{
    head = 0;
    tail = 0;
}
The syntax for new isn't correct at lines 5 and 6.
example:
1
2
int * ptr;     // define a pointer to an int
ptr = new int; // allocate memory for an int 


http://www.cplusplus.com/reference/new/operator%20new/
Topic archived. No new replies allowed.