0% found this document useful (0 votes)
25 views20 pages

Linked List

Uploaded by

Akash Gaonkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views20 pages

Linked List

Uploaded by

Akash Gaonkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT 2 Data Structures Using C 1

Chapter 2
Linked List
Linked list is a linear collection of data elements called nodes. Each node is divided into 2
fields. The first part is for info or data field and second part for pointer or link field to the
next node. The general representation is

INFO LINK
Where INFO is used to store the data value, LINK is used to store the address of next node.
Example:
START
5 8 3 NULL

Here START is an external pointer it stores the address of the first node. NULL indicates it
is the end of linked list.
Advantages of Linked List
 Efficient memory utilization
 Insertion and Deletion operation are easier
 Dynamic Data Structure
 Extensive manipulation
 Arbitrary memory location
 Efficient memory utilization
Memory can be allocated when ever we are inserting a new node and we deallocate when it is
deleted.
 Insertion and Deletion operation are easier
Linked List flexibility in inserting a data item at a specified position and Deletion of a data
item from a given position.
 Dynamic Data Structure
Linked List can grow or shrink during the execution of program.
 Extensive manipulation
We perform a complex manipulation a Linked Link without having any prior knowledge of
memory space available.
 Arbitrary memory location
In Linked List memory location are not consecutive, it may arbitrary. But the data is linked
with previous node. So, we can store the data in affecting memory effectively by using the
link value in Linked List.
Disadvantages of Linked List
1. It requires more memory than array because each node has a pointer, which requires
more memory.
UNIT 2 Data Structures Using C 2

2. Difficult to traverse the nodes in a linked list.


3. Reverse traversing in a linked list is very difficult, because it requires more memory
for the pointer.
Representation of Linked list in memory
List can be represented in 2 ways.
 Static representation
 Dynamic representation
Static Representation
Static representation of singly linked list maintains 2 arrays
1) INFO[] 2) LINK[]
Example
START
INFO[] LINK[]
1 A 3
2
3 B 5
4
5 C NULL
Here, INFO[1]=A, LINK[1]=3 ,
INFO[3]=B, LINK[3]=5 and
INFO[5]=C, LINK[5]=NULL.
i) The memory size of those 2 arrays should be sufficient to store the entire linked
list
ii) Two arrays should be in equal size and parallel to each other.
Dynamic Representation
The process of allocating memory at runtime is known as dynamic memory allocation.
Functions for allocating memory:
i) malloc() ii) calloc()
Functions for deallocating memory:
i) free()
In dynamic representation of Linked List, a node is created dynamically whenever it required.
To create a node of Linked List, a structure is to be defined and it holds the data and address
of the next structure data.
UNIT 2 Data Structures Using C 3

struct node
{
int info;
struct node *link;
};
It is a self-referential structure in C. Here link is the pointer that points the next data of same
structure.
Types of Linked List
Linked List are classified into 5 types:
 Singly Linked List
 Circular Linked List
 Doubly Linked List
 Header Linked List
 Circular Doubly Linked List
Singly Linked List
A singly linked list is a linear collection of data elements called nodes, where each node is
divided into 2 fields: Info field and link field. The link field points the next node and the link
field of last node points NULL.
Example:
START
1 58 24 NULL
0

Operations on Singly Linked List


 Creation
 Traversal
 Length
 Insertion
 Deletion
 Searching
 Sorting
 Merging
 Reversing
Creation
This operation creates a linked list. The following algorithm creates the Linked List
Algorithm
1. Create a node and assign the address to start pointer
CURNODE=(NODE*) malloc(sizeof(NODE));
START=CURNODE
2. Store the data
CURNODEINFO=item;
UNIT 2 Data Structures Using C 4

3. Store null character in link field


CURNODELINK=NULL
4. Return
Explanation
The first step is to create a node with 2 fields and assign the address of the node to start
START

INFO LINK
The second step get the data item and place in INFO field
START
50
INFO LINK
The third step place NULL value in Link fields
START
50 NULL
INFO LINK
The fourth step will return to main program.

Traversal
The process of visiting all nodes in Linked List is known as traversing. The following
algorithm traverse the Linked List
Algorithm
1. Is list is empty?
If(START==NULL)
Display “Linked list is empty”
Return
End if
2. Assign start value to CURPTR
CURPTR=start
3. Repeat while (CURPTR != NULL)
Print CURPTRINFO
CURPTR=CURPTRLINK
4. Return
Example
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 201 198
5
10 58 24 NUL
L
UNIT 2 Data Structures Using C 5

