0% found this document useful (0 votes)
18 views

Linked List

data structure notes

Uploaded by

kashif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Linked List

data structure notes

Uploaded by

kashif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Linked list

Linked list is a linear data structure that includes a series of connected nodes. Linked list can be
defined as the nodes that are randomly stored in the memory. A node in the linked list contains
two parts, i.e., first is the data part and second is the address part. The last node of the list
contains a pointer to the null. After array, linked list is the second most used data structure. In a
linked list, every link contains a connection to another link.

The problems with arrays are:


• When a number of users share main memory, there may not be enough
adjacent memory locations left to hold an array. But there could be enough
memory in the shape of small free blocks.
• The second major problem with array is when we have a large list of data
elements and exact number of elements cannot be known in advance while an
array has a fixed size and we cannot increase the size of array on run time
when additional memory is required; therefore, arrays are called static data
structure.
• The data access speed becomes slow when the size of the array becomes large.

To overcome these limitations Linked Lists are used. In a link list the elements are
logically adjacent needs not to be physically adjacent in the memory, but they
should be linked or connected through a pointer.
Type of link List

Singly Linked List

Doubly Linked List

Circular Linked List

Circular Doubly Linked List

Header Linked List

Singly Linked List:


In one-way link list, a node (each element of a link list is called a “node”) have at least two
fields, the first one is the data or information field (more than one data fields can be used in a
node to store information) which contain the actual data of a list and the other field is called
link field or next-pointer field which contain the address of the next node of a link list.

Data /Information Pointer

You have to start somewhere, so we give the address of the first node a special name called
HEAD. Also, the last node in the linked list can be identified because its next portion points to
NULL.
Example:
In the following diagram of a one-way link list have four nodes. Each node has two parts.
The left part represents the information part of the node and the right part represents pointer
field or link field, which contains the memory address of the next node. In the list we have a
start node whose address is stored in a pointer START. We need a START pointer to trace the
list. A special case is the list that when the list has no nodes. Such a list is called a null list or
empty list and denoted by null pointer in the START pointer.
START
1000

1000 1001 1002


10 1001 20 1002
1003 30 1003 40 NULL

A big benefit with using linked lists is that nodes are stored wherever there is free space in
memory, the nodes do not have to be stored contiguously right after each other like elements are
stored in arrays. Another nice thing with linked lists is that when adding or removing nodes, the
rest of the nodes in the list do not have to be shifted.

Operations on One Way Linked List with

algorithms:
1. Insertion Operation:
Let us suppose LIST be a one-way link list with N successive nodes and a node
insert is to be inserted in a link list. The given node can be inserted in a link list in
the following three locations.
➢ Front Insertion
➢ Middle Insertion
➢ End Insertion
I. Front Insertion:
It is the easiest way to insert a node in the link list. Following algorithm is used to insert
a
node in the front of the LIST.

ALGORITHM: FRONT INSERTION (Insert, Node, First, Data, Info) This algorithm is used
to insert a node insert at the start of a one-way link list.
Step 1: [Create a node and assign it to insert]

Insert = (struct node *) malloc (size of (struct node))


Step 2: [Store Information]
Insert -> data = info
Step 3: [assign first to the insert link]
Insert -> link = First
Step 4: [Set first as insert]
First = Insert
Step 5: [Finish]
Exit
In step 1, 2 & 3 we create a node insert, store information in it and assign to the first
node address. In step 4, we assign insert to first pointer to make newly inserted
node the first node of the LIST. The following figure shows the above mechanism:
FIRST
1004

1000 1001 1002


10 1001 1003
20 1002 30 1003
40 NULL
1004
05 1000
INSERT

II. Middle Insertion: There are two methods, which can be used to insert node at
the middle if a one-way link list.
Method 1: If the nodes of the given list are unsorted then we insert node in a list after
a given node.
Method 2: If the nodes of the given list are sorted in some particular order,
then the node is inserted at a particular position so that the sorted
order could be maintained.
Here is discussed the second method. The following algorithm inserts a node at a proper
place.
ALGORITHM: MIDDLE INSERTION (Prev, First, Cur, X, Insert)

This algorithm is used to insert a node at the proper location in as ordered LIST

Step 1 [Set Prev to First]


