linked List

A linked list is a linear data structure where each element is a separate object. Linked list elements are not stored at contiguous location; the elements are linked using pointers. Each node of a list is made up of two items - the data and a reference to the next node. The last node has a reference to null. It can be illustrated from following figure.

                                                        fig: linked list





Program : Creation of Linear Linked List


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
//creating node
struct node
{
int data;
struct node *next;
};
 void print_list(struct node *ptr)
{
while(ptr!=NULL)
{
printf("|%d| |->",ptr->data);
ptr=ptr->next;
}

}
int main()
{
int a,b,c;
//creating pointer for three node
struct node *first=NULL;
struct node *second=NULL;
struct node *third=NULL;
//allocating memory for each pointer dynamically
first=(struct node *)malloc(sizeof(struct node));
second=(struct node *)malloc(sizeof(struct node));
third=(struct node *)malloc(sizeof(struct node));
//getting data values for different node from user
printf("Enter first node info:");
scanf("%d",&a);
printf("Enter Second node info:");
scanf("%d",&b);
printf("Enter Third node info:");
scanf("%d",&c);
//assining information and address to node
//for first node
first->data=a;
first->next=second;
//second node
second->data=b;
second->next=third;
//third node
third->data=c;
third->next=NULL;
//calling function to print list
print_list(first);
printf("NULL");

return 0;
}

It's output is: