Hallo abhi143!
I hope the example below will give you some inspirations:
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main(int argc, char **argv)
{
int i = 3; // gets later an another value
/* This won't change, or we would lose the list in memory */
struct node *root;
/* This will point to each node as it traverses the list */
struct node *conductor;
struct node *temp = NULL;
//root = malloc( sizeof(struct node) );
root = (struct node*)malloc(sizeof(struct node));
root->next = 0;
printf( "Enter a integer. ");
scanf("%d", &root->x); // = 12;
conductor = root;
for (i = 0; i < 4; i++)
{
if ( conductor != 0 )
{
while ( conductor->next != 0)
{
conductor = conductor->next;
}
}
/* Creates a node at the end of the list */
conductor->next = (struct node*)malloc(sizeof(struct node));
conductor = conductor->next;
if ( conductor == 0 )
{
printf( "Out of memory" );
return 0;
}
if ( conductor != 0 )
{ /* Makes sure there is a place to start */
while ( conductor->next != 0 )
{
printf( "Value: %d\n", conductor->x );
conductor = conductor->next;
}
printf( "%d\n", conductor->x );
}
/* initialize the new memory */
conductor->next = 0;
printf( "Enter a integer. ");
scanf("%d", &conductor->x); // = 42; conductor->x = 42;
}
/*-----------------------Liste zeigen----------------------------*/
temp = root;
printf("Show the list:\n");
while (temp != NULL)
{
printf(" %d ", temp->x);
temp = temp->next;
}
printf("\nEnd of program\n");
return 0;
}