Linked List Notes
Linked List Notes
Linked List
A linked list is a linear collection of data elements. These data elements are called
nodes.
Each node is divided into two parts
1. Information field
2. Pointer to the Next Node / Link Field
Information field Pointer to the Next Node or Link
Field
The information field holds the actual value to be stored and accessed in a list
The pointer to the next node or link field contains the address of the next
node.
For example the following figure illustrates a linked list structure with 4
nodes, each node containing one information field and one pointer field
A linked list is a sequence of nodes in which each node contains one or more
data fields and a pointer to the next node
It is a dynamic data structure
The data elements in the linked list are not in consecutive memory locations.
Representation of Linked List in Memory
There are two ways represent a linked list in memory
1. Static representation using array
2. Dynamic representation
Static Representation
Static representation of a single linked list maintains two arrays one array for
information and the other for the links or pointers as INFO[] and LINK[]
These two arrays should be of equal size and parallel to each other.
The memory size of these two arrays should sufficient to store the entire
linked list
Length Operation
This operation is used to find the number of elements or nodes in the given existing
linked list.
If linked list is empty then it returns zero
Function to find the length of Linked List
This
int length()
{
int len=0;
NODE *CURRPTR;
if (start== NULL)
{
printf(“The linked list is empty \n”);
return(len);
}
CURRPTR=start;
while (CURRPTR!=NULL)
{
len++;
CURRPTR=CURRPTR->LINK;
}
return(len);
}
Searching in a Linked List
Searching operation refers to search a particular element in the list for searching we
have to compare each element of the list with the required element until the element
is found. If search is successful, it return the location of that node otherwise it
returns NULL
Algorithm to search an element in a linked list
Step 1: set CURRPTR=start, loc = NULL
Step 2: Repeat step3 while CURRPTR!=NULL
Step 3: if(Item==INFO[CURRPTR])
then LOC = CURRPTR
and display “Search successful”
EXIT
else
CURRPTR=LINK[CURRPTR]
Step 4: if LOC = NULL
display “search unsuccessful”
Step 5: Exit
Function to Search an Item in a Linked List
void search(int ITEM)
{
int i=0, itemFound=0;
NODE *CURRPTR=start;
while(CURRPTR!=NULL)
{
i++;
If (ITEM==CURRPTR->INFO)
{
itemFound=1;
break;
}
Else
CURRPTR=CURRPTR->LINK;
}
if(itemFound)
prinntf(“\n the item %d is found at position no :%d”,ITEM,i);
else
printf(“\n the item/node does not exist”);
}