Introduction
LINKED LISTS: We know that a list refers to a set of items organized sequentially. An array is an example of list.
One major problem with array is that the size of an array must be specified precisely at the beginning.
A completely different way to represent a list is
to make each item in the list part of a structure that also contains a 'link' to the structure containing the next item. This type of list is called a
linked list because it is a list whose order is given by links from one item to the next.
A linked list is dynamic data structure. Therefore the primary advantage
of linked lists is that linked list can grow or shrink in size during the execution of the program.
Self-Referential Structure: Each structure of list is called a node and consists of two fields, one containing the item
and the other containing the address of the next item (A pointer to the next item) in the list.
A linked list is therefore a collection of structures
ordered not by their physical placement in the memory (like an array) but by logical links that are stored as part of the data in the structure itself.
The link is in the form of a pointer to another structure of the same type. Such structures which contain a member field that points to the same structure
type are called self-referential structures.
#include <stdio.h> void creation(); void display(); void deletion(); void insertion(int); struct Employee { char ename[20]; int eno; float esal; struct Employee *next; }; struct Employee *hptr=NULL,*tptr=NULL; void main() { int i; for(i=0;i<2;i++) { creation(); } display(); insertion(1); display(); deletion(); display(); } void creation() { struct Employee *nptr; nptr=(struct Employee *)malloc(sizeof(struct Employee)); printf("\n Enter the details of Employee"); printf("\n Number name and salary : "); scanf("%d %s %f",&nptr->eno,nptr->ename,&nptr->esal); if(hptr==NULL) { hptr=nptr; tptr=nptr; } else { tptr->next=nptr; tptr=nptr; } tptr->next=NULL; } void display() { struct Employee *eptr; printf("\n the details are"); for(eptr=hptr;eptr!=NULL;eptr=eptr->next) { printf("\n %d %s %f",eptr->eno,eptr->ename,eptr->esal); } } void insertion(int ps) { int count=1; int pos=ps; struct Employee *nptr; nptr=(struct Employee *)malloc(sizeof(struct Employee)); printf("\n Enter the record to be insert"); scanf("%d %s %f",&nptr->eno,nptr->ename,&nptr->esal); if(pos==1) { nptr->next=hptr; hptr=nptr; } else { for(tptr=hptr;tptr!=NULL;tptr=tptr->next) { if(count==(pos-1)) { nptr->next=tptr->next; tptr->next=nptr; break; } count++; } } } void deletion() { int ele; struct Employee *cptr,*prev=NULL; printf("\n Enter the record to deleted :"); scanf("%d",&ele); for(cptr=hptr;cptr!=NULL;prev=cptr,cptr=cptr->next) { if(cptr->eno==ele) { if(prev==NULL) { hptr=cptr->next; free(cptr); return; } else { prev->next=cptr->next; free(cptr); return; } } } for(cptr=hptr;cptr!=NULL;cptr=cptr->next) printf("\n %d",cptr->eno); }
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment
venkat 11/5/2012 (IST) / Reply
i am a diploma cse student.i am from hyd. i want short data struct throug c lang ....... plz any one help me.