1

Hi I wish to implement a simple linked list and all the values to the end of the list. As simple as that but I am not able to do so. Can you please tell me where I am doing it wrong ? Initially I am declaring a pointer and assigning NULL value to it. Later in each iteration I am allocating memory to the pointer that was initially NULL.

#include <stdio.h>
#include <malloc.h>

struct node{
    int a;
    struct node* next;
};
struct node* insert(struct node* start,int value);
void print(struct node* head);
int main()
{
    int a;
    struct node* head = NULL;
    while(scanf("%d",&a) != EOF)//taking input
    {
        head = insert(head,a);
        print(head);
    }
    return 0;
}

struct node* insert(struct node* start,int value)
{
    struct node* head = start;
    while(start != NULL)
    {
        start = start->next;//getting upto the end of the linked list
    }
    start = (struct node*)malloc(sizeof(struct node));//allocating memory at the end
    start->a = value;
    start->next = NULL;
    if(head == NULL) 
    {
        return start;//for the base case when list is initally empty
    }
    return head;
}

void print(struct node* head)
{
    while(head != NULL)
    {
        printf("%d\n",head->a);
        head = head->next;
    }
    return;
}
3
  • Déjà vu
    – ohmu
    Commented Feb 14, 2014 at 18:06
  • 1
    please note malloc.h is not standard. You should be using stdlib.h
    – ajay
    Commented Feb 14, 2014 at 18:34
  • Okay I will keep that in mind afterwards. Thanks Commented Feb 14, 2014 at 18:42

2 Answers 2

1

You're losing your linkage between your tail and your new node, try this instead

struct node* insert(struct node* head,int value)
{
struct node* tail = head;
while(tail != NULL && tail->next != NULL)
{
    tail= tail->next;//getting upto the end of the linked list
}

struct node* start = (struct node*)malloc(sizeof(struct node));//allocating memory at the end
start->a = value;
start->next = NULL;
if(head == NULL) 
{
    return start;//for the base case when list is initally empty
}
else
{
    tail->next = start;
}
return head;
}
3
  • Thanks, but I am still now able to figure out my mistake. Please help me. Commented Feb 14, 2014 at 18:18
  • You mean to say that "start = (struct node*)malloc(sizeof(struct node));" causes the loss of the linkage but it is preserved when "start->next = (struct node*)malloc(sizeof(struct node))" is done ? Commented Feb 14, 2014 at 18:21
  • Yes. You can handle your variables initially anyways that you'd like, but at some point you MUST assign start->next, which you aren't doing in your code. I simply re-orged it a bit and changed variable names so that it was a little simpler to follow.
    – jakebower
    Commented Feb 20, 2014 at 17:02
0
struct node* insert(struct node* start,int value){
    struct node* head = start;
    struct node* np = (struct node*)malloc(sizeof(struct node));
    np->a = value;
    np->next = NULL;

    if(head == NULL)
        return np;

    while(start->next != NULL){
        start = start->next;
    }
    start->next = np;
    return head;
}

What makes the approach I am using buggy ?

nodeX
|
+a
|
+next(address to OtherX)

nodeX.next = new_node;//update link(case of OK)

tempPointer = nodeX.next;//address to OtherX set to tempPointer
tempPointer = new_node;//contents of tempPointer changed, but orignal (nodeX.next not change)
2
  • thanks but why start->next but can't be code in such a way that start points to NULL and later change it the way I am doing. What makes the approach I am using buggy ? Commented Feb 14, 2014 at 18:28
  • 1
    @user1244590 It is discarded if you set the address to the local variables. Must put the correct link.
    – BLUEPIXY
    Commented Feb 14, 2014 at 18:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.