Zisis Diamantis
Zisis Diamantis

Reputation: 131

Printing a Singly Linked List

I have a problem with this function. When I call it, the clients should be printed from the last (tail), to the first (head).

The problem is that I want to print them from the first (head) to the last (tail). Everything that I have tried has either resulted in a segmentation fault or prints only the first client.

Note that the program is about a bank. I have a queue of customers that I want to print in order.

Thank you in advance!

void printAll(clientData* tail) {
    clientData *current;
    current = tail;
    if (current == NULL) {
        printf("There is no client in queue\n");
        return ;
    }
    while (current != NULL) {
        printf("%s %s %d/%d/%d %s %s %s %s\n",
            current->firstname,
            current->lastname,
            current->birthday.day,
            current->birthday.month,
            current->birthday.year,
            current->bankaccount,
            current->telephone,
            current->email,
            current->bankcommand);

        current = current->next;
    }
    return;
}

Upvotes: 1

Views: 371

Answers (1)

Imre Kerr
Imre Kerr

Reputation: 2428

What you need is a stack. Your system provides one automatically for you when you call a function recursively:

void printAll(clientData* current)
{
    if (current->next != NULL) {
        printAll(current->next);
    }
    printf("%s %s %d/%d/%d %s %s %s %s\n", current->firstname,current->lastname,current->birthday.day,current->birthday.month,current->birthday.year,current->bankaccount,current->telephone,current->email,current->bankcommand);
}

Upvotes: 4

Related Questions