2

I have implemented linklist as below,

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

void initNode(node* head, int data)
{
    head->data = data;
    head->next = NULL;
}

void addNode(node* linkList, int data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->next = NULL;

    while (linkList)
    {
        if (linkList->next == NULL)
        {
            linkList->next = newnode;
            return;
        }
        linkList = linkList->next;
    }
}

void insertFrontNode(node* linkList, int data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->next = linkList;
    linkList = newnode;
}

void DisplayLinkList(struct node* linkList)
{
    int i = 1;
    while (linkList)
    {
        cout << "Linklist(" << i << ") " << linkList->data << endl;
        linkList = linkList->next;
        i++;
    }
}


int main()
{
    node* linkList  =new node;
    initNode(linkList, 5);
    addNode(linkList, 10);
    addNode(linkList, 30);
    insertFrontNode(linkList, 12);  
    DisplayLinkList(linkList);


    cin.get();
    return 0;
}

My question is, when i initialise linklist in initNode or add node in addNode, the changes are reflected in linkst pointer which i am passing thru argument. but If i am adding node infront in insertFrontNode, changes are not reflecting in the linklist pointer.

Why? and what is difference?

Sneha
  • 21
  • 3
  • The difference is that the change you perform in `insertFrontNode()` is *inherently* not reflected in the (original) head node of the list. The whole point is that the list will have a new head node, but you provide no means for the new head node pointer to be returned to the caller. – John Bollinger Nov 21 '15 at 00:49
  • you should add the c++ tag. – ChuckCottrill Nov 21 '15 at 02:45
  • not sure about C++, but in C all the references to `node` should be `struct node` to avoid raising "error unknown type name `node`" – user3629249 Nov 21 '15 at 17:49
  • The posted code contains the keywords cin and new and cout and `endl` which are only recognized in C++, so please remove the 'c' tag and add the 'c++ tag – user3629249 Nov 21 '15 at 17:52
  • 1
    in C, parameters are passed by value, so changing a parameter has no effect on the callers' variable. Need to pass the address of the parameter (and since the parameter is already a pointer, in the called function use `**` in the function parameter list. In C++ pass the parameter by `reference` rather than by 'address of` – user3629249 Nov 21 '15 at 17:55
  • regarding this line: `cin.get();`, My research says there is no method `.get()` – user3629249 Nov 21 '15 at 18:03
  • @ John Bollinger : Thanks for reply. But, I am not returning linklist in addNewNode() function too. But in the main() when I call addNewNode(), I am getting new node added in the linklist. So why it is not getting in the insertFrontNode() ? – Sneha Nov 23 '15 at 02:13

2 Answers2

2

You might find that adding a list (slist, single linked list) structure around your list will help (and make both add at head and add a tail O(1) operations).

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

struct slist
{
    node* head;
    node* tail;
};

void addHead(slist* linkList, int data)
{
    //new list node
    node* newnode = new node;
    newnode->data = data;
    newnode->next = NULL;

    newnode->next = linkList->head;
    linklist->head = newnode->next;
}

void addTail(slist* list, int data)
{
    //new list node
    node* newnode = new node;
    newnode->data = data;
    newnode->next = NULL;

    if( list->tail ) {
        list->tail->next = newnode;
    }
    list->tail = newnode;
}

void DisplayLinkList(struct slist* linkList)
{
    int i = 1;
    node* link = linklist->head;
    while (link)
    {
        cout << "Linklist(" << i << ") " << link->data << endl;
        link = link->next;
        i++;
    }
}

int main()
{
    slist* linkList=new slist;
    addTail(linkList, 5);
    addTail(linkList, 10);
    addTail(linkList, 30);
    addHead(linkList, 12);  
    DisplayLinkList(linkList);
    return 0;
}
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42
2

You are doing fine.You are modifying the head of your linked list but not updating it to your outer world.So when you are calling DisplayLinkList it is called with the old head pointer which is why the changes are not reflecting. Check the below code:

void insertFrontNode(node** linkList, int data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->next = *linkList;
    *linkList = newnode;
}

int main()
{
    node* linkList = new node;
    initNode(linkList, 5);
    addNode(linkList, 10);
    addNode(linkList, 30);
    insertFrontNode(&linkList, 12);
    DisplayLinkList(linkList);


    cin.get();
    return 0;
}

Above code,not only add the node at the front but also updates the head pointer such that display functions get the new updated head pointer.

Harajyoti Das
  • 721
  • 5
  • 12
  • Thanks for the reply. But, I am not returning linklist in addNode() function too. But in the main() when I call addNode(), I am getting new node added in the linklist. So why it is not getting in the insertFrontNode() ? – – Sneha Nov 23 '15 at 02:25
  • because You are not modifying the head of the linked list in addNode() and so it worked.. – Harajyoti Das Nov 23 '15 at 05:35