0

I'm trying so hard to find out what's gone wrong with the result. it only prints out 1 information instead of everything in the list. I checked every single function from insert to display but nothing is different from the sample that works. I'm really at a loss now. At first, I thought it was the *l that I forgot to malloc, the isEmptyTrain function was wrong... but after fixing all of these it still does not work at all. Can you help me with this exercise? Below is my code:

    typedef struct Car{
    char name;
    int number;
    struct Car *pNext;
} Car;

typedef struct Train{
    int size;
    Car *pHead;
} Train;
Car *initCar(char name, int number){ 
//  Car *car = (Car*)malloc(sizeof(*car));
    Car *p = new(Car);
    p->name = name;
    p->number = number;
    p->pNext = NULL;
    return p;
}
//initList
void initTrain(Train *l){
    l->size = 0;
    l->pHead = NULL;
}
bool isTrainEmpty(Train *l){
    return (l->size == 0);
}
bool isCarEmpty(Car *p){
    return (p->number == 0);
}
bool length(Train *l){
    return l->size;
}

void insert(Car *pNew, Train *l, Car *pOld = NULL){
    //insert first
    if(isTrainEmpty(l)){
        l->pHead = pNew;
    }
    else{
        pNew->pNext = pOld->pNext;
        pOld->pNext = pNew;
    }
    l->size++;
}
void display(Train *l){
    Car *p = l->pHead;
    for(int i = 0; i < length(l); i++){
        cout << "Car " << p->name << " has " << p->number << " passenger(s)" << endl;
        p = p->pNext;
    }
}
    int main(){
    Train *l, obj;
    l = new(Train);
    l = &obj;
    char name;
    int number = 0, size, add = 0, del = 0;
    Car *p, *q = NULL;

    initTrain(l);
    cout << "Enter the length of the train: " << endl;
    cin >> size;
    if(size <= 0) {
        cout << "The length must be greater than 0!" << endl;
        return 0;
    }
    else
        for(int i = 0; i < size; i++){
            cout << "Enter the name of the car (1 char): " << endl;
            cin >> name;
            cout << "Enter the number of passengers on the car: " << endl;
            cin >> number;
            p = initCar(name, number);
            if(i == 0){
                insert(p, l);
            }
            else{
                insert(p, l, q);
            }
            q = p; 
        }   
        display(l); return 0;}

Thank you!

William Le
  • 825
  • 1
  • 9
  • 16
  • 2
    The standard library already has `std::list` and `std::forward_list` if you insist on using a horrible data structure like a linked list. Why reinvent the wheel? Btw; a `std::vector` is likely to serve you better.. – Jesper Juhl Dec 25 '19 at 16:44
  • 2
    Ditch all the manual memory management. Learn to use smart pointers and containers. Naked `delete` is a *code smell* in modern C++. – Jesper Juhl Dec 25 '19 at 16:47
  • 1
    Unrelated: Looks like what you are learning is more C than C++. I recommend a [change in reference materials](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). For now consider replacing `initCar` with a `Car` constructor, replacing `initTrain` with a `Train` constructor, adding a destructor to `Train` and observing [the Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). You can also remove the `typedef`s. They aren't necessary in C++. – user4581301 Dec 25 '19 at 17:00
  • One of the best ways to debug a linked list is to visualize what's going on by drawing pictures. Draw the list in its initial state before performing an operation, then draw the expected after. Then following your coded instructions exactly, draw the list after each step of the operation. When you find yourself drawing something silly, you've found a bug and probably have a good idea what you should draw instead. – user4581301 Dec 25 '19 at 17:03
  • If you still can't spot the problem, step through the function that's giving you problems with the debugging utility that came with your development tools. Watch what the program really does and make sure everything matches your drawings. If it doesn't, you've found the bug and need to figure out why the program violates your expectations. – user4581301 Dec 25 '19 at 17:05
  • I know this code might look chunky but my teacher did assign these style coding as homework for the purpose of learning 'the basics'. Really appreciate all of your feedbacks – William Le Dec 25 '19 at 17:06
  • Writing a linked list is a kind of programmer rite of passage. To successfully write one you have to understand and execute quite a lot in a relatively short amount of time as well as have a solid grasp on the fundamentals. For now remove all of the user input and hardcode values so you can easily test the exact same inputs over and over. This also helps when asking a question here. You can tune the inputs to demonstrate exactly what the problem is and remove all of the code that is not required to demonstrate the problem. Often eliminating all of this extra noise exposes the bug to you. – user4581301 Dec 25 '19 at 17:14
  • BTW, you don't *need* `typedef struct` in C++. Structs and classes are already types. You should get a better reference book. – Thomas Matthews Dec 25 '19 at 18:43
  • 1
    Also, `char` is for a *single* letter. The `std::string` is for zero or more characters, like names and texts. – Thomas Matthews Dec 25 '19 at 18:44

1 Answers1

1

length(l) returns a bool. You then use it as an integer, whereby it's converted to either 0 or 1; it can't ever return a value greater than 1.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85