This tutorial demonstrate the implementation of singly linked list.
This tutorial demonstrate the implementation of singly linked list.In this example you will see how to create a node and insert data record in singly link list.
# include <stdio.h> # include <stdlib.h> struct node { int data; struct node *link; }; struct node *insert(struct node *p , int num) { struct node *temp; if(p==NULL) { p=(struct node *)malloc(sizeof(struct node)); if(p==NULL) { printf("Error Occurred\n"); exit(0); } p-> data = num; p-> link = NULL; } else { temp = p; while (temp-> link != NULL) temp = temp-> link; temp-> link = (struct node *)malloc(sizeof(struct node)); if(temp -> link == NULL) { printf("Error Occurred\n"); exit(0); } temp = temp-> link; temp-> data = num; temp-> link = NULL; } return (p); } void printlist ( struct node *p ) { printf("The data values of your list are\n"); while (p!= NULL) { printf("%d\t",p-> data); p = p-> link; } } void main() { int n; int x; struct node *start = NULL ; printf("Enter number of nodes you want to create \n"); scanf("%d",&n); while ( n-- > 0 ) { printf( "Enter data values for each node\n"); scanf("%d",&x); start = insert ( start , x ); } printf("The created single link list is\n"); printlist ( start ); }