1

I had created a program to create a linked list and insert an array in it. But it's not giving the desired output, may be the problem is in display function

#include <iostream>

using namespace std;

//node 
class node {
    public:
    int data;
    node *next;
} *head = nullptr;

//function to insert array in linkedlist
void create(int A[], int n) {
    node *head = new node;
    node *temp;
    node *last;
    head->data = A[0];
    head->next = nullptr;
    last = head;
    for (int i = 1; i < n; i++) {
        //temp is an temporary variable which creates each time and last remebers its position
        temp = new node;
        temp->data = A[i];
        cout << temp->data << endl;
        temp->next = nullptr;
        last->next = temp;
        last = temp;
    }
}

//function to display linked list which can access head because it is global
void display() { 
    node *p = head;
    while (p != nullptr) {
        cout << p->data << endl;
        p = p->next;
    }
}

int main() {
    int A[] = {1, 6, 9, 4, 5, 6};
    node *head;
    create(A, 6);
    display();
    return 0;
}

Maybe I understand something wrong like to add display function in class. But I had never seen a class pointer having functions.

This is my first linked list program. If you could share some good info about it please do, thank you

5
  • node *head= new node; declares a new local variable that hides the global variable head. See e.g. learncpp.com/cpp-tutorial/variable-shadowing-name-hiding Commented Sep 6, 2022 at 5:59
  • 2
    I'd suggest forgetting that C-style arrays exist in the language and instead always use either std::array or std::vector. Commented Sep 6, 2022 at 6:28
  • Would you mind adding actual and expected output? Commented Sep 6, 2022 at 6:39
  • @JesperJuhl not to mention providing node with a constructor, destructor, and usefully in this case, an append (or prepend) member function. C++ makes this possible. What's shown is basically C with iostream. Commented Sep 6, 2022 at 6:48
  • 2
    It's never too soon to stop using global variables. Commented Sep 6, 2022 at 6:49

1 Answer 1

1

The problem is that you have multiple variables named head being used for different purposes. You have a global variable head, which your display() function uses, but your create() function does not populate. create() populates a local variable named head instead, which shadows the global variable. display() never sees the created list. And main() has its own local variable named head, which is not being used for anything at all.

Get rid of the global variable. Make create() return the list it creates. And then make main() pass that list to display().

Try something like this:

#include <iostream>
using namespace std;

//node 
class node {
public:
    int data;
    node *next;
};

//function to insert array in linkedlist
node* create(int A[], int n) {
    node *head = nullptr;
    node **p = &head;
    for (int i = 0; i < n; i++) {
        *p = new node{ A[i], nullptr };
        p = &((*p)->next);
    }
    return head;
}

//function to display linked list
void display(const node *head) { 
    node *p = head;
    while (p != nullptr) {
        cout << p->data << endl;
        p = p->next;
    }
}

//function to destroy linked list
void destroy(node *head) {
    node *p = head;
    while (p != nullptr) {
        node *next = p->next;
        delete p;
        p = next;
    }
} 

int main() {
    int A[] = {1, 6, 9, 4, 5, 6};
    node *head = create(A, 6);
    display(head);
    destroy(head);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.