Prev = First
Step 2: [Set Cur to Prev Link]
Cur = Prev -> link
Step 3: [Read value for insertion]
Read (X)
Step 4: [Search for a proper location]
Repeat step 5 & 6 while (Cur -> data < X)
Step 5: [Set Cur as Prev]
Prev = Cur
Step 6: [Set Cur to Cur Link]
Cur = Cur -> link
Step 7: [Create a Node and set it as Insert]
Insert = (struct node *) malloc (sizeof (struct node))
Step 8: [Store information]
Insert -> data = X
Step 9: [Link with Cur]
Insert -> link = Cur
Step 10: [Link Prev with Insert]
Prev -> link = Insert
Step 11: [Finish]
Exit
In above algorithm step 4, 5 & 6 are used to find out a proper position for a new
node. In step 7 & 8, we create a new node, assign its address to insert pointer
and store information. In step 9, 10 & 11, the new node is linked to the current and
previous nodes.
The following figure shows the above mechanism:

I. End Insertion:
By end insertion, means to insert a node at the end/last of a link list. The
algorithm is given as follow:

ALGORITHM: END INSERTION (Prev, First, Cur, X, Insert)


This algorithm is used to insert a node insert at the end of a one-way link list.
In the above algorithm step 2 & 3 are used to reach to the end of link list. When end
of list is found then in step 4, 5 & 6, we create a new node; assign its address to insert
pointer, store information and set insert pointer as NULL, because null link
pointer shows the end of the list. In step 7, the new node is linked to previous node.

The following figure shows the above mechanism.

2. Deletion Operation:
Let LIST be a given link list with N successive nodes and we want to delete a node
X from a link list. Then we can delete that node from three different location of a
link list, which is given as:
➢ Front Deletion
➢ Middle Deletion
➢ End Deletion

I. Front Deletion: Front deletion is the easiest way to delete a node from a link
list. Following is the algorithm is used to delete a node from one-way link list from
the front or start.

ALGORITHM: FRONT DELETION (Cur, First)

This algorithm is used to delete a node from the front of a one-way link list.

Step 1: [Set Cur to First] Step 1: [Set Prev to First]


Cur = First Prev = First
Step 2: [Update First] OR Step 2: [Set Cur to Prev link]
First = Cur -> link Cur = Prev -> link
Step 3: [Delete Node] Step 3: [Update Prev]
Free (Cur) Prev -> link = Cur -> link
Step 4: [Finish] Step 4: [Delete Node]
Exit Free (Cur)
Step 5: [Finish]
Exit
In the above algorithm in step 1, we store the address of first node in current
pointer. In step 2 we update the first to the next node to make the second node as
first. In step 3, we delete the node by free () function provided in C language.

The following figure shows the above mechanism.

I. Middle Deletion: In middle deletion if we want to delete node X from a linked list
which is not at the first or last location. For this we start searching for that node in a
list. If found then we should update successor and predecessor and then delete the
node. The following algorithm is used to delete a node from middle form one-way
link list.

ALGORITHM: MIDDLE DELETION (Prev, Cur, First, X)


This algorithm is used to delete a node from middle form one-way link list.
In the above algorithm in step 1, 2 we assign previous pointer to first node and cur
to the next of first node. In step 4, 5, 6 & 7 we start loop for searching for the node,
if it is found it is deleted and exited otherwise the prev is updated to cur and cur is
updated to the next node.

The following figure shows the above mechanism.

I. End Deletion: To delete the end node of a one-way link list first we have to reach to
the end of the list.

ALGORITHM: END DELETION (Prev, First, Cur)


This algorithm is used to delete the end node of a one-way link list.

In the above algorithm in step 1 & 2 we assign prev to first node and cur to the next
node to the prev. In step 3, 4 & 5, we reach to the end of the list. In step 6 & 7, we
assign Null to prev to make it the end node and free cur pointer to delete the end
node.
The following figure shows the above mechanism.

FIRST
1000

1000 1001 1002 1003


10 1001 20 1002 30 NULL ….. 40 NULL
PREV CUR

Time Complexity
Accessing an Element (Search)

• Best Case: O(1)

o Explanation: The best case occurs when the element you are looking for is at the
head of the list.

• Average Case: O(n)

o Explanation: On average, you may need to search through half the elements to
find the target, but the complexity is still linear because of the linear nature of
traversal.

• Worst Case: O(n)

o Explanation: The worst case occurs when the element is at the end of the list or
not present at all, requiring traversal of the entire list.

Inserting an Element

• At the Beginning:

o Best, Average, Worst Case: O(1)

▪ Explanation: Inserting at the beginning is always O(1) because it only


requires updating the head pointer to the new node and pointing the new
node to the old head.

• At the End:

o Best Case: O(1) (if you have a tail pointer)


▪ Explanation: If there is a tail pointer, inserting at the end is O(1) because
you can directly access the last node.

o Average and Worst Case: O(n)