The first step is to check START is NULL. Here START=2002


The second step is to assign START value to CURPTR=START ie., CURPTR=2002
The third step it checks CURPTR is NULL, if it is not then it print the INFO value 10.
Repeatedly until CURPTR become NULL.
The following code illustrates the Linked List traversal
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:");
while(ptr!=NULL)
{
printf("%dt",ptr->info );
ptr=ptr->next ;
}
}
}

Insertion
This operation is used to insert a new node in the Linked List. The new node can be inserted
in 3 ways:
1) Insert at the beginning of a Linked List
2) Insert at the end of a Linked List
3) Insert at the specified position in Linked List
1) Insert at the beginning of a Linked List
The following algorithm insert a node at the beginning.
Algorithm:
1. Create a node using malloc
NEWNODE=(NODE*) malloc(sizeof (NODE))
2. Store the data
NEWNODEINFO=ITEM
3. Assign the start value to the link of NEWNODE
NEWNODELINK=start
4. Assign NEWNODE as START
Start=NEWNODE
UNIT 2 Data Structures Using C 6

5. Exit
Explanation
Consider the following linked list contain 3 nodes
START
2010 201 215 1003
0
10 90 24 NUL
L

The first step creates a node with two fields

2025

NEWNODE=2025
The second step stores data in INFO field

2025
50
The third step assign NEWNODE link to START

2025
50 2010

START
2010 201 215 1003
0
10 90 24 NUL
L

The fourth step to assign START as NEWNODE.


START
2025 202 201 215 1003
5 0
50 10 90 24 NULL

The following code illustrates inserting a node in the beginning of the Linked List
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
UNIT 2 Data Structures Using C 7

printf("\nEnter the data value for the node:" );


scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}

2) Insert at the end of a Linked List


The following algorithm insert a node at the end of Linked List
Algorithm:
1. Is List is empty?
if(Start = = NULL)
Call insert at the beginning
Exit()
2. Assign the Start to CURPTR
CURPTR=Start
3. Traverse the List to obtain Last node address
while(CURPTR  LINK != NULL)
CURPTR=CURPTR  LINK
4. Create a node using malloc
NEWNODE=(NODE*) malloc(sizeof (NODE))
5. Store the data
NEWNODEINFO=ITEM
6. Create a link between Last Node and NEWNODE
CURPTR  LINK = NEWNODE
NEWNODELINK=NULL
7. Exit
Explanation
Consider the following linked list contain 3 nodes
START
2010 201 215 1003
0
10 90 24 NUL
L

The first step check START is NULL, if it is NULL it call Insert at beginning otherwise
it assign Start value to CURPTR
The third step is traverse to point the last node
START
UNIT 2 Data Structures Using C 8

2010 201 215 1003


0
10 90 24 NUL
L

In fourth step creates a node with two fields

2025

NEWNODE=2025
The fifth step stores data in INFO field

2025
50
The sixth step assign NEWNODE link to CURPTR  LINK and NEWNODE 
LINK = NULL. The pictorial representation is

START
2010 2010 215 100 2025
3
10 90 24 50 NULL

The following code illustrates inserting a node at the end of the Linked List
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the data value for the node:" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
UNIT 2 Data Structures Using C 9

3) Insert at the specified position in Linked List


The following algorithm insert a node a specified position in Linked List
Algorithm
1. Assign Start to CURPTR
CURPTR=Start
2. Check position if( POS == 1)
Call insert at beginning
Exit()
3. Set the CURPTR to specified position
for ( i=0; i<pos-2;i++ )
CURPTR=CURPTR  LINK
4. Create a new node using malloc()
NEWNODE=(NODE*) malloc(sizeof(NODE))
5. Store data into NEWNODE  INFO
NEWNODE  INFO = ITEM
6. Create the link between NEWNODE and CURPTR
NEWNODE  LINK =CURPTR  LINK
CURPTR  LINK = NEWNODE
7. Exit
Explanation
Consider the following linked list contain 3 nodes
START
2010 201 215 1003
0
10 90 24 NUL
L

In first step it assign Start value to CURPTR.


The second step it check position value if it is one it call insert at beginning()
In third step it set CURPTR to specified position.
In fourth step it creates new node

2025

NEWNODE=2025
In fifth step it assigns INFO Value to NEWNODE

2025
50
UNIT 2 Data Structures Using C 10

In sixth step it creates link between CURPTR and NEWNODE. Consider pos is 2.The
pictorial representation is as follows.
START
2010 2010 2025 215 1003
10 50 90 24 NULL

The following code illustrates inserting a node at a specific position of the Linked List
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the position for the new node to be inserted:");
scanf("%d",&pos);
printf("\nEnter the data value of the node:");
scanf("%d",&temp->info) ;

temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}

DELETION
This operation is used to delete a node from the Linked List. A node can be deleted using
three ways:
UNIT 2 Data Structures Using C 11

1) Delete a node from the beginning of a Linked List


2) Delete a node from end of a Linked List
3) Delete a node from a specified position in Linked List
1) Delete a node from the beginning of a Linked List
This operation deletes a node from the beginning of Linked List.
Algorithm
1. Is List is Empty?
if (START= = NULL)
print “List is Empty”
else
go to step 2
2. Assign Start value to CURPTR
CURPTR = Start
3. Assign Link field of CURPTR to Start
Start= CURPTR  LINK
4. Using free() delete CURPTR
free(CURPTR)
5. Exit
Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 201 198
5
10 58 24 NUL
L

The first step check Start is NULL. If it is print list is empty otherwise step2
In second step assign CURPTR by Start (Here Start = 2002 therefore, CURPTR=
2002)
In third step set the CURPTR  LINK to Start
In fourth step free the CURPTR
The pictorial representation after deletion is
START
2015 201 198
5
58 24 NULL

The following code illustrates delete a node from the beginning of the Linked List
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty\n");
return;
UNIT 2 Data Structures Using C 12

}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is : %d",ptr->info);
free(ptr);
}
}

2) Delete a node from end of a Linked List


This operation delete the node from end of Linked List
Algorithm
1. Is List is empty?
if( Start = = NULL)
print (“List is Empty”)
else
step 2
2. Check list contain one element
if (Start  LINK = = NULL) then
Start = NULL
else
Step 4
3. delete start node
free (Start)
4. assign Start value to CURPTR
CURPTR = Start
5. assign NULL to PREPTR
PREPTR= NULL
6. Traverse the Linked List until the CURPTR pointer points the last node.
while(CURPTR  LINK != NULL)
{
PREPTR = CURPTR
CURPTR= CURPTR  LINK
}
7. Delete CURPTR
free (CURPTR)
8. Set PREPTR  LINK = NULL
9. Exit
Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 201 198
5
10 58 24 NUL
L
UNIT 2 Data Structures Using C 13

The first step check Start is NULL. If it is print list is empty otherwise step2
In second step it check is list contain one element if it is set Start is NULL otherwise
step4
In third step delete start
In fourth step Assign start to CURPTR
In fifth step set PREPTR to NULL
In sixth step traverse the list until CURPTR points the last node.
In seventh step delete CURPTR
In eight step set PREPTR  LINK as NULL
START
2002 2002 2015
10 58 NULL

The following code illustrates delete a node from end of the Linked List

void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty\n");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
}

3) Delete a node from a specified position in Linked List


This operation delete the node at a given position.
UNIT 2 Data Structures Using C 14

Algorithm
1. if (POS = = 1)
call delete_beginning()
else
goto step 2
2. set CURPTR to Start
CURPTR=Start
3. set PREVPTR to NULL
PREVPTR = NULL
4. set CURPTR to specified position using loop
for(i=1; i<POS; i++)
{
PREVPTR=CURPTR
CURPTR=CURPTR  LINK
}
5. create link between PREVPTR and the nextnode of CURPTR
PREVPTR  LINK = CURPTR  LINK
free(CURPTR)
6. Exit
Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 201 198
5
10 58 24 NUL
L

The first step check POS= = 1 if it call delete_beginning otherwise goto step2
In second step Assign start to CURPTR
In third step set PREPTR to NULL
In fourth step CURPTR as specified position using loop.
In fifth step set link between PREPTR and next node of CURPTR. Then delete the
CURPTR.

In seventh step delete CURPTR


In eighth step set PREPTR  LINK as NULL
START
2002 200 198
2
10 58 NULL

The following code illustrates delete a node from a specific position of the Linked List
void delete_pos()
{
UNIT 2 Data Structures Using C 15

int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is: %d",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d",ptr->info );
free(ptr);
}
}
}

Length
This operation is used to find the number of nodes in Linked List
Algorithm
1. Initialize len=0
2. If (Start = = NULL)
{
Print “List is Empty”
Return
}
UNIT 2 Data Structures Using C 16

3. Set CURPTR=Start
4. while(CURPTR != NULL)
{
len++
CURPTR= CURPTR  LINK
}
5. return len
Explanation
Consider a Linked List with the following nodes:
START
2002 2002 201 198
5
10 58 24 NUL
L
In the first step it initializes the variable len as 0
In the second step it checks whether the Start is NULL , Here Start =2002
In the third step it assigns the Start value to CURPTR that is CURPTR =2002
In the fourth step it checks CURPTR is NULL if it is not then it increment Len value by 1
and set CURPTR= CURPTR  LINK. Repeat until CURPTR is NULL
In fifth step it returns len value , Here len=3
Searching
It is a process of searching an element in a linked list.
Algorithm
1. Assign CURPTR= start, LOC = NULL
2. Repeat step 3 while (CURPTR != NULL)
3. If(ITEM = = CURPTR  INFO)
{
LOC= CURPTR
Print “Search Successful”
}
Else
CURPTR= CURPTR  LINK
4. If (LOC = = NULL)
Print “Search Unsuccessful”
5. Exit

Consider a Linked List with the following nodes and it search 58


START
2002 2002 201 198
UNIT 2 Data Structures Using C 17

5
10 58 24 NUL
L

In the first step it assigns CURPTR= Start and LOC =NULL


In the second step it checks CURPTR is NULL if not it repeat step 3
In the third step it checks ITEM = = CURPTR  INFO if so assign CURPTR to LOC and
display “Successful” otherwise CURPTR = CURPTR  LINK
In the fourth step, if LOC is NULL then display “Unsuccessful”
In the fifth step exit
Types of Linked List
Linked List are classified into 5 types:
 Singly Linked List
 Circular Linked List
 Doubly Linked List
 Header Linked List
 Circular Doubly Linked List
Singly Linked List
A singly linked list is a linear collection of data elements called nodes, where each node is
divided into 2 fields: Info field and link field. The link field points the next node and the link
field of last node points NULL.
Example:
START
1 58 24 NULL
0

Circular Linked List


Circular linked list is similar as singly linked list except that the last node always points the
first node instead of null.
Consider the following pictorial representation shows circular linked list.

2002 201 198


5
10 58 24

In circular linked list, only one pointer is used for insertion and deletion.
UNIT 2 Data Structures Using C 18

Advantages of circular linked list


1. It can be traversed from any node
2. All the nodes link address will have valid address instead NULL
3. Node can be inserted or deleted from any position of the linked list
4. Traverse the whole list from any node
Disadvantages of circular linked list
1. Difficult to reverse the circular linked list.
2. If proper care is not taken, then the problem of infinite loop can occur
Doubly Linked List
A doubly linked list is the kind of linked list, in which a node has three fields:
 INFO
 BACK
 FORW
The INFO field stores the data and the BACK field stores the address of the previous node
and the FORW field stores the address of the next node.
The node in doubly linked list is represented below:

BACK INFO FORW


The following code declare the doubly linked list using dynamic implementation.
struct node
{
int INFO;
struct node *BACK;
struct node *FORW;
};
The pictorial representation of doubly linked list is illustrated below.
START
1800 1800 4020 5042
NULL 24 4200 1800 67 5042 4020 80 NULL
Advantages of Doubly Linked List
1. It can be traversed in forward and backward direction
2. Insertion and deletion are easy because every node has forward and backward link
3. It is used to represent the trees effectively
4. This simplifies list management
Disadvantages of Doubly Linked List
1. Extra memory is required to store the back pointer.
2. Extra effort is required to deal with the extra link.
Header Linked List
 A special node in the beginning of the list is known as header node.
UNIT 2 Data Structures Using C 19

 This node does not contain actual data but contains useful information about the entire
linked list such as total number of nodes, pointer to the last node etc.
 The start points to the header node in the header linked list.
 Link field of header node points the first node in the list.
 The header linked list will always contain a header node even if list is empty.
Header linked list are of two types:
 Grounded header linked list
 Circular header linked list
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.
Example
START
2002 2002 201 198
5
58 24 NUL
L
Header node

Circular header linked list


A linked list in which last node points to the header node is called circular header linked list.
Example

Head Node 2015 198


58 24

Define GetNode and FreeNode operations on Linked list


The finite pool of empty nodes in memory is called Availability list.
The getNode() operation removes the first node from the availability list for use.
The freeNode() operation adds a node to the front of the availability list.
What is a Garbage and what is Garbage Collection (GC)?
Garbage is a block of heap memory that does not have a reference to it. It can also be a
reference that exists to a block of memory that is no longer allocated.
Garbage Collection is automatic management of dynamically allocated storage. It reclaims
unused heap blocks for later use by program.
UNIT 2 Data Structures Using C 20

You might also like