You are on page 1of 14

THE NATIONAL DEGREE COLLEGE BAGEPALLI

DEPARTMENT OF COMPUTER SCIENCE

SUBJECT- DATA STRUCTURES

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

Here A B C represent the information and 1,2,3,,,6 represent the addresses of


the nodes that points to their next nodes.

Dynamic Representation of linked list


 The process of allocating memory at runtime is known as dynamic memory
allocation.
 The functions malloc() and calloc() are used allocate memory and free() is
used to deallocate memory
 One major problem with static representation is that the size of an array must
be specified precisely at the beginning
 In dynamic representation of linked list, a node is created dynamically
whenever it is required. Any number of nodes can be created depending upon
the requirement.
 Note that there is no need to setup any actual storage space at this stage as
space will be allocated for each node as needed when the program is running
Types of Linked List
1. Singly Linked List
2. Circular Linked List
3. Doubly Linked List
4. Header Linked List

Singly Linked List


A singly linked list or one way list is a linear collection of data elements called
nodes, Where each node is divided into two parts info field and link field.
The link field of ach node will point to the next node and link field of last node
always point to NULL

Circular Linked List


In singly linked list, link part of the last node has NULL value; it represents the last
node of linked list, but in circular linked lists, link part of the last node points to the
first node of the linked list and represents the linked list in circular way.

Advantages of circular linked list


1. The circular list can be traversed to any node from any node
2. All the nodes link address will have valid address, instead of NULL pointer.
3. One can start at any node in the list and traverse the whole list

Doubly Linked List


We know that in singly linked list one way list. sometimes it is required to traverse
the list in either forward direction or backward direction,. This increases the
performance and efficiency of algorithms so a two way list called doubly linked
list.
In doubly linked list we maintain two link fields called BACK and FORW. Now we
can define a doubly linked list as as collection of nodes, where each node is divided
into three fields
1) Pointer to previous node
2) Information field
3) Pointer to next node

Advantages of Doubly Linked List


The doubly linked list can be traversed in forward or backward direction.
This simplifies list management
Disadvantages of Doubly Linked List.
Extra memory is required to store the back pointer.
Header Linked List
A header linked list is a linked list which always contains a special node called the
header node at the beginning of the list.
The info field of such a header node generally contains the global information of
the entire list
The following are two kinds of widely used header linked list
1) Grounded header linked list
The Grounded header linked list is also referred as singly header linked list. In this
the last node LINK field contains the NULL pointer.

2) Circular header linked list


A linked list in which last node points to the header bode is called circular header
linked list
Operations on Single Linked List
1. Creation
2. Traversing/Display
3. Length
4. Searching
5. Insertion
6. Deletion
7. Sorting
8. Merging
9. Reversing

Creating s Linked List


In order to create a linked list the required number of nodes are to be created and
the information fields of all the nodes are to be accepted.
When the required number of elements are accepted, the Link fields of the last node
to be made ‘NULL’. And all these nodes are to be linked to make a linked list.
Algorithm for creating the linked list
Step 1: [ create first node and assign the address of first node to the pointer ‘start’,
now ‘start’ will contain the address of the first node]
Start=getnode()
Step 2:[use another pointer, to store the address of the first node, called CURRPTR]
CURRPTR=start;
Step 3:[Accept the element/item and store it in the INFO field of the node ]
INFO[CURRPTR]=ITEM;
Step 4:[Accept the choice from the keyboard whether to create another node or not]
If (choice = ‘Y’ )
Goto step 5
Else
Goto step 6
Step 5:[If choice is ‘Y’ , to create another node]
(i) NEWNODE = getnode();
Function to create Linked List
void create()
{
char ch;
int i=0;
NODE *CURRPTR, *NEWNODE;
CURRPTR = (NODE*) malloc (sizeof(NODE));
start=CURRPTR;
while(1)
{
printf(“\n Enter the node %d : “, i+1);
scanf(“%d”, &CURRPTR->INFO);
printf(“\n Do you wish to add one more node (Y/N):”);
ch=getche();
if (toupper(ch)==’y’)
{
NEWNODE = (NODE *)malloc(sizeof(NODE));
CURRPTR->LINK=NEWNODE;
CURRPTR=NEWNODE;
}
else
{
CURRPTR->LINK=NULL;
break;
}
i++;
}
}
Traversing a Linked List
Suppose we want to traverse a list in order to process each node exactly once. Let
linked list be stored in memory. Our aim is to process each node in the list starting
from the first node to the end of the list
Algorithm for traversing is given below
Step 1: [Is Linked list is empty? ]
If (start=NULL)
Display (“Linked list is empty”)
Exit()
Step 2:[Assign start value to CURRPTR]
CURRPTR=start
Step 3: Repeat while(CURRPTR!=NULL)
Process INFO[CURRPTR]
CURRPTR=LINK[CURRPTR]
Step 4: Exit
Displaying a Linked List
*If we want to display a linked list in a certain order, we can process each node
exactly once
*We should display the INFO fields starting from the first node to the end of the list
*For this let us consider a pointer variable CURRPTR that points to the first node
using CURRPTR=start and display the info field at this position. Then traverse the
list by making CURRPTR to point to the next node.
CURRPTR=LINK(CURRPTR)
Algorithm for Displaying a List
Step 1 : [check “LIST EMPTY”]
If(start==NULL)
Write(“LIST EMPTY”)
Return;
Step 2:set CURRPTR=start
[store the address of first node in another pointer CURRPTR]
Step 3:repeat step4 and step5 while (CURRPTR!=NULL)
Step 4:[Display the items one-by-one from the first node until CURRPTR becomes
NULL]
Display[INFO[CURRPTR]);
Step 5:[After the display of this element, CURRPTR should point to the next next
node]
CURRPTR=LINK[CURRPTR]
Step 6:Exit
Function to display the contents of linked list
void display()
{
NODE *CURRPTR = start;
if (start==NULL)
printf(“\n The Linked list is empty”);
else
{
While (CURRPTR != NULL)
{
printf(“%d”,CURRPTR->INFO);
printf(“->”);
CURRPTR=CURRPTR->LINK;
}
printf(“NULL”);
}
}

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”);
}

You might also like