#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
Pls help me understand this.
thnx in advance
Prince