Tutorials
C LanguageIn 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.
|
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
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.
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.
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
| your article is really nice and you have presented everything in a lucid manner so that i am able to clear my doubts. |
| This info is very useful to know about Linked Lists |
| It is a mindblowing concept which i came to know through your site. |
| this concept is so nice to understand... |
|
you have presented in clear and crisp format |
| You have written about linklist in very easy and fair understandable. |
| its a very good reference for linkedlists and very easy to understand |
| your article is very ease to understand for beginners... |
| good article.... really very useful to the beginners.. |
| still confused!!! help me |
| how to count the number of different element in linked list? |
|
please give one example program related to linked list....thnaks |
| 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. |
|
this is wonderful explanation about linked lists |
| Extremely nice explanation. |
| This website is very useful to understand easily... |
| Gud explantation......it useful for who is needing this |
| so nice so perfect |
| one of the most innovative sites ever! |
| explanation is good abut linked list, but still need with diagram so that everyone can easily understand |
| This website is very useful to understand easily & learn more... |
| It is very helpful website particularly for learning students |
|
#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()*/ |
|
#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()*/ |
|
#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); } } |
|
liner collection of yourself referential class object,called node |
|
// 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*/ |