▪ Explanation: Without a tail pointer, you must traverse the entire list to
find the last node, making it linear time.

• At a Specific Position:

o Best Case: O(1) (if the position is the head)

▪ Explanation: If the position is at the head (index 0), insertion is O(1).

o Average and Worst Case: O(n)

▪ Explanation: For other positions, you need to traverse the list to reach the
specific position, making it linear time.

3. Deleting an Element

• At the Beginning:

o Best, Average, Worst Case: O(1)

▪ Explanation: Deleting at the beginning only requires updating the head


pointer, which is a constant-time operation.

• At the End:

o Best Case: O(1) (if there’s a tail pointer and a doubly linked list structure or
reference to the previous node)

▪ Explanation: In the ideal scenario (with additional data structures or


pointers), deleting the last node is O(1).

o Average and Worst Case: O(n)

▪ Explanation: Without a tail pointer or with a singly linked list, you must
traverse the list to find the second-to-last node to delete the last node,
making it linear time.

• At a Specific Position:

o Best Case: O(1) (if the position is the head)

▪ Explanation: If the position is at the head, deletion is O(1).


o Average and Worst Case: O(n)

▪ Explanation: For other positions, you must traverse the list to reach the
specific position, making it linear time.

4. Updating an Element

• Best Case: O(1)

o Explanation: If the element to be updated is at the head, the update is O(1).

• Average and Worst Case: O(n)

o Explanation: You need to traverse the list to the specific position, which is linear
time in the average and worst cases.
TWO WAY LINKED LIST
Introduction to Two Way Linked List/ Double Linked List:
One of the big disadvantages of one-way link list is that only possible way to traverse
the data is in the list is the forward traversing. There is no backward traversing of
a list in one-way link list. The problem is handled by the double or two-way link list.
In two-way link list each node is linked to both its successor and predecessor. The
two-way link list is traversable from either direction i.e. forward and backward. On
two-way link list a node is divided into three parts.
➢ Information part: It contains the data of a node.
➢ Right or next pointer: It points to the successor node.
➢ Left or previous pointer: Pointer to the predecessor node.

Left Information Right

In the following diagram of two-way link list with three nodes, each node has three
parts. The left part contains the address of predecessor node while the right part
contains the address of the successor node and the information part contains the
information about the element. There are two pointers also used i.e. FIRST and
LAST. The FIRST pointer points to the first or start node of the two-way link list
and the LAST pointer points to the last or end node of the two-way link list.

In the above example, structure node is defined with three fields:


• Data is the int type and is used to store integer values in the node.
• Left as a pointer to the left node. It is used to store the memory addresses.
It contains the memory address of the predecessor node of list.
Right as a pointer to the right node. It is used to store the memory addresses. It contains the
memory address of the successor node of list.
Operations on Two Way Linked List with algorithms:
1. Insertion Operation:
Let us suppose LIST be a Two-way link list with N successive nodes and a node
insert is to be inserted in a link list. The given node can be inserted in a link list in
the following three locations.
I. Front Insertion
II. Middle Insertion
III. End Insertion

I. Front Insertion:
Following algorithm is used to insert a node in the front/start of the Two-way link list.
I. Middle Insertion:

There are two methods that can be used to insert node at the middle of a Two-way link list.

Method 1: If the nodes of the given list are unsorted then we insert node in a list after
a given node.
Method 2: If the nodes of the given list are sorted in some particular order then
the node is inserted at a particular position so that the sorted order
could be maintained. We will discuss the second method. The
following algorithm inserts a node at a proper place in Two-way link
list.
ALGORITHM: MIDDLE INSERTION (Prev, Cur, First, Insert)

This algorithm is used to insert a node at the proper location in an ordered LIST in
Two-way link list.

In the above algorithm step 4, 5 & 6 are used to find out a proper position for a new node. In
step 7 & 8, we create a new node, assign its address to insert pointer and store information.
In step 9, 10 & 11, we link the new node to the current and previous nodes respectively.
The following figure shows the above mechanism

End Insertion:

End insertion in the two-way link list is much easier than the one –way link list
because we have a direct access to the end node of the two-way link list node.

Following is the algorithm for end insertion in Two-way link list.


ALGORITHM: END INSERTION (Prev, Last, Insert)
This algorithm is used to insert a node at the end of the Two-way link list.
1. Deletion Operation:

Let LIST be a given Two-way link list with N successive nodes and we want to delete
a node X from LIST. Then we can delete that node from three different location of a
Two-way link list which is given as:
I. Front Deletion
II. Middle Deletion
III. End Deletion

