Home > front end >  Queue linked list not printing it's contents
Queue linked list not printing it's contents

Time:03-25

I am trying to get input of a queue with the linked list implementation. The issue is that the contents of the queue aren't being printed. I tried debugging but it says that in the function displayCar that pointer p is null regardless. I can't tell what's wrong with why pointer p is NULL. Is there a missing reference when I am trying to push from the carInput function?

#include <iostream>
#include <queue>

using namespace std;

class record
{
public:
    string ownerID, plateNumber;
    record* next;
};


void push(string ownerID1, string plateNumber1, record **head, record **tail) {
    record *n = new record();
    n->ownerID = ownerID1;
    n->plateNumber = plateNumber1;
    n->next = NULL;

    if (*head == NULL) {

        *head =*tail= n;
    }

    else {

        (*tail)->next = n;
        *tail = n;

    }

}


void pop(record** head, record** tail) {

    record* p = *head;

    while (*head != NULL) {
        *head = (*head)->next;
        free(p);
        p = *head;
    }

    if (*head == NULL)
    {
        *tail = NULL;
    }

}

void carInput(record *head, record *tail) {
    char choice = 'Y';
    string ownTemp, plateTemp;
    while (choice == 'Y') {
        cout << "Enter Owner Name: ";
        cin >> ownTemp;
        cout << "Enter Plate Number: ";
        cin >> plateTemp;
        push(ownTemp,plateTemp,&head,&tail);
        cout << "Press [Y] for next input: ";
        cin >> choice;

    }


}

void displayCar(record* head, record *tail) {

    record* p = head;

    cout << "List Of Cars: \n";
    int i = 1;
    while (p!= NULL) {
        cout << i << ". Owner Name: " << p->ownerID << endl;
        cout << i << ". Plate Number: " << p->plateNumber<< endl;
        pop(&head,&tail);
        i  ;
    }
}


void serviceCar(record *head,record*tail) {

    record* p = head;

    string plateTemp;
    int i = 0, time = 0;
    char choice = 'Y';



    cout << "Enter Plate Number:";
    cin >> plateTemp;

    while (p!= NULL) {

        if (p->plateNumber == plateTemp) {
            cout << "There is [" << i << "] car in queue before your turn. Estimated time in queue: " << time;
        }
        else {
            i  ;
            time = time   45;
        }
        pop(&head,&tail);
    }
}



int main() {


    record* head = NULL;
        record*tail = NULL;
    


    cout << ":: Car Record::\n\n";
    carInput(head,tail);
    displayCar(head,tail);
    serviceCar(head, tail);










}

CodePudding user response:

I don't know why you are punishing yourself with code like this when you are in C and there are plenty easier ways to do the same, but I'll try to help anyway by underlining the main problems:

1). The main reason that you must be struggling is that in the first push, even after *head =*tail= n;, the *head->next is still NULL and later when you'll try to iterate from the head, as you do in pop, *head = (*head)->next; you will get nothing.

2). if you want to do pop, you should delete one element for each call, not the whole collection - so you need if instead of while. You have while where you using pop for each iteration and in the pop you also have the while, so think about it. Also, you should be returning the value to display it easily - or change the way you trying to cout p in displayCar.

3). When you want to display the collection you just have to iterate through the collection instead of deleting all the elements, which will leave you empty collection after one display. You just need to iterate and display them, not to delete, something like that:

record* p = *head;
int i = 0;
while (p != NULL) {
    cout << i << ". Owner Name: " << p->ownerID << endl;
    cout << i << ". Plate Number: " << p->plateNumber<< endl;
    p = p->next;
    i  ;
}

There are some other points that should be mentioned, but I think that's enough to get the code right direction - anyway, my advice would be to try to take a good look for simple linked-list how it's done and then try Queue linked-list, or just check already written examples and then try it by yourself. GeeksForGeeks

CodePudding user response:

Your carInput receives pointers by value, and modifying those pointers has no effect on the pointers you pass to it.
Thus, main's head and tail are always null.
(You solved this with the pushing and popping functions, but failed to apply the same principle here.)

void carInput(record **head, record **tail) {
    char choice = 'Y';
    string owner, plate;
    while (choice == 'Y') {
        cout << "Enter Owner Name: ";
        cin >> owner;
        cout << "Enter Plate Number: ";
        cin >> plate;
        push(owner, plate, head, tail);
        cout << "Press [Y] for next input: ";
        cin >> choice;
    }
}

You need to combine this fix with the fixes pointed out in the comments.

  • Related