Home > OS >  display list is only displaying the head of the node
display list is only displaying the head of the node

Time:02-17

I wrote a code that asks the user to enter a custom list, and I want to display it, but it's only displaying the head node.

I want to know if the problem is from the display function or the input function. I want to remove the display function later, but I want my list to be created.

Here is my code:

#pragma once
#include <cstddef>
#include <iostream>
using namespace std;

struct node {
    char data;
    node* next;
};

node* inputList();
void displayList(node*);

int exercice1() {
    node* head;
    head = inputList();
    displayList(head);
    return 0;
}

node* inputList() {
    node* tmp;
    node* cur, *head;
    int n;
    do {
        cout << "enter the number of nodes: ";
        cin >> n;
    } while (n <= 0);
    tmp = new node;
    cout << "enter the values inside each node: ";
    cin >> tmp->data;
    head = tmp;
    cur = tmp;
    for (int i = 1; i < n; i  ) {
        tmp = new node;
        cur = cur->next;
        cout << "enter a node: ";
        cin >> tmp->data;
        cur = tmp;
    }
    cur->next = NULL;
    return head;
}

void displayList(node* head) {
    if (head != NULL) {
        cout << head->data;
        displayList(head->next);
    }
}

CodePudding user response:

inputList fails to link the nodes together. We could probably get away with something like

tmp = new node;
cur->next = tmp; // point current node's next at new node
cur = cur->next;

but here's a cleaner approach:


node* inputList() {
    node * head; // storage for the start of the list. Keeps track so we 
                 // know what to return when we're done.
    node ** insert = &head; // store where we're going to insert a node rather than 
                            // tracking the current node with another variable.
                            // this is both tmp and cur by always pointing at where 
                            // the next node is going to go.
    int n;
    do {
        cout << "enter the number of nodes: ";
        cin >> n;
    } while (n <= 0);
    *insert = new node; //node goes right into head
    cout << "enter the values inside each node: ";
    cin >> (*insert)->data;
    insert = &(*insert)->next; // now the insertion point points at head's next

    for (int i = 1; i < n; i  ) {
        *insert = new node; // new node gets linked right in next
        cout << "enter a node: ";
        cin >> (*insert)->data;
        insert = &(*insert)->next; // advance to next of the node we just added
    }
    *insert = NULL; // terminate list by pointing the insertion point at NULL
    return head;
}

And now that he have abstracted head into just another insertion point like next, we can eliminate some duplicated code:

node* inputList() {
    node * head;
    node ** insert = &head; // head is no different from a next. It just 
                            // has a different name
    int n;
    do {
        cout << "enter the number of nodes: ";
        cin >> n;
    } while (n <= 0);

// no special handling for head.
    for (int i = 0; i < n; i  ) { // iteration now starts at 0. The loop 
                                  // builds all of the nodes.
        *insert = new node;
        cout << "enter a node: ";
        cin >> (*insert)->data;
        insert = &(*insert)->next;
    }
    *insert = NULL;
    return head;
}
  • Related