I. Front Deletion:

Front deletion is the easiest way to delete a node from a link list. Following is the
algorithm which is used to delete a node from Two-way link list from the front or start.
ALGORITHM: FRONT DELETION (Cur, First)

This algorithm is used to delete a node from the front of a Two-way link list.

In the above algorithm in step 1, we store the address of first node in current
pointer. In step 2, we update the FIRST to the next node to make the second node
as first. In step 3, we delete the node by free () function provided in C language. In
step 4, again we set the current pointer to point to first node and now current node
is the first node in the list. In step 5, we assign NULL to current left pointer to
make it first node of the list.
I. Middle Deletion:

Let LIST be a Two-way link list with N successive nodes and we want to delete a
node X from a linked list which is not at the FIRST or LAST location. For this we
start searching for that node in the list. If found then one should update the successor
and predecessor node pointers.

The following algorithm inserts a node at a proper place in Two-way link list.
ALGORITHM: MIDDLE DELETION (Prev, Cur, First)

This algorithm is used to delete a node at the proper location in Two-way link list.

In the above algorithm in step 1, 2, we assign previous pointer to first node and cur
to the next of first node. In step 4, 5, 6 & 7, we start loop for searching for the node,
if it is found it is deleted and exited otherwise the prev is updated to cur and cur is
updated to the next node at right side.
I. End Deletion:

End deletion is also very easy as front deletion in Two-way link list. Following algorithm is
used to delete end node of Two-way link list.
ALGORITHM: END DELETION (Cur, Last)

This algorithm is used to delete the end node of a Two-way link list.

In step 2 of the above algorithm we updated the LAST pointer to point the 2nd last
node in the list. In step 3, memory is free occupied by the last node. In step 4, again
set current pointer to last node, and now current node is the last node in the list.
Therefore, assign NULL to current right pointer.

The following figure shows the above mechanism:


Circular Linked List
A circular linked list is a type of linked list in which the first and the last nodes are also
connected to each other to form a circle.

There are basically two types of circular linked list:

1. Circular Singly Linked List

Here, the address of the last node consists of the address of the first node.

There are four main ways to add items:


• Insertion in an empty list
• Insertion at the beginning of the list
• Insertion at the end of the list
• Insertion at specific position in the list
For the insertion of a node at the beginning, we need to traverse the whole list. Also, for insertion
at the end, the whole list has to be traversed. If instead of the start pointer, we take a pointer to
the last node, then in both cases there won’t be any need to traverse the whole list. So insertion at
the beginning or at the end takes constant time, irrespective of the length of the list.

Insertion in the circular linked list:


Insertion is a fundamental operation in linked lists that involves adding a new node to the list.
The only extra step is connecting the last node to the first one. In the circular linked list
mentioned below, we can insert nodes in four ways:
Insertion in an empty List in the circular linked list
Step-by-step approach:
• Check if last is not nullptr. If true, return last (the list is not empty).
• Otherwise, Create a new node with the provided data.
o Set the new node’s next pointer to point to itself (circular link).
o Update last to point to the new node and return it.
Time Complexity: O(1)

Insertion at the beginning in circular linked list:


To insert a new node at the beginning of a circular linked list, we first create the new node and
allocate memory for it. If the list is empty (indicated by the last pointer being NULL), we make
the new node point to itself. If the list already contains nodes then we set the new node’s next
pointer to point to the current head of the list (which is last->next), and then update the last
node’s next pointer to point to the new node. This maintains the circular structure of the list.

Step-by-step approach:
• Create a new node with the given value.
• Check Empty List (last == nullptr):
o Make newNode->next point to itself.
• Insert at Beginning:
o Set newNode->next to last->next.
o Update last->next to newNode.

Time Complexity: O(1)


Insertion at the end in circular linked list:
To insert a new node at the end of a circular linked list, we first create the new node and allocate
memory for it. If the list is empty (mean, last or tail pointer being NULL), we initialize the list
with the new node and making it point to itself to form a circular structure. If the list already
contains nodes then we set the new node’s next pointer to point to the current head (which is tail-
>next), then update the current tail’s next pointer to point to the new node. Finally, we update the
tail pointer to the new node. This will ensure that the new node is now the last node in the list
while maintaining the circular linkage.
Step-by-step approach:
• Create a new node with the given value.
• Check Empty List, If last is nullptr then initialize the list with the new node and make it
point to itself.
• Otherwise, Set the new node’s next to the head node.
• Update the current last’s next to point to the new node.
• Update last to the new node.
Time Complexity: O(1)
Insertion at specific position in circular linked list
To insert a new node at a specific position in a circular linked list, we first check if the list is
empty. If it is and the position is not 1 then we print an error message because the position
doesn’t exist in the list. If the position is 1 then we create the new node and make it point to
itself. If the list is not empty, we create the new node and traverse the list to find the correct
insertion point. If the position is 1, we insert the new node at the beginning by adjusting the
pointers accordingly. For other positions, we traverse through the list until we reach the desired
position and inserting the new node by updating the pointers. If the new node is inserted at the
end, we also update the last pointer to reference the new node, maintaining the circular structure
of the list.

