0

Trying to work out Linked list problems. Stuck up with this basic mistake Head value is not "NULL" in createLinkList(). What trick I am missing here . Here is my code.

#include <iostream>
using namespace std;

void createLinkList(struct node**);
void showList();
void insertNode();

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

int main()
{
    struct node* head = NULL;
    createLinkList(&head);
    cout<<"inside main function \t"<<head<<endl;
    return 0;
}
void createLinkList(struct node **head){
    int data;
    struct node * new_node;

    cout<<"creating Link List ..."<<endl;
    cout<< "Enter the data to be inserted"<<endl;
    cin >> data;
    cout<<"inside createLinkList \t"<<head<<endl;
    if (head == NULL){
        new_node->data=data;
        new_node->next=*head;
        *head=new_node;
        cout<<"Element Added at Head Position"<<endl;
    }
    else{
        cout<<"Element added at other positions"<<endl;
    }

}

Output: enter image description here

cannot understand why head value is different in main() and createLinkList().

Nihar
  • 553
  • 1
  • 10
  • 29

2 Answers2

5

Your createLinkList method isn't taking a head pointer, it's taking a pointer-to-head pointer. It should probably be called pHead:

void createLinkList(struct node **pHead){

So your head will never be NULL - what you should be testing is whether *head is NULL.

But you've got WAY more problems than that. You're not creating the new node!

Your code says (without the debug lines):

struct node * new_node;    // <<< You probably want new_node = new node;

if (head == NULL){         // <<< You definitely want *head here!
    new_node->data=data;   // <<< This variable is uninitialised
    new_node->next=*head;  // <<< You know this is NULL - the if said so
    *head=new_node;

In short, you need to go back to the drawing board.

John Burger
  • 3,662
  • 1
  • 13
  • 23
  • Hi John , I have tried that thing already ,but the code crashes. – Nihar Nov 13 '16 at 10:02
  • Check my edit - you haven't written anywhere near enough code! – John Burger Nov 13 '16 at 10:07
  • As mentioned in my comment the condition should be `if (*head == NULL){`. – πάντα ῥεῖ Nov 13 '16 at 10:11
  • @ John ,thx for pointing that out ,I was looking so hard at the NULL thing that I almost messed it up. – Nihar Nov 13 '16 at 10:12
  • Likely, `node` should be a class rather than a struct as well. – David Hoelzer Nov 13 '16 at 10:17
  • @ David Hoelzer ,I cannot figure it out ,why I should go for a class ?,when there is nothing much to differentiate between a struct and a class. – Nihar Nov 13 '16 at 10:29
  • @N.Nihar I'm going to take a guess that what David Hoelzer meant wasn't simply "use the `class` keyword", but more "apply OOP best practices", which means that the code doing the pointer manipulations shouldn't be doing that directly, and shouldn't even have access to those pointers, they should be restricted to the `node` class, or possibly a helper `list` class. Typically, the `struct` keyword is used for raw data, and the `class` keyword is used when the types contain logic, but you're right that either keyword can be used with either approach. –  Nov 13 '16 at 10:38
  • The difference between `struct` and `class` in a _language_ sense is very simple: they're identical, except that `struct` members by default are `public`, while `class` members by default are `private`. But that difference is huge in a _concept_ sense - `class`es should be designed to hide/abstract away their variable members, delegating their modification to methods; while `struct`s assume by default that all members will be available for modification to all. Although you _can_ use `private` in `struct`s to hide things there too, it's the starting concept that suggests the difference in use. – John Burger Nov 15 '16 at 08:06
0

You need to use *head everywhere in your createLinkList function. You also need to initialize you new_node with malloc.

#include <iostream>
#include <cstdlib>
using namespace std;

void createLinkList(struct node**);
void showList();
void insertNode();

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

int main()
{
    struct node* head = NULL;
    createLinkList(&head);
    cout<<"inside main function \t"<< head->data <<endl;
    return 0;
}

void createLinkList(struct node **head){
    int data;
    struct node *new_node = (struct node*)malloc(sizeof(struct node));

    cout<<"creating Link List ..."<<endl;
    cout<< "Enter the data to be inserted"<<endl;
    cin >> data;
    cout<<"inside createLinkList \t"<<endl;
    if ((*head) == NULL){
        new_node->data=data;
        new_node->next=*head;
        *head=new_node;
        cout<<"Element Added at Head Position"<<endl;
    }
    else{
        cout<<"Element added at other positions"<<endl;
    }

}
Luka Lopusina
  • 2,557
  • 3
  • 27
  • 32
  • but when working with cpp ,why I should be working with malloc ,when I have super cool and powerful "new" – Nihar Nov 13 '16 at 10:14
  • Note that you _usually_ use `new` with `class`es, not `struct`s. I agree that you should use `new`, but then change `node` to be a `class`, and ***WRITE A CONSTRUCTOR***. That will initialise everything – John Burger Nov 13 '16 at 10:16
  • You probably can use new but check this out http://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new – Luka Lopusina Nov 13 '16 at 10:17
  • @LukaLopusina Which has the first answer (ordered by votes) saying not to use `malloc`, and a second answer saying not to use `malloc` unless you have to. The other answers say the same thing. Do any support your position that the OP should in this case call `malloc`? –  Nov 13 '16 at 10:26
  • As @JohnBurger mentioned if you want to use new it would be better to create class with constructor for you node and to try/catch that because new is operator, it calls constructors, it returns exact data type not a pointer like malloc, ... I used malloc for simplicity if you want to change all of this stuff you it would be better :) – Luka Lopusina Nov 13 '16 at 10:35