Help with linked list.

#include<stdio.h>
#include<stdlib.h>

struct LinkedList
{
        int val;
        struct LinkedList *next;
}node; /*Creating a structure variable*/

typedef struct LinkedList Node;
Node *start = NULL;
int create(int i)
{
        Node *temp = NULL;
        if (start == NULL)
        {
                start = (Node*)malloc(sizeof(node)); /*creating space for start*/
                start->val = i;
                start->next=NULL;
                return 0;
        }
        else
                temp = start;
                while(temp != NULL) /*traversing to last node*/
                {
                        printf("Value in temp = %d\n", temp->val);
                        temp=temp->next;
                }
                temp = (Node*)malloc(sizeof(node));
                temp->val = i;
                start->next= temp; /*Start pointing to newly created node*/
                temp->next = NULL;
                return 0;
}

void display()
{
        Node *temp = NULL;
        printf("Displaying List\n");
        if (start == NULL)
                printf("List is Empty\n");
        else
        {
                temp = start;
                while(temp != NULL)
                {
                        printf("%d      ", temp->val);
                        temp = temp->next;
                }
        }
}

int main()
{
        int j =0;
        for(j; j <= 10; j++)
        {
                create(j);
        }
        display();
        return 0;
}

---------- Post updated at 01:05 PM ---------- Previous update was at 01:03 PM ----------

HI,
I am trying to write a program for single linked list. But the problem is the in the while loop in the below written program the value of the nodes created is not getting saved.
Like for example if I try to display the list..all I get is 0 10 :frowning:
Pls help me understand this.
thnx in advance
Prince

You link the 1st element to the newly created element, which then links to NULL.
So you always have the 1st and the latest element - the intermediate elements are lost.

Thanks Germany,
You mean I should have done that last linking.
All what I want to do is to link the newly created element to NULL and link Start to the newly created element.
Please can you also tell me what could there be..instead of what I have written.

A proposal (please always wrap in CODE tags!)

#include<stdio.h>
#include<stdlib.h>

struct LinkedList
{
int val;
struct LinkedList *next;
}node; /*Creating a structure variable*/

typedef struct LinkedList Node;
Node *start = NULL;
int create(int i)
{
Node *temp = start;
while(temp != NULL) /*traversing to last node*/
{
printf("Value in temp = %d\n", temp->val);
temp=temp->next;
}
temp = (Node*)malloc(sizeof(node));
temp->val = i;
temp->next = start; /* link this node to the start of the list */
start = temp; /* this node becomes the new start */
return 0;
}

void display()
{
Node *temp = NULL;
printf("Displaying List\n");
if (start == NULL)
printf("List is Empty\n");
else
{
temp = start;
while(temp != NULL)
{
printf("%d ", temp->val);
temp = temp->next;
}
}
}

int main()
{
int j =0;
for(j; j <= 10; j++)
{
create(j);
}
display();
return 0;
}

Hey Germany,

Thanks for the reply. It really helped. But I was wondering if I do that in the code, it would certainly add the new nodes at the beginning. What If I want to add them at the end?

Thanks Again,
Prince

Linking at the beginning is easier.
Linking at the end either requires traversing to the end of the list, then linking to it.
Or, more efficient, have an external end pointer in addition to the start pointer:

...
Node *start = NULL;
Node *end = NULL;

int create (int i) {
...
temp = (Node*)malloc(sizeof(node));
temp->val = i;
temp->next = NULL;
if ( start == NULL ) {
  start = temp; /* the first node becomes the start */
} else {
  end->next = temp; /* link last node to this node */
}
end = temp; /* update the end pointer */
...
}
...

This time we need an if clause, because the first element needs to be treated differently.