Step-by-step approach:
• If last is nullptr and pos is not 1, print “Invalid position!“.
• Otherwise, Create a new Node with given data.
• Insert at Beginning: If pos is 1, update pointers and return last.
• Traverse List: Loop to find the insertion point; print “Invalid position!” if out of bounds.
• Insert Node: Update pointers to insert the new node.
• Update Last: If inserted at the end, update last.

Time Complexity: O(n), we have to traverse the list to find the specific position.

Insertion in Doubly Circular Linked List

Circular Doubly Linked List has properties of both doubly linked list and circular linked list in
which two consecutive elements are linked or connected by the previous and next pointer and the
last node points to the first node by the next pointer and also the first node points to the last node
by the previous pointer.

Insertion in Circular Doubly Linked List:


1. Insertion at the end of the list or in an empty list:
A node(Say N) is inserted with data = 5. So, the previous pointer of N points to N and the next
pointer of N also points to N. But now start pointer points to the first node of the list.

List initially contains some nodes, start points to the first node of the List:
A node(Say M) is inserted with data = 7, so the previous pointer of M points to the last node, the
next pointer of M points to the first node and the last node’s next pointer points to this M node,
and first node’s previous pointer points to this M node.
Insertion at the beginning of the list:
To insert a node at the beginning of the list, create a node(Say T) with data = 5, T next pointer
points to the first node of the list, T previous pointer points to the last node of the list, last node’s
next pointer points to this T node, first node’s previous pointer also points this T node and at last
don’t forget to shift ‘Start’ pointer to this T node.

Insertion in between the nodes of the list:


To insert a node in between the list, two data values are required one after which new node will
be inserted and another is the data of the new node.
Time Complexity: O(N)

Header Linked List:

Header Linked List is a modified version of Singly Linked List. In Header linked list, we have a
special node, the Header Node present at the beginning of the linked list. The Header Node is an
extra node at the front of the list storing meaningful information about the list. Such a node is not
similar in structure to the other nodes in the list. It does not represent any items of the list like
other nodes rather than the information present in the Header node is global for all nodes such
as Count of Nodes in a List, Maximum among all Items, Minimum value among all Items etc.
This gives useful information about the Linked list.
Now, Header Linked List is of two types:
• Grounded Header Linked List
• Circular Header Linked List.
Grounded Header Linked List

In this type of Header Linked List, the last node of the list points to NULL or holds the reference
to NULL Pointer. The head pointer points to the Header node of the list. If the there is no node to the next
of head pointer or head.next equals NULL then we know that the Linked List is empty. The operations
performed on the Header Linked List are same as Singly Linked List such as Insertion, Deletion, and
Traversal of nodes. Let us understand this with an example:

The above image shows a Grounded Header Linked List. We can see the Header Node at the beginning
followed by the actual nodes of the list where the last node points to NULL. The header node maintains
global information about the list. Firstly, we have to create Header Node first whose data field will be
NULL or 0 initially. After that we can perform any operations on that linked list and store the global
information about the linked list on the header node. Here, we store the count or Total no. of nodes
present in the Linked List, the Maximum and Minimum value among all nodes in the list. So, in the above
Linked List Total Nodes are 4 , Maximum Value is 99 and Minimum value is 5 which are present as fields
in Header Node.

Circular Header Linked List:


A Linked List whose last node points back to the First node or the Head Node of the list is called a
Circular Linked List. Similarly, if the last node of the Header Linked List points back to Header Node
then it is a Circular Header Linked List. The last node of the list does not hold NULL reference. In this
case, We have to use external pointers to maintain the last node. The end of the list is not known while
traversing. It can perform same operations as its counterpart such as Insert, Delete and Search. Let us see
an example.

We can see the last node of the list points to the header node and not the node 99. There might be problem
in traversing through the nodes. It is because the structure of the last node and header node are different
which might cause an Type Mismatch Exception. So while traversing we fix the Header node and keep on
traversing until node. Next points to header node. The rest features of the Header remains same as its
Grounded counterpart.

You might also like