Exforsys.com
 
Home Tutorials C Language
 

C Programming - Linked Lists

 

C Programming - Linked Lists

In this tutorial you will learn about C Programming - Linked Lists, Structure, Advantages of Linked List, Types of linked list and Applications of linked lists.



A linked list is called so because each of items in the list is a part of a structure, which is linked to the structure containing the next item. This type of list is called a linked list since it can be considered as a list whose order is given by links from one item to the next.


Structure

Item


®



Each item has a node consisting two fields one containing the variable and another consisting of address of the next item(i.e., pointer to the next item) in the list. A linked list is therefore a collection of structures ordered by logical links that are stored as the part of data.


Consider the following example to illustrator the concept of linking. Suppose we define a structure as follows


struct linked_list
{
float age;
struct linked_list *next;
}
struct Linked_list node1,node2;


this statement creates space for nodes each containing 2 empty fields

node1


 


node1.age


 


node1.next



node2


 


node2.age


 


node2.next



The next pointer of node1 can be made to point to the node 2 by the same statement.
node1.next=&node2;


This statement stores the address of node 2 into the field node1.next and this establishes a link between node1 and node2 similarly we can combine the process to create a special pointer value called null that can be stored in the next field of the last node


Advantages of Linked List:

A linked list is a dynamic data structure and therefore the size of the linked list can grow or shrink in size during execution of the program. A linked list does not require any extra space therefore it does not waste extra memory. It provides flexibility in rearranging the items efficiently.


The limitation of linked list is that it consumes extra space when compared to a array since each node must also contain the address of the next item in the list to search for a single item in a linked list is cumbersome and time consuming.


Types of linked list:

There are different kinds of linked lists they are
Linear singly linked list
Circular singly linked list
Two way or doubly linked list
Circular doubly linked list.


Applications of linked lists:

Linked lists concepts are useful to model many different abstract data types such as queues stacks and trees. If we restrict the process of insertions to one end of the list and deletions to the other end then

we have a mode of a queue that is we can insert an item at the rear end and remove an item at the front end obeying the discipline first in first out. If we restrict the insertions and deletions to occur only at one end of list the beginning then the model is called stacks. Stacks are all inherently one-dimensional. A tree represents a two dimension linked list. Trees are frequently encounters in every day life one example are organization chart and the other is sports tournament chart.



Read Next: C Programming - File management in C



 

 

Comments


bhushan said:

  your article is really nice and you have presented everything in a lucid manner so that i am able to clear my doubts.
February 19, 2007, 1:55 pm

kruiz413 said:

  This info is very useful to know about Linked Lists
June 21, 2007, 2:52 am

Rachit Saki said:

  It is a mindblowing concept which i came to know through your site.
October 19, 2007, 12:00 am

pooja_786 said:

  this concept is so nice to understand...
June 30, 2008, 10:38 am

RamSai said:

  you have presented in clear and crisp format
July 16, 2008, 5:40 am

Belal said:

  You have written about linklist in very easy and fair understandable.
August 9, 2008, 12:16 pm

pagadala said:

  its a very good reference for linkedlists and very easy to understand
October 17, 2008, 4:25 pm

babuhari said:

  your article is very ease to understand for beginners...
October 22, 2008, 6:18 am

s.vinod kumar said:

  good article.... really very useful to the beginners..
November 4, 2008, 1:02 pm

gorry said:

  still confused!!! help me
November 6, 2008, 12:57 am

dayany dawood said:

  how to count the number of different element in linked list?
November 7, 2008, 8:36 am

nisha said:

  please give one example program related to linked list....thnaks

November 17, 2008, 12:11 am

honey said:

  program to create a linked list and interchange the elements of the list at positions m and n and display the contents of the list before and after interchanging the elements.
November 19, 2008, 5:22 am

define said:

  this is wonderful explanation about linked lists
November 25, 2008, 8:09 pm

amresh kumar said:

  Extremely nice explanation.
November 26, 2008, 12:12 am

rak said:

  This website is very useful to understand easily...
December 3, 2008, 6:09 am

senthila said:

  Gud explantation......it useful for who is needing this
December 10, 2008, 4:56 am

mitesh said:

  so nice so perfect
December 23, 2008, 11:01 pm

shubhajoy said:

  one of the most innovative sites ever!
December 28, 2008, 6:35 am

Ahmad said:

  explanation is good abut linked list, but still need with diagram so that everyone can easily understand
January 6, 2009, 10:50 am

1 said:

  This website is very useful to understand easily & learn more...
January 16, 2009, 11:35 pm

RASHAD MAQSOOD said:

  It is very helpful website particularly for learning students
March 26, 2009, 12:09 am

Mayank said:

  #include<stdio.h>
#include<malloc.h>
struct node
{
int item;
struct node *next;

}*start=NULL;

void main()
{
int ch,n;
clrscr();
while(1)
{
printf("n1. Create List");
printf("n2. Display the list");
printf("nEnter any choice:");
scanf("%d",&ch);
getch();
switch(ch)
{
case 1:{scanf("%d",&n);
create_list(n);break;}
case 2:{display();break;}
default:{printf("Wrong choice");return;}
}; /*end of switch*/
} /*end of while*/

} /*end of main*/

create_list(int data)
{
struct node *q,*tmp;
tmp=malloc(sizeof(struct node));
tmp->item=data;
tmp->next=NULL;

if(start==NULL) /*if list is empty*/
start=tmp;
else
{ /*element inserted at the end*/
q=start;
while(q->next!=NULL)
{
q=q->next;
q->next=tmp;
} /*end of while*/
} /* end of else*/
} /*end of create_list()*/

display()
{
struct node *q;
if(start==NULL)
{
printf("List is empty");
return;
} /*end of if()*/
q=start;
printf("nnList is :n");
while(q!=NULL)
{
printf("t%d",q->item);
q=q->next;
} /*end of while()*/

} /*end of display()*/

November 7, 2009, 11:04 pm

himanshu said:

  #include<stdio.h>
#include<malloc.h>
struct node
{
int item;
struct node *next;

}*start=NULL;

void main()
{
int ch,n;
clrscr();
while(1)
{
printf("n1. Create List");
printf("n2. Display the list");
printf("nEnter any choice:");
scanf("%d",&ch);
getch();
switch(ch)
{
case 1:{scanf("%d",&n);
create_list(n);break;}
case 2:{display();break;}
default:{printf("Wrong choice");return;}
}; /*end of switch*/
} /*end of while*/

} /*end of main*/

create_list(int data)
{
struct node *q,*tmp;
tmp=malloc(sizeof(struct node));
tmp->item=data;
tmp->next=NULL;

if(start==NULL) /*if list is empty*/
start=tmp;
else
{ /*element inserted at the end*/
q=start;
while(q->next!=NULL)
{
q=q->next;
q->next=tmp;
} /*end of while*/
} /* end of else*/
} /*end of create_list()*/

display()
{
struct node *q;
if(start==NULL)
{
printf("List is empty");
return;
} /*end of if()*/
q=start;
printf("nnList is :n");
while(q!=NULL)
{
printf("t%d",q->item);
q=q->next;
} /*end of while()*/

} /*end of display()*/
November 11, 2009, 8:48 am

deepak said:

  #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link_list
{
int num;
struct link_list *next;
};
typedef struct link_list node;

main()
{
int item;
node *start,*p;
void creat(node *);
void print(node *);
clrscr();
start=(node *)malloc(sizeof(node));
creat(start);
print(start);
p=(node *)malloc(sizeof(node));
printf("put a item which you want to add");
scanf("%d",&item);
p->num=item;
p->next=start;
start=p;
print(start);
getch();
}
void creat(node *list)
{
printf("enter the no.:put-999 to exit");
scanf("%d",&list->num);
if(list->num==-999)
{
list->next='';
return;
}
else
{
list->next=(node *)malloc(sizeof(node));
creat(list->next);
}
}
void print(node *list)
{
if(list->next!='')
{
printf("the item=%d",list->num);
print(list->next);
}
}
December 28, 2009, 6:57 pm

anil kumar said:

  liner collection of yourself referential class object,called node
January 2, 2010, 3:41 am

aarti said:

  // Operation on singly Linked List.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *newnode,*temp,*start=NULL,*x;
void create()
{
do
{
newnode=malloc(sizeof(struct node));
printf("n Enter the data ");
scanf("%d",&newnode->data);
if(newnode->data==0)
break;
newnode->next=NULL;
if(start==NULL)
start=newnode;
else
{
for(temp=start;temp->next!=NULL;temp=temp->next);
temp->next=newnode;
}
}while(1);
}

void display()
{
if(start==NULL)
{
printf("n Linklist is EMPTY ");
}
else
{
for(temp=start;temp!=NULL;temp=temp->next)
{
printf("n %d",temp->data);
}
}
}
void insert()
{
int i,pos;
newnode=malloc(sizeof(struct node));
printf("n Enter the data which you want to insert ");
scanf("%d",&newnode->data);
newnode->next=NULL;
printf("n Enter the position ");
scanf("%d",&pos);
if(pos==1)
{
newnode->next=start;


start=newnode;
}
for(temp=start,i=1;temp!=NULL;temp=temp->next,i++)
{
if(i==pos-1)
{
newnode->next=temp->next;
temp->next=newnode;
}
}
}
void delete()
{
int pos,i;
printf("n Enter the position of node which you want to delete ");
scanf("%d",&pos);
for(temp=start,i=1;temp!=NULL;temp=temp->next,i++)
{
if(i==pos-1)
{
x=temp->next;
temp->next=temp->next->next;
free(x);
}
}
}
void erase()
{
start=NULL;
}
void main()
{
int ch;
clrscr();
while(ch!=6)
{
printf("nt MENU n1.Create n2.Insert n3.Delete n4.Erase n5.Display n6.Exit");
printf("n Enter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
erase();
break;
case 5:



display();
break;
}
}getch();
}



/* OUTPUT :


MENU
1.Create
2.Insert
3.Delete
4.Erase
5.Display
6.Exit
Enter your choice 1

Enter the data 1

Enter the data 2

Enter the data 3


MENU
1.Create
2.Insert
3.Delete
4.Erase
5.Display
6.Exit

Enter your choice 2

Enter the data which you want to insert 10

Enter the position 1

MENU
1.Create
2.Insert
3.Delete
4.Erase
5.Display
6.Exit
Enter your choice 5

10
1
2
3


MENU
1.Create
2.Insert



3.Delete
4.Erase
5.Display
6.Exit
Enter your choice 3

Enter the position of node which you want to delete 1

MENU
1.Create
2.Insert
3.Delete
4.Erase
5.Display
6.Exit
Enter your choice 5

1
2
3

MENU
1.Create
2.Insert
3.Delete
4.Erase
5.Display
6.Exit
Enter your choice 4

MENU
1.Create
2.Insert
3.Delete
4.Erase
5.Display
6.Exit
Enter your choice 5

Linklist is EMPTY

MENU
1.Create
2.Insert
3.Delete
4.Erase
5.Display
6.Exit
Enter your choice 6*/
January 6, 2010, 1:16 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape