SlideShare a Scribd company logo
Linked List Data Structures
Dr. Ashutosh Satapathy
Assistant Professor, Department of CSE
VR Siddhartha Engineering College
Kanuru, Vijayawada
August 24, 2024
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 1 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 2 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 3 / 172
Introduction
Array is a data structure where elements are stored in consecutive
memory location. It is simple to understand, time to access any
clement from an array is constant.
Some severe limitations can also be attributed to its simple structure.
1 The size of an array has to be defined when the program is being
written and its space is reserved during compilation of the program.
2 During execution of the program, it may happen that the actual size is
less than the defined size. So, a good amount of memory space is
wasted.
3 In contrast, while executing with larger data, the size of actual data
may exceed the limits of the defined size creating undesirable results.
4 During insertion operation in an array, the elements after the inserted
element have to be shifted one position to the right.
5 Similarly, during deletion all elements to the right of the deleted
element have to be shifted one position to the left.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 4 / 172
Introduction
Link representation of the list provides a sophisticated solution to the
data mobility issue that arises in arrays.
A dynamic data structure, or linked list, allows its memory
requirements to be adjusted as needed. Adjacency between elements
(nodes) in a linked list is preserved by links or pointers.
A link or pointer is the address of the element (node) that comes
after it. As a result, in a linked list, both the link and the data must
be maintained.
Two fields make up a node: info, which stores information, and next,
which points to the node after it.
Figure 1.1: Node structure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 5 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 6 / 172
Linked List Types
A data structure called a single linked list is made up of a series of
units known as nodes.
Each node is made up of two components: data and a pointer, or
reference, to the node after it in the sequence.
Traversal in a single linked list is forward-only, which means that
nodes can be traversed other than from the head node.
Because there is no direct indexing, this structure can be less efficient
for random access than arrays, but it can be efficient for insertion and
deletion operations at the beginning and end of the list.
Figure 1.2: Single linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 7 / 172
Linked List Types
A double linked list is a data structure used in computer science to
store a collection of elements.
In contrast to a singly linked list, where each node contains a
reference to the next node, a double linked list contains references to
both the next and the previous nodes.
This structure allows for efficient traversal in both forward and
backward directions.
Insertions and deletions can also be performed efficiently at both ends
or at any point within the list, given the reference to the previous and
next nodes.
Figure 1.3: Double linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 8 / 172
Linked List Types
A circular linked list is a variation of a linked list data structure where
the last node of the list points back to the first node, creating a
circular loop.
Unlike a traditional linked list where the last node points to NULL, in
a circular linked list, the last node’s pointer points back to the first
node.
The last node’s next pointer points to the first node, forming a
circular loop that connects the last node to the first node.
This circular structure enables seamless traversal from any node in
the list to any other node.
Figure 1.4: Circular singly linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 9 / 172
Linked List Types
A circular double linked list is a variation of the double linked list data
structure in which the last node points to the first node, creating a
circular loop, just like in a circular linked list.
However, in a circular double linked list, each node also has a
reference to the previous node, in addition to the reference to the
next node.
The last node’s next pointer points to the first node, and the first
node’s previous pointer points to the last node, forming a circular
loop with bidirectional connections.
Each node in a circular double linked list contains three fields:
previous pointer, data and next pointer.
Figure 1.5: Circular double linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 10 / 172
Linked List Types
A header linked list is a variant of a linked list data structure that
includes an additional ”header node” at the beginning of the list.
This header node does not store any data but serves as a dummy
node to simplify operations and improve code clarity.
In a header linked list, the header node typically contains pointers to
the first node of the linked list.
A header-linked list typically includes the following components: a
header node and data nodes.
Like a single linked list, each data node contains pointers to the next
node in the sequence.
Figure 1.6: Header linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 11 / 172
Linked List Types
A circular header linked list is a variation of a linked list data
structure that combines features of both circular linked lists and
header linked lists.
It includes a header node at the beginning of the list, which may
contain metadata or attributes about the list.
Like a circular linked list, the last node points back to the first node,
creating a circular loop.
The header node contains metadata about the list and pointers to the
first data node. Data nodes are the actual nodes of the list,
containing the data elements.
Figure 1.7: Circular header linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 12 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 13 / 172
Static Implementation
Another name for a single-linked list is a one-way list. Each node in a
single linked list has a data field and a single pointer field that points
to the node next to it in the list.
A single linked list can be represented in memory in two different
ways. Representations that are both static and dynamic.
Two arrays are maintained in a static representation of a single linked
list: one for links and one for data.
The complete linked list should fit inside the two parallel arrays that
are allotted, each of equal size.
Figure 2.1: Static representation of a single linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 14 / 172
Static Implementation
A linked list can be thought of as an array of nodes, with each node
containing a structure with two fields: info and next.
We can declare a linked list containing 50 nodes as follows:
An array index represents a pointer to a node.
In other words, a pointer is a number that refers to a specific element
in the array of nodes and ranges from 0 to SIZE − 1.
As an illustration, the array index 5 points to the node[5].
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 15 / 172
Static Implementation
The notations that we employ are
1 node(p) : pth
indexed node. (i.e., node[p])
2 info(p) : info field value of pth
node. (i.e., node[p].info)
3 next(p) : next field value of pth
node. (i.e., node[p].next)
4 NULL is symbolized by -1.
Figure 2.2: Static implementation of a single linked list
The array’s first element, node[5], is indicated by the array index of 5.
node[5].next has the value 10. node [10] is regarded as the array’s
second element.
node[10].next has the value 15, thus node [15] is regarded as the
array’s third element. Nodes [30] and [40] are the array’s fourth and
fifth items, respectively.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 16 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 17 / 172
Operations
Available List Initialization:
Create an array of nodes available for building a single linked list.
Figure 2.3: Array of 50 available nodes
Algorithm 1 Available List Initialization
1: procedure List-initialization(node, MAX)
2: for i ← 0 to MAX − 2 do
3: node[i].next := i + 1
4: end for
5: node[SIZE − 1].next := −1
6: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 18 / 172
Operations
Empty Node Selection:
A node can be acquired from the available list when it is required for
use in a linked list.
Index position of the node that was removed from the available list is
returned by the GetNode() function.
Algorithm 2 Empty Node Selection
1: procedure NodeSelection(avail)
2: if (avail = −1) then
3: write ”overflow”
4: return -1
5: end if
6: p := avail
7: avail := node[avail].next
8: return p
9: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 19 / 172
Static Implementation
Empty Node Selection:
Figure 2.4: List of available nodes
In the available list above, there are four nodes.
p stores avail value while node selection, i.e., we store 11 into p.
After removing node[11] from the available list, the avail must be
made to point to the next node[31] in the available list.
For this, we write avail = node[avail].next. Now, the first node[11] is
removed, with its index position available with p.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 20 / 172
Operations
Insert After a Given Node:
Insert a node next to the node with index p, i.e., node[p].
Fetch the first node from the available list and update the avail.
Algorithm 3 Insert After a Node
1: procedure Insert(p, avail, x)
2: if p = −1 then
3: write ”void insertion”
4: return -1
5: end if
6: q := NODESELECTION(avail)
7: node[q].info := x
8: node[q].next := node[p].next
9: node[p].next := q
10: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 21 / 172
Operations
Insert After a Given Node:
(a) Single linked list before insert operation
(b) Single-linked list after insert operation (p = 39, x = 55).
Figure 2.5: Insert after a given node
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 22 / 172
Operations
Deletion After a Given Node:
Delete node next to the node with index p, i.e., node[p].
Add the removed node to the available list and update the avail.
Algorithm 4 Insert After a Node
1: procedure Deletion(p)
2: q := node[p].next
3: x := node[q].info
4: node[q].info := ∞
5: node[p].next := node[q].next
6: node[q].next := −1
7: FREENODE(q, avail)
8: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 23 / 172
Operations
Insert After a Given Node:
(a) Single linked list before deletion
(b) Single linked list before FREENODE execution (p = 7, q = 13).
Figure 2.6: Deletion after a given node
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 24 / 172
Operations
Returning Node to Available List:
FREENODE function accepts the index position of a node and
returns that node to the available list.
Add the removed node at the beginning of the available list.
Algorithm 5 Add Node to Available List
1: procedure Freenode(q)
2: node[q].next := avail
3: avail := q
4: return avail
5: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 25 / 172
Operations
(a) Available list before FREENODE execution.
(b) Available list after FREENODE execution (q = 13).
(c) Single linked list after FREENODE execution (q = 13).
Figure 2.7: Single linked list after deletion.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 26 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 27 / 172
Dynamic Implementation
The number of elements in an array must be specified by the C
language during compilation.
This could result in memory space being wasted. Dynamic data
structures can be used to handle such circumstances.
Dynamic memory allocation management strategies enable the
allocation of extra memory or the release of unnecessary space during
runtime.
During program execution, the following memory management
functions can be used to allocate and free up memory:
1 malloc(): Allocate size of bytes
2 calloc(): Allocate an array of defined no. of elements of specified size.
3 realloc(): Resize the previous assigned space.
4 free(): Release space associated with a pointer.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 28 / 172
Dynamic Implementation
Single Block of Memory Allocation
A memory block can be allocated using the malloc() function. It
returns a pointer of type void and reserves a certain amount of
memory.
ptr = (cast-type*)malloc(byte-size);
The malloc() returns a pointer (of cast type) to an area of memory
with size byte-size.
y = (int*)malloc(100*sizeof(int));
In the above statement, 100 times the size of an integer variable of
space is reserved.
The pointer y is assigned the address of the first byte of the allocated
memory. The malloc function is initialized to garbage values if no
value given.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 29 / 172
Dynamic Implementation
Multi-Block of Memory Allocation
At runtime, memory space for storing derived data types is sought
after using the calloc() function.
The calloc() function is used to allocate multiple storage blocks.
ptr = (cast-type*)calloc(n, element-size);
Contiguous space for n blocks, each of element-size bytes, is allocated.
y = (int*)calloc(100, sizeof(int));
100 blocks of integer bytes of space are allocated, and pointer y is
assigned with the starting address.
Memory blocks allocated by the calloc function are always initialized
to zero.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 30 / 172
Dynamic Implementation
Resizing Allotted Memory
The realloc() function makes an effort to modify the size of a memory
block that was previously allocated. The new dimensions may be
greater or lesser.
Increases in block size result in memory being added to the end of the
block while the previous contents stay the same. The remaining
contents stay unaltered if the size is decreased.
ptr = (cast-type*)realloc(ptr, new-size);
y = (int*)realloc(y, sizeof(int)*50);
Realloc() will try to assign a new block of memory and duplicate the
contents of the previous block if the original block size cannot be
changed.
This will result in the return of a new pointer with a different address.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 31 / 172
Dynamic Implementation
Releasing Memory
Memory allocated with the help of the calloc and malloc routines is
not released on its own.
To release memory that has already been allotted, use the free()
function. It accepts a pointer as its parameter. For instance,
free(ptr);
ptr has to point to memory that was allocated using the malloc()
function.
This feature aids in preventing memory leaks, which might result in
system slowdown or crash.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 32 / 172
Dynamic Implementation
Utilizing a free pool of storage for a linked list involves a Memory
Manager program handling memory allocation requests by searching
for available blocks in a Memory Bank.
Each time a new node is required, the Memory Manager locates a
suitable memory block from the bank and assigns it to the node.
A Garbage Collector component is activated when nodes are no
longer in use, returning their memory blocks to the bank for reuse.
The Memory Bank serves as a list of available memory blocks for the
programmer to manage efficiently.
This approach streamlines memory usage, ensuring optimal allocation
and deallocation processes for linked list construction and
maintenance.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 33 / 172
Dynamic Implementation
One-way lists are another name for single linked lists. Pointers are
used to indicate the linear order of this node-based, linear collection
of data components.
Every node has two components, information and pointer to next
node fields. A linked list is made up of several structures that aren’t
always connected.
Every structure has a pointer to a structure that houses its successor
as well as one or more information fields.
A particular value known as the NULL pointer, or any invalid address,
is present in the pointer field of the final node.
A single linked list with five nodes is depicted in the accompanying
figure.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 34 / 172
Dynamic Implementation
Figure 2.8: Single linked list with 5 nodes
Node Structure
struct Node{
int info;
struct Node *next;
};
Create a start pointer:
struct Node *start = NULL;
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 35 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 36 / 172
Operations
Operations on a Single Linked List
1 Traverse a linked list
2 Insert at beginning
3 Insert at end
4 Insert at a specific location
5 Insert after a given node
6 Deletion from beginning
7 Deletion at end
8 Delete at a specific location
9 Delete after a given node
10 Merge two linked lists
11 Search in a linked list
12 Sort a linked list
13 Reverse a linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 37 / 172
Operations
Traverse a Linked List
Suppose we have a single linked list in memory and we wish to
process each node exactly once by traversing the list.
Starting at the first node and working our way down the list is our
goal.
Let us consider a pointer-type variable node, node→next, which
points to the node after it, and which points to the node that is
presently being processed.
Figure 2.9: Single linked list with 5 data nodes
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 38 / 172
Operations
Algorithm 6 Traverse a Linked List
1: procedure Traverse(start)
2: node := start ▷ initialize pointer variable node
3: while node ̸= NULL do
4: write node → info ▷ print info[node]
5: node := node → next ▷ Move to next node
6: end while
7: end procedure
Output: 11 22 33 44 55
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 39 / 172
Operations
Insert at Beginning
An element can be added to an already-existing linked list using the
insertion operation.
The list could be in any order. An ordered list’s information fields can
be arranged in either a decreasing or increasing order.
In an ordered list, we must compare the information fields of every
node till the end of the list in order to determine the proper location
for each element to be inserted.
Assume that a linked list that isn’t necessarily sorted is present in
memory. The simplest method is to add a node at the start.
Figure 2.10: Single linked list before insertion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 40 / 172
Operations
Algorithm 7 Insert at Beginning
1: procedure Insert Begin(start, x)
2: node := new Node ▷ Create a new node named as node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := start ▷ new node now points to first node
8: start := node
9: end procedure
Figure 2.11: Single linked list after insertion (x = 11)
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 41 / 172
Operations
Insert at End
We must iterate through the list and advance the pointer until the
last node is reached in order to insert a node at the end.
The new node is added as the final node, with an information field
value of 77, as shown in figure 2.13.
Figure 2.12: Single linked list before insertion
Figure 2.13: Single linked list after insertion (x = 77)
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 42 / 172
Operations
Algorithm 8 Insert at End
1: procedure Insert End(start, x)
2: node := new Node ▷ Create a new node named as node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := NULL
8: curr := start
9: while curr ̸= NULL do
10: prev := curr
11: curr := curr → next
12: end while
13: prev → next := node
14: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 43 / 172
Operations
Insert at a Specific Location
There are situations when placing things in a list at a specified
location is necessary.
Tracking the beginning node, current node, and prior node is required
in order to insert an element at a specific point.
This is accomplished by starting at the beginning of the list and
scanning our way up to the target node, where the new node is to be
inserted.
Third place, loc = 3, is where the new node with 88 in its information
field is inserted.
Figure 2.14: Single linked list before insertion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 44 / 172
Operations
Algorithm 9 Insert at a Specific Location
1: procedure Insert at Location(start, x)
2: node := new Node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := NULL
8: read loc
9: i := 1
10: curr := start
11: while curr ̸= NULL and i < loc do
12: prev := curr
13: curr := curr → next
14: i := i + 1
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 45 / 172
Operations
Algorithm 10 Insert at a Specific Location (Cont.)
15: end while
16: if curr = NULL then
17: write ”Position not found” and exit
18: end if
19: prev → next := node
20: node → next := curr
21: end procedure
Figure 2.15: Single linked list after insertion (loc = 3, x = 88)
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 46 / 172
Operations
Insert After a Given Node
To insert a node after a given node, we need to traverse the list and
move the pointer until we find that node.
Let it be node A. Then we create a new node, i.e., node n, and it is
inserted in between node A and the next node.
The new node whose information field contains 88 is inserted after
the given node, which is 33.
Figure 2.16 and 2.17 illustrate the linked list before and after
insertion, respectively.
Figure 2.16: Single linked list before insertion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 47 / 172
Operations
Algorithm 11 Insert After a Given Node
1: procedure Insert After Node(start, x)
2: node := new Node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := NULL
8: read item
9: curr := start
10: while curr ̸= NULL and curr → info ̸= item do
11: curr := curr → next
12: end while
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 48 / 172
Operations
Algorithm 12 Insert After a Given Node (Cont.)
13: if curr = NULL then
14: write ”item is not in the list” and exit
15: end if
16: node → next := curr → next
17: curr → next := node
18: end procedure
Figure 2.17: Single linked list after insertion (item = 33, x = 88)
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 49 / 172
Operations
Deletion from Beginning
It is not feasible to delete anything from a list that is empty; the
output should say ”Deletion is not possible”.
Assume that a linked list that isn’t necessarily sorted is present in
memory. Eliminating the initial node is the simplest method.
The list will be empty if there is only one node on it after deletion.
After being deleted, the pointer variable start should point to the
second node instead of the first.
The operating system gathers free space and creates a link to the
node that has space available right now.
Figure 2.18: Single linked list before deletion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 50 / 172
Operations
Algorithm 13 Deletion from Beginning
1: procedure Delete Begin(start)
2: temp := start ▷ temp points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: start := start → next ▷ start points to next node
7: free temp ▷ Free space associated with temp
8: end procedure
Figure 2.19: Single linked list after deletion.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 51 / 172
Operations
Deletion at End
Moving the pointer along the list until we reach the address of the
last node is necessary in order to remove it.
Keep the address of the preceding node in mind as you advance the
pointer.
Since the preceding node will be the last, enter NULL in its next
pointer field and release the memory space occupied by the last node.
Figure 2.20: Single linked list before deletion
Figure 2.21: Single linked list after deletion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 52 / 172
Operations
Algorithm 14 Deletion at End
1: procedure Delete End(start)
2: node := start ▷ node points to the first node
3: temp := start ▷ temp points to the first node
4: if start = NULL then
5: write ”UNDERFLOW” and exit
6: end if
7: while node → next ̸= NULL do
8: temp := node
9: node := node → next
10: end while
11: temp → next := NULL ▷ Place NULL in second last node
12: free node ▷ Free space associated with node
13: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 53 / 172
Operations
Delete at a Specific Location
Let’s say we wish to remove a node from a linked list at a particular
position. To get the node’s position, we must iterate through the
linked list.
Transfer the pointer to that specific node, modify the pointer (i.e.,
the pointer of this node’s predecessor will now point to this node’s
successor), and release the node.
Verify if the list is empty or not before performing the deletion. If so,
an ’UNDERFLOW’ message is displayed, and the program terminates.
Figure 2.22: Single linked list before deletion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 54 / 172
Operations
Algorithm 15 Delete at a Specific Location
1: procedure Delete at Location(start)
2: node := start ▷ node points to the first node
3: temp := start ▷ temp points to the first node
4: if start = NULL then
5: write ”UNDERFLOW” and exit
6: end if
7: i := 1
8: read loc
9: while node ̸= NULL and i < loc do
10: temp := node
11: node := node → next
12: i := i + 1
13: end while
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 55 / 172
Operations
Algorithm 16 Delete at a Specific Location (Cont.)
14: if node = NULL then
15: write ’Position not Found’ and exit
16: end if
17: temp → next := node → next ▷ Next node addr. in temp next
18: free node ▷ Free space associated with node
19: end procedure
Figure 2.23: Single linked list after deletion (loc = 3).
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 56 / 172
Operations
Delete After a Given Node
We must go through the list and move the pointer until we locate the
node we want to delete after a specific node.
Select node A. Node n is the next node that needs to be removed.
Once nodes A and B (next node of node n) are linked, delete node n.
If the list is empty, an ’UNDERFLOW’ message is displayed, and the
program terminates.
Figure 4.16 shows the node whose information field contains 44 is
deleted after the given node, which is 33.
Figure 2.24: Single linked list before deletion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 57 / 172
Operations
Algorithm 17 Delete After a Given Node
1: procedure Delete After Node(start)
2: node := start ▷ node points to the first node
3: temp := start ▷ temp points to the first node
4: if start = NULL then
5: write ”UNDERFLOW” and exit
6: end if
7: read item
8: while node ̸= NULL and item ̸= node → info do
9: temp := node
10: node := node → next
11: end while
12: if node = NULL then
13: write ’INVALID DELETION’ and exit
14: end if
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 58 / 172
Operations
Algorithm 18 Delete After a Given Node (Cont.)
15: temp → next := node → next ▷ Next node addr. in temp next
16: free node ▷ Free space associated with node
17: end procedure
Figure 2.25: Single linked list after deletion (item = 44).
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 59 / 172
Operations
Merge Two Linked Lists
Memory contains two linked lists. To combine two linked lists into
one bigger list, you must do this.
It is required to connect the final node of the first list to the initial
node of the second list in order to obtain the merged list.
(a) Single linked list 1
(b) Single linked list 2
Figure 2.26: Linked lists before merge operation.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 60 / 172
Operations
Algorithm 19 Merge Two Linked Lists
1: procedure Merge Lists(start := NULL, start1, start2)
2: node1 := start1
3: if start1 = NULL then
4: write ’Out of Memory Space’ and exit
5: end if
6: node := new Node
7: start := node
8: start → info := node1 → info
9: start → next := NULL
10: node1 := node1 → next
11: while node1 ̸= NULL do
12: curr := new Node
13: if curr = NULL then
14: write ’Out of Memory Space’ and exit
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 61 / 172
Operations
Algorithm 20 Merge Two Linked Lists (Cont.)
15: end if
16: curr → info := node1 → info
17: curr → next := NULL
18: node1 := node1 → next
19: node → next := curr
20: node := curr
21: end while
22: node2 := start2
23: while node2 ̸= NULL do
24: curr := new Node
25: if curr = NULL then
26: write ’Out of Memory Space’ and exit
27: end if
28: curr → info := node2 → info
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 62 / 172
Operations
Algorithm 21 Merge Two Linked Lists (Cont.)
29: curr → next := NULL
30: node2 := node2 → next
31: node → next := curr
32: node := curr
33: end while
34: end procedure
Figure 2.27: Linked list after merging linked lists 1 and 2.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 63 / 172
Operations
Search in a Linked List
Finding an item in a list is the definition of searching.
Assume we have the linked list shown in Figure 2.28 and we need to
look for one item, let’s say 44.
The item’s storage location (in this case, number 4) will be returned
if it is located in the list.
Else, ”Element not found.” will be displayed.
Algorithm 22 Search in a Linked List
1: procedure Search List(start)
2: node := start ▷ node points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: count := 1
7: read item
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 64 / 172
Operations
Algorithm 23 Search in a Linked List (Cont.)
8: while node ̸= NULL do
9: if item = node → info then
10: write ’Element Found at Location’, count and exit
11: end if
12: count := count + 1
13: node := node → next
14: end while
15: write ’Element not Found’
16: end procedure
Figure 2.28: Search an element 44.
Output: Element Found at Location 4
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 65 / 172
Operations
Sort a Linked List
Arranging the list elements in either ascending or descending order is
known as sorting. Figure 2.29 and 2.30 exhibits the linked list before
and after sorting respectively.
Figure 2.29: Linked list before sorting
Algorithm 24 Sort a Linked List
1: procedure Sort Nodes(start)
2: node := start ▷ node points to the first node
3: while node → next ̸= NULL do
4: ptr := node → next
5: while ptr ̸= NULL do
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 66 / 172
Operations
Algorithm 25 Sort a Linked List (Cont.)
6: if node → info > ptr → info then ▷ If TRUE, Swap
7: temp := node → info
8: node → info := ptr → info
9: ptr → info := temp
10: end if
11: ptr := ptr → next
12: end while
13: node := node → next
14: end while
15: end procedure
Figure 2.30: Linked list after sorting
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 67 / 172
Operations
Reverse a Linked List
Iterate through the list, reversing the direction of pointers between
nodes. Update the start pointer to point to the last node encountered
during traversal.
Figure 2.31: Original linked list
Algorithm 26 Reverse a Linked List
1: procedure Reverse List(start)
2: curr := start ▷ node points to the first node
3: if curr → next = NULL then ▷ Check for single node linked list
4: exit
5: end if
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 68 / 172
Operations
Algorithm 27 Reverse a Linked List (Cont.)
6: prev := curr → next
7: curr → next := NULL
8: while prev → next ̸= NULL do
9: ptr := prev → next
10: prev → next := curr
11: curr := prev
12: prev := ptr
13: end while
14: prev → next := curr
15: start := prev
16: end procedure
Figure 2.32: Reversed linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 69 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 70 / 172
Dynamic Implementation
There is only one way to go from the first node to any other node in
a single linked list, and that is left to right.
A single linked list is also known as a one-way list for this reason.
Conversely, a doubly linked list allows for bidirectional movement,
allowing for left-to-right or right-to-left movement.
Each node in a two-way list is a linear collection of data elements
called nodes that are split into three sections.
1 A data-containing information field.
2 The next field is a pointer field that holds the address of the node that
follows in the list.
3 The address of the node in the list that comes before it is stored in a
pointer field called pre.
Figure 3.1: Doubly linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 71 / 172
Dynamic Implementation
The pointers pre of the first node and next of the last node contain
the value NULL, i.e., they do not store the address of any other node.
This indicates the beginning and end of the list, respectively. The
advantage of a doubly linked list over a single-linked list is that
traversal is possible in both directions.
This makes it an ideal data structure for applications like databases
and word processors, in which moving in both directions is necessary.
Node Structure
struct dNode{
struct dNode *pre;
int info;
struct dNode *next; };
Create a start pointer:
struct dNode *start = NULL;
Create a last pointer:
struct dNode *last = NULL;
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 72 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 73 / 172
Operations
Operations on a Doubly Linked List
1 Traverse a linked list
2 Insert at beginning
3 Insert at end
4 Insert at a specific location
5 Insert after a given node
6 Deletion from beginning
7 Deletion at end
8 Delete at a specific location
9 Delete after a given node
10 Merge two linked lists
11 Search in a linked list
12 Sort a linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 74 / 172
Operations
Traverse a Linked List
Let’s say we have a doubly linked list in memory and we want to
process each node exactly once by traversing the list.
Our goal is to iterate through the list, beginning at the beginning and
ending at the conclusion.
Let curr be a pointer-type variable that points to the node that is
being processed at the moment. The next node is indicated by
curr → next.
Figure 3.2: Doubly linked list with 5 data nodes
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 75 / 172
Operations
Algorithm 28 Traverse a Linked List
1: procedure Traverse(start)
2: curr := start ▷ initialize pointer variable node
3: while curr ̸= NULL do
4: write curr → info ▷ print info[curr]
5: curr := curr → next ▷ Move to next node
6: end while
7: end procedure
Output: 11 22 33 44 55
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 76 / 172
Operations
Insert at Beginning
Figure 3.3a and 3.3b show the double-linked list both before and after
the insertion. During insertion, the previous and next pointer fields
should be changed.
(a) Doubly linked list before insertion.
(b) Doubly linked list after insertion (x = 66).
Figure 3.3: Insert at the beginning.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 77 / 172
Operations
Algorithm 29 Insert at Beginning
1: procedure Insert Begin(start, x)
2: node := new Node ▷ Create a new node named as node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := start ▷ New node now points to first node
8: node → pre := NULL
9: start → pre := node ▷ First node’s pre points to new node
10: start := node
11: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 78 / 172
Operations
Insert at End
Skip every node from the start to the finish in order to add a node at
the end. Once It is possible to build a link if we know the address of
the final node.
The previous and next pointer fields should be changed appropriately.
(a) Doubly linked list before insertion.
(b) Doubly linked list after insertion (x = 55).
Figure 3.4: Insert at the end.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 79 / 172
Operations
Algorithm 30 Insert at End
1: procedure Insert End(start, x)
2: node := new Node ▷ Create a new node named as node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := NULL
8: curr := start
9: while curr ̸= NULL do
10: prev := curr
11: curr := curr → next
12: end while
13: prev → next := node
14: node → pre := prev
15: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 80 / 172
Operations
Insert at Specific Location
A node can be added at the desired location if its value and location
are known. Figures 3.5a and 3.5b illustrate the list before and after
insertion, respectively.
(a) Doubly linked list before insertion.
(b) Doubly linked list after insertion (loc = 3, x = 66).
Figure 3.5: Insert at a specific location.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 81 / 172
Operations
Algorithm 31 Insert at a Specific Location
1: procedure Insert at Location(start, x)
2: node := new Node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := NULL
8: read loc
9: i := 1
10: curr := start
11: while curr ̸= NULL and i < loc do
12: prev := curr
13: curr := curr → next
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 82 / 172
Operations
Algorithm 32 Insert at a Specific Location (Cont.)
14: i := i + 1
15: end while
16: if curr = NULL then
17: write ”Position not found” and exit
18: end if
19: prev → next := node
20: node → pre := prev
21: node → next := curr
22: curr → pre := node
23: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 83 / 172
Operations
Insert After a Given Node
To insert a node after a given node, an appropriate position has to be
found after advancing the node pointer. Then a link can be
established. Figures 3.6a and 3.6b illustrate the list before and after
insertion, respectively.
(a) Doubly linked list before insertion.
(b) Doubly linked list after insertion (item = 22, x = 66).
Figure 3.6: Insert after a given node.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 84 / 172
Operations
Algorithm 33 Insert After a Given Node
1: procedure Insert After Node(start, x)
2: node := new Node
3: if node = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node → info := x ▷ Copies new data into new node
7: node → next := NULL
8: read item
9: curr := start
10: while curr ̸= NULL and curr → info ̸= item do
11: curr := curr → next
12: end while
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 85 / 172
Operations
Algorithm 34 Insert After a Given Node (Cont.)
13: if curr = NULL then
14: write ”item is not in the list” and exit
15: end if
16: temp := curr → next
17: node → next := temp
18: temp → pre := node
19: curr → next := node
20: node → pre := curr
21: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 86 / 172
Operations
Deletion from Beginning
Figures 3.7a and 3.7b illustrate the list before and after deletion,
respectively. The node pointer should be adjusted accordingly after
deletion.
(a) Doubly linked list before deletion.
(b) Doubly linked list after deletion.
Figure 3.7: Deletion from beginning.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 87 / 172
Operations
Algorithm 35 Deletion from Beginning
1: procedure Delete Begin(start)
2: temp := start ▷ temp points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: start := start → next ▷ start points to next node
7: start → pre := NULL
8: free temp ▷ Free space associated with temp
9: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 88 / 172
Operations
Deletion at End
Figures 3.7a and 3.7b illustrate the list before and after deletion,
respectively. The node pointer should be adjusted accordingly after
deletion.
(a) Doubly linked list before deletion.
(b) Doubly linked list after deletion.
Figure 3.8: Deletion at end.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 89 / 172
Operations
Algorithm 36 Deletion at End
1: procedure Delete End(start)
2: node := start ▷ node points to the first node
3: temp := start ▷ temp points to the first node
4: if start = NULL then
5: write ”UNDERFLOW” and exit
6: end if
7: while node → next ̸= NULL do
8: temp := node
9: node := node → next
10: end while
11: temp → next := NULL ▷ Place NULL in second last node
12: free node ▷ Free space associated with node
13: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 90 / 172
Operations
Delete at a Specific Location
Illustrated in Figures 3.9a and 3.9b are the list before and after
deletion. The previous and next pointer fields must be adjusted to
maintain the link.
(a) Doubly linked list before deletion.
(b) Doubly linked list after deletion (loc = 3).
Figure 3.9: Delete at a specific location.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 91 / 172
Operations
Algorithm 37 Delete at a Specific Location
1: procedure Delete at Location(start)
2: node := start ▷ node points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: i := 1
7: read loc
8: while node → next ̸= NULL do
9: node := node → next
10: c := c+1
11: end while
12: if loc = 1 and loc = c then
13: start := NULL and exit
14: else
15: start := node → next
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 92 / 172
Operations
Algorithm 38 Delete at a Specific Location (Cont.)
16: start → pre := NULL and go to 29
17: end if
18: while node ̸= NULL and i < loc do
19: prev := node
20: node := node → next
21: i := i + 1
22: end while
23: if node = NULL then
24: write ’Position not Found’ and exit
25: end if
26: temp := node → next ▷ temp points to Next node
27: prev → next := temp ▷ temp addr. in prev next
28: temp → pre := prev
29: free node ▷ Free space associated with node
30: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 93 / 172
Operations
Delete After a Given Node
Figure 3.10a and 3.10b illustrate the list before and after deletion,
respectively. The previous and next pointer fields should be adjusted
accordingly to maintain the link.
(a) Doubly linked list before deletion.
(b) Doubly linked list after deletion (item = 22).
Figure 3.10: Delete after a given node.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 94 / 172
Operations
Algorithm 39 Delete After a Given Node
1: procedure Delete After Node(start)
2: node := start ▷ node points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: read item
7: while node ̸= NULL and item ̸= node → info do
8: prev := node
9: node := node → next
10: end while
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 95 / 172
Operations
Algorithm 40 Delete After a Given Node (Cont.)
11: if node = NULL and node → next = NULL then
12: write ’INVALID DELETION’ and exit
13: end if
14: prev := node
15: node := node → next
16: temp := node → next ▷ temp points to the Next node
17: prev → next := temp ▷ temp addr. in prev next
18: temp → pre := prev
19: free node ▷ Free space associated with node
20: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 96 / 172
Operations
Merge Two Linked Lists
Memory contains two linked lists. To combine two linked lists into
one bigger list, you must do this.
It is required to connect the final node of the first list to the initial
node of the second list in order to obtain the merged list.
(a) Single linked list 1
(b) Single linked list 2
Figure 3.11: Linked lists before merge operation.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 97 / 172
Operations
Algorithm 41 Merge Two Linked Lists
1: procedure Merge Lists(start := NULL, start1, start2)
2: node1 := start1
3: if start1 = NULL then
4: write ’Out of Memory Space’ and exit
5: end if
6: node := new Node
7: start := node
8: start → info := node1 → info
9: start → next := NULL
10: start → pre := NULL
11: node1 := node1 → next
12: while node1 ̸= NULL do
13: curr := new Node
14: if curr = NULL then
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 98 / 172
Operations
Algorithm 42 Merge Two Linked Lists (Cont.)
15: write ’Out of Memory Space’ and exit
16: end if
17: curr → info := node1 → info
18: curr → next := NULL
19: curr → pre := node
20: node → next := curr
21: node1 := node1 → next
22: node := curr
23: end while
24: node2 := start2
25: while node2 ̸= NULL do
26: curr := new Node
27: if curr = NULL then
28: write ’Out of Memory Space’ and exit
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 99 / 172
Operations
Algorithm 43 Merge Two Linked Lists (Cont.)
29: end if
30: curr → info := node2 → info
31: curr → next := NULL
32: curr → pre := node
33: node → next := curr
34: node2 := node2 → next
35: node := curr
36: end while
37: end procedure
Figure 3.12: Linked list after merging linked lists 1 and 2.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 100 / 172
Operations
Search in a Linked List
Finding an item in a list is the definition of searching.
Assume we have the linked list shown in Figure 3.13 and we need to
look for one item, let’s say 44.
The item’s storage location (in this case, number 4) will be returned
if it is located in the list.
Else, ”Element not found.” will be displayed.
Algorithm 44 Search in a Linked List
1: procedure Search List(start)
2: node := start ▷ node points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: count := 1
7: read item
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 101 / 172
Operations
Algorithm 45 Search in a Linked List (Cont.)
8: while node ̸= NULL do
9: if item = node → info then
10: write ’Element Found at Location’, count and exit
11: end if
12: count := count + 1
13: node := node → next
14: end while
15: write ’Element not Found’
16: end procedure
Figure 3.13: Search an element 44.
Output: Element Found at Location 4
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 102 / 172
Operations
Sort a Linked List
Arranging the list elements in either ascending or descending order is
known as sorting. Figure 3.14 and 3.15 exhibits the linked list before
and after sorting respectively.
Figure 3.14: Linked list before sorting
Algorithm 46 Sort a Linked List
1: procedure Sort Nodes(start)
2: node := start ▷ node points to the first node
3: while node → next ̸= NULL do
4: ptr := node → next
5: while ptr ̸= NULL do
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 103 / 172
Operations
Algorithm 47 Sort a Linked List (Cont.)
6: if node → info > ptr → info then ▷ If TRUE, Swap
7: temp := node → info
8: node → info := ptr → info
9: ptr → info := temp
10: end if
11: ptr := ptr → next
12: end while
13: node := node → next
14: end while
15: end procedure
Figure 3.15: Linked list after sorting
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 104 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 105 / 172
Dynamic Implementation
A circular linked list is one in which every node is connected to every
other node to create a circle.
The start and end nodes in a circular linked list are connected to one
another to form a circle. The end does not contain a NULL.
We traverse a circular singly linked list until we reach the same node
where we started. Operating systems primarily use circular linked lists
for task maintenance.
Each node in a circular linked list is split into two sections.
1 A data-containing information field.
2 The next field is a pointer field that holds the address of the node that
follows in the list.
Figure 4.1: Circular singly linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 106 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 107 / 172
Operations
Operations on a Circular Singly Linked List
1 Traverse a linked list
2 Insert at beginning
3 Insert at end
4 Insert at a specific location
5 Insert after a given node
6 Deletion from beginning
7 Deletion at end
8 Delete at a specific location
9 Delete after a given node
10 Merge two linked lists
11 Search in a linked list
12 Sort a linked list
13 Reverse a linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 108 / 172
Operations
Traverse a Circular Linked List
A circular singly linked list traversal begins from any node and
continues until the traversal reaches the starting point again.
Traversal involves visiting each node in the list, starting from the first
node, and moving to the next node until the first node is encountered
again.
Traversing a circular singly linked list ensures that every element in
the list is visited once, maintaining the circular structure without
getting stuck in an infinite loop.
Figure 4.2: Circular singly linked list with 5 data nodes
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 109 / 172
Operations
Algorithm 48 Traverse a Circular Singly Linked List
1: procedure Traverse(start)
2: node := start ▷ initialize pointer variable node
3: write node → info ▷ print info[node]
4: node := node → next ▷ Move to next node
5: while node ̸= start do
6: write node → info ▷ print info[curr]
7: node := node → next ▷ Move to next node
8: end while
9: end procedure
Output: 11 22 33 44 55
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 110 / 172
Operations
Insert at Beginning
Figure 4.3a and 4.3b show the circular singly linked list both before
and after the insertion. When inserting a node, the start pointer and
next pointer of the end node should be updated.
(a) Circular singly linked list before insertion.
(b) Circular singly linked list after insertion (x = 66).
Figure 4.3: Insert at the beginning.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 111 / 172
Operations
Algorithm 49 Insert at Beginning
1: procedure Insert Begin(start, x)
2: node := start ▷ initialize pointer variable node
3: node1 := new Node ▷ Create a new node named as node
4: if node1 = NULL then
5: write ”Out of memory space” and exit
6: end if
7: node1 → info := x ▷ Copies new data into new node
8: node1 → next := start ▷ New node now points to first node
9: while node → next ̸= start do
10: node := node → next
11: end while
12: node → next := node1
13: start := node1
14: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 112 / 172
Operations
Insert at End
Adding a node at the end of a circular singly linked list involves
traversing the list to reach the last node.
If the list is empty, the new node becomes the first node, and its next
pointer is set to itself, forming a circle.
For non-empty lists, the new node is appended after the last node,
and its next pointer is connected back to the start to maintain the
circular structure.
The process ensures that the new node becomes the new last node in
the circular list.
Figure 4.4: Circular single linked list before insertion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 113 / 172
Operations
Algorithm 50 Insert at End
1: procedure Insert End(start, x)
2: node1 := new Node ▷ Create a new node named as node
3: if node1 = NULL then
4: write ”Out of memory space” and exit
5: end if
6: node1 → info := x ▷ Copies new data into new node
7: node := start
8: if node = NULL then
9: node1 → next := node1
10: start := node1 and exit
11: end if
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 114 / 172
Operations
Algorithm 51 Insert at End (Cont.)
12: while node → next ̸= start do
13: node := node → next
14: end while
15: node → next := node1
16: node1 → next := start
17: end procedure
Figure 4.5: Circular single linked list after insertion (x = 66).
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 115 / 172
Operations
Insert at a Specific Location
Inserting a node at a specific location in a circular singly linked list
involves updating pointers to maintain the circular structure.
Determine the insertion point by traversing the list until the desired
location is reached.
Adjust pointers to link the new node to the list while preserving the
circular nature of the structure.
Special cases include inserting at the beginning (changing the start)
and end (updating the last node’s pointer).
Figure 4.6: Circular single linked list before insertion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 116 / 172
Operations
Algorithm 52 Insert at a Specific Location
1: procedure Insert at Location(start, x)
2: curr := start
3: node1 := new Node ▷ Create a new node named as node
4: if node1 = NULL then
5: write ”Out of memory space” and exit
6: end if
7: node1 → info := x ▷ Copies new data in to new node
8: node1 → next := NULL
9: read loc
10: if curr = NULL and loc > 1 then
11: write ”position not found” and exit
12: end if
13: count := 0
14: while curr → next ̸= start do
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 117 / 172
Operations
Algorithm 53 Insert at a Specific Location (Cont.)
15: count := count + 1
16: curr := curr → next
17: end while
18: count := count + 1
19: if count + 1 < loc then
20: write ”position not found” and exit
21: else if loc = 1 then
22: INSERT BEGIN(start, x)
23: else if count + 1 = loc then
24: INSERT END(start, x)
25: else
26: i := 1
27: curr := start
28: while i < loc do
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 118 / 172
Operations
Algorithm 54 Insert at a Specific Location (Cont.)
29: prev := curr
30: curr := curr → next
31: i := i + 1
32: end while
33: prev → next := node1
34: node1 → next := curr
35: end if
36: end procedure
Figure 4.7: Circular single linked list after insertion (loc = 3, x = 66)
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 119 / 172
Operations
Insert After a Given Node
To insert after a specific node in a circular linked list, the pointer of
the new node must be adjusted to point to the node after the given
node.
The insertion operation involves updating pointers to maintain the
circular structure while inserting the new node in the correct position.
Insertion after a given node in a circular linked list may require special
handling for inserting at the beginning or end of the list.
Figure 4.8 and 4.9 illustrate the circular linked list before and after
insertion, respectively.
Figure 4.8: Circular single linked list before insertion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 120 / 172
Operations
Algorithm 55 Insert After a Given Node
1: procedure Insert After Node(start, x)
2: curr := start
3: if curr = NULL then
4: write ”item is not in the list” and exit
5: end if
6: node1 := new Node ▷ Create a new node named as node
7: if node1 = NULL then
8: write ”Out of memory space” and exit
9: end if
10: node1 → info := x ▷ Copies new data into new node
11: node1 → next := NULL
12: read item
13: count := 1
14: while curr → next ̸= start do
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 121 / 172
Operations
Algorithm 56 Insert After a Given Node (Cont.)
15: count := count + 1
16: curr := curr → next
17: end while
18: i := 1
19: curr := start
20: while i ≤ count and item ̸= curr → info do
21: i := i + 1
22: curr := curr → next
23: end while
24: if i = count then
25: INSERT END(start, x)
26: else if i > count then
27: write ”Item is not in the list” and exit
28: else
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 122 / 172
Operations
Algorithm 57 Insert After a Given Node (Cont.)
29: node1 → next := curr → next
30: curr → next := node1
31: end if
32: end procedure
Figure 4.9: Circular single linked list after insertion (item = 22, x = 66)
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 123 / 172
Operations
Deletion from Beginning
Figures 4.10a and 4.10b illustrate the circular list before and after
deletion, respectively.
(a) Circular single linked list before deletion.
(b) Circular single linked list after deletion.
Figure 4.10: Deletion from beginning.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 124 / 172
Operations
Algorithm 58 Deletion from Beginning
1: procedure Delete Begin(start)
2: temp := start ▷ temp points to the first node
3: node := start ▷ node points to the first node
4: if start = NULL then
5: write ”UNDERFLOW” and exit
6: else if start → next = start then
7: start := NULL and exit
8: else
9: while node → next ̸= start do
10: node := node → next
11: end while
12: start := start → next ▷ start points to next node
13: node → next := start
14: free temp ▷ Free space associated with temp
15: end if
16: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 125 / 172
Operations
Deletion at End
Deleting at the end of a circular list involves updating the pointers of
the last node and its predecessor.
Deletion at the end of a circular list involves reconnecting the second
last node to the first node.
Figure 4.11: Circular single linked list before deletion
Figure 4.12: Circular single linked list after deletion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 126 / 172
Operations
Algorithm 59 Deletion at End
1: procedure Delete End(start)
2: node := start ▷ node points to the first node
3: temp := start ▷ temp points to the first node
4: if start = NULL then
5: write ”UNDERFLOW” and exit
6: else if start → next = start then
7: start := NULL and exit
8: else
9: while node → next ̸= start do
10: temp := node
11: node := node → next
12: end while
13: temp → next := start ▷ Place 1st node addr. in next of temp
14: free node ▷ Free space associated with node
15: end if
16: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 127 / 172
Operations
Delete at a Specific Location
Deleting a node at a specific location in a circular linked list involves
reassigning pointers to maintain integrity.
Identify the target node to delete by traversing the list until reaching
the desired position. Adjust the pointers of neighboring nodes to
bypass the node being deleted, effectively removing it from the
sequence.
Ensure proper handling of edge cases, such as deletion at the start or
last of the circular linked list, to maintain consistency.
Figure 4.13: Circular single linked list before deletion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 128 / 172
Operations
Algorithm 60 Delete at a Specific Location
1: procedure Delete at Location(start)
2: node := start ▷ node points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: read loc
7: count := 1
8: while node → next ̸= start do
9: count := count + 1
10: node := node → next
11: end while
12: if loc = 1 then
13: DELETE BEGIN(start)
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 129 / 172
Operations
Algorithm 61 Delete at a Specific Location (Cont.)
14: else if loc = count then
15: DELETE END(start)
16: else if loc > count then
17: write ”Position is not found” and exit
18: else
19: node := start
20: i := 1
21: while i < loc do
22: temp := node
23: node := node → next
24: i := i + 1
25: end while
26: temp → next := node → next ▷ Next node addr. in temp next
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 130 / 172
Operations
Algorithm 62 Delete at a Specific Location (Cont.)
27: free node ▷ Free space associated with node
28: end if
29: end procedure
Figure 4.14: Circular single linked list after deletion (loc = 3).
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 131 / 172
Operations
Delete After a Given Node
Deleting a node after a given node in a circular linked list involves
updating the pointers to bypass the node to be deleted.
To delete a node after a given node, we need to traverse the list until
we find the given node.
Once the given node is located, we adjust the pointers to skip the
next node, effectively removing it from the list.
Care must be taken to handle edge cases such as when the given
node is the last node or when the list has only one node.
Figure 4.15: Circular single linked list before deletion
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 132 / 172
Operations
Algorithm 63 Delete After a Given Node
1: procedure Delete After Node(start)
2: node := start ▷ node points to the first node
3: temp := start ▷ temp points to the first node
4: if start = NULL then
5: write ”UNDERFLOW” and exit
6: end if
7: read item
8: while node → next ̸= start and item ̸= node → info do
9: temp := node
10: node := node → next
11: end while
12: if node → next = start then
13: DELETE END(start) and exit
14: end if
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 133 / 172
Operations
Algorithm 64 Delete After a Given Node (Cont.)
15: temp := node
16: node := node → next
17: temp → next := node → next ▷ Next node addr. in temp next
18: free node ▷ Free space associated with node
19: end procedure
Figure 4.16: Circular single linked list after deletion (item = 22).
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 134 / 172
Operations
Search in a Linked List
Finding an item in a circular list is the definition of searching.
Assume we have the linked list shown in Figure 4.17 and we need to
look for one item, let’s say 44.
The item’s storage location (in this case, number 4) will be returned
if it is located in the list.
Else, ”Element not found.” will be displayed.
Algorithm 65 Search in a Linked List
1: procedure Search List(start)
2: node := start ▷ node points to the first node
3: if start = NULL then
4: write ”UNDERFLOW” and exit
5: end if
6: count := 1
7: read item
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 135 / 172
Operations
Algorithm 66 Search in a Linked List (Cont.)
8: while node → next ̸= start do
9: if item = node → info then
10: write ’Element Found at Location’, count and exit
11: end if
12: count := count + 1
13: node := node → next
14: end while
15: write ’Element not Found’
16: end procedure
Figure 4.17: Search an element 44.
Output: Element Found at Location 4
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 136 / 172
Operations
Sort a Linked List
Arranging the circular list elements in either ascending or descending
order is known as sorting. Figure 4.18 and 4.19 exhibits the linked list
before and after sorting respectively.
Figure 4.18: Circular single linked list before sorting
Algorithm 67 Sort a Linked List
1: procedure Sort Nodes(start)
2: node := start ▷ node points to the first node
3: while node → next ̸= start do
4: ptr := node → next
5: while ptr ̸= start do
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 137 / 172
Operations
Algorithm 68 Sort a Linked List (Cont.)
6: if node → info > ptr → info then ▷ If TRUE, Swap
7: temp := node → info
8: node → info := ptr → info
9: ptr → info := temp
10: end if
11: ptr := ptr → next
12: end while
13: node := node → next
14: end while
15: end procedure
Figure 4.19: Circular single linked list after sorting
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 138 / 172
Operations
Reverse a Linked List
Iterate through the list, reversing the direction of pointers between
nodes. Update the start pointer to point to the last node encountered
during traversal.
Figure 4.20: Original linked list
Algorithm 69 Reverse a Linked List
1: procedure Reverse List(start)
2: curr := start ▷ curr points to the first node
3: temp := start ▷ temp points to the first node
4: if curr → next = start then ▷ Check for single node linked list
5: exit
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 139 / 172
Operations
Algorithm 70 Reverse a Linked List (Cont.)
6: end if
7: prev := curr → next
8: while prev → next ̸= start do
9: ptr := prev → next
10: prev → next := curr
11: curr := prev
12: prev := ptr
13: end while
14: prev → next := curr
15: start := prev
16: temp → next := start
17: end procedure
Figure 4.21: Reversed linked list
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 140 / 172
Operations
Merge Two Linked Lists
Circular linked lists are merged by identifying the tail node of the first
list and pointing it to the first node of the second list, and vice versa,
creating a continuous chain.
(a) Circular single linked list 1
(b) Circular single linked list 2
Figure 4.22: Circular single linked lists before merge operation.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 141 / 172
Operations
Algorithm 71 Merge Two Linked Lists
1: procedure Merge Lists(start := NULL, start1, start2)
2: node1 := start1
3: if start1 = NULL then
4: write ’Out of Memory Space’ and exit
5: end if
6: node := new Node
7: start := node
8: start → info := node1 → info
9: start → next := NULL
10: node1 := node1 → next
11: while node1 ̸= start1 do
12: curr := new Node
13: if curr = NULL then
14: write ’Out of Memory Space’ and exit
15: end if
16: curr → info := node1 → info
17: curr → next := NULL
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 142 / 172
Operations
Algorithm 72 Merge Two Linked Lists (Cont.)
18: node1 := node1 → next
19: node → next := curr
20: node := curr
21: end while
22: node2 := start2
23: curr := new Node
24: if curr = NULL then
25: write ’Out of Memory Space’ and exit
26: end if
27: curr → info := node2 → info
28: curr → next := NULL
29: node2 := node2 → next
30: node → next := curr
31: node := curr
32: while node2 ̸= start2 do
33: curr := new Node
34: if curr = NULL then
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 143 / 172
Operations
Algorithm 73 Merge Two Linked Lists (Cont.)
35: write ’Out of Memory Space’ and exit
36: end if
37: curr → info := node2 → info
38: curr → next := NULL
39: node2 := node2 → next
40: node → next := curr
41: node := curr
42: end while
43: node → next := start
44: end procedure
Figure 4.23: Circular linked list after merging circular linked lists 1 and 2.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 144 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 145 / 172
Polynomial Representation
A single variable polynomial equation can be generalized as follow.
f (x) =
n
X
i=0
ai xi
(1)
An example of a single variable polynomial equation is 2x5+4x3-9.
The order of the polynomial equation is 5.
Addition, subtraction, multiplication, division, differentiation,
integration, etc. are the most common operations performed on
polynomial equations.
Hand-calculating polynomial operations can take a lot of time. E.g.,
d(5x7 + 9x5 − 3x4 + 13x2 − 4)
dx
= 35x6
+ 45x4
− 12x3
+ 26x (2)
Polynomial ADTs can be implemented in two different ways. Arrays
and Linked lists.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 146 / 172
Polynomial Representation
Array Implementation:
f1(x) = 3x3 + 8x2 + 5x − 9, f2(x) = −4x5 + 7x2 + 10,
f3(x) = −10x14 + 8x2 − 12x
(3)
(a) f1(x) (b) f2(x)
(c) f3(x)
Figure 5.1: Representation of polynomial equations using arrays.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 147 / 172
Polynomial Representation
Array Implementation:
Indices and their values represent exponents and coefficients. This
representation of a sparse polynomial equations causes a waste of
memory space as shown in Figure 5.1c
Advantages
Good for non-sparse polynomial equations.
Easy for storage and retrieval.
Disadvantages
Array size allotted during compilation time causes the storage of
polynomial equations up to a certain order.
Wastage of space and runtime in the case of the array
implementation of sparse polynomial equations.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 148 / 172
Polynomial Representation
Linked List Implementation:
f1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3
f2(x) = 4x6 + 10x4 + 12x + 8
(4)
(a) f1(x)
(b) f2(x)
Figure 5.2: Representation of polynomial equations using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 149 / 172
Polynomial Representation
Linked List Implementation:
A node represents a term, containing fields for coefficient, exponent,
and a pointer to the next term in the polynomial.
If the coefficients of the polynomial are floating-point numbers, the
data type used in the term struct should be adjusted accordingly.
Advantages
Make it easy to maintain and save space (you don’t need to worry
about sparse polynomials).
Declaring nodes just when necessary, and there is no requirement to
set a list size.
Disadvantages
Not able to traverse the list in reverse.
Not able to go from the end of the list to the beginning.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 150 / 172
Polynomial Representation
Linked List Implementation:
Node Structure
f (x) = anxn
+ an−1xn−1
+ an−2xn−2
+ ... + a2x2
+ a1x + a0 (5)
struct PloyNode{
int coef;
int exp;
struct PolyNode *next;
};
typedef struct PolyNode *polynode;
If the coefficients of the polynomial are floating-point numbers, the
data type used in the struct PolyNode should be adjusted accordingly.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 151 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 152 / 172
Polynomial Addition
Using a linked list, add the two polynomial equations, f1 and f2, and store
the result in the f3 linked list. In order to accomplish this, we must break
down the process cases:
Case 1: exponent of node from f1 = exponent of node from f2
Create a node in f3 with the same exponent and with the sum of the
coefficients of f1 and f2 nodes.
Case 2: exponent of node from f1 > exponent of node from f2
Copy node of f1 to end of f3.
Case 3: exponent of node from f1 < exponent of node from f2
Copy node of f2 to end of f3
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 153 / 172
Polynomial Addition
f1(x) = 3x8
+ 14x5
+ 7 (6)
(a) f1(x)
f2(x) = 6x8
− 9x3
+ 5x2
(7)
(b) f2(x)
Figure 5.3: Representation of polynomial equations using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 154 / 172
Polynomial Addition
Step - 1
(a) Input polynomial equations (a → exp = 8, b → exp = 8).
(b) Addition as per case 1 (d → coef = 9, d → exp = 8).
Figure 5.4: Polynomial addition using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 155 / 172
Polynomial Addition
Step - 2
(a) Input polynomial equations (a → exp = 5, b → exp = 3)
(b) Next term as per case 2 (d → coef = 14, d → exp = 5).
Figure 5.5: Polynomial addition using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 156 / 172
Polynomial Addition
Step - 3
(a) Input polynomial equations (a → exp = 0, b → exp = 3)
(b) Next term as per case 3 (d → coef = -9, d → exp = 3).
Figure 5.6: Polynomial addition using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 157 / 172
Polynomial Addition
Step - 4
(a) Input polynomial equations (a → exp = 0, b → exp = 2)
(b) Next term based on case 3 (d → coef = 5, d → exp = 2).
Figure 5.7: Polynomial addition using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 158 / 172
Polynomial Addition
Step - 5
(a) Input polynomial equations (a → exp = 0, b = NULL)
(b) Remaining term in f1 added at the end of f3.
Figure 5.8: Polynomial addition using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 159 / 172
Polynomial Addition
Step - 5
(a) Input polynomial equations (a → exp = 0, b = NULL)
(b) Remaining term in f1 added at the end of f3.
Figure 5.9: Polynomial addition using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 160 / 172
Polynomial Addition
Algorithm 74 Addition of Polynomials
1: procedure Polynomial Addition(f 1, f 2)
2: pptr := f 1
3: qptr := f 2
4: f 3 := new PolyNode ▷ Create a header node
5: f 3 → next := NULL
6: rptr := f 3
7: while pptr ̸= NULL and qptr ̸= NULL do
8: if pptr → exp = qptr → exp then
9: rptr := ADDITION(pptr, qptr, rptr)
10: pptr := pptr → next
11: qptr := qptr → next
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 161 / 172
Polynomial Addition
Algorithm 75 Addition of Polynomials (Cont.)
12: else if pptr → exp > qptr → exp then
13: rptr := ADDITION(pptr, NULL, rptr)
14: pptr := pptr → next
15: else if pptr → exp < qptr → exp then
16: rptr := ADDITION(NULL, qptr, rptr)
17: qptr := qptr → next
18: end if
19: end while
20: while pptr ̸= NULL do
21: rptr := ADDITION(pptr, NULL, rptr)
22: pptr := pptr → next
23: end while
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 162 / 172
Polynomial Addition
Algorithm 76 Addition of Polynomials (Cont.)
24: while qptr ̸= NULL do
25: rptr := ADDITION(NULL, qptr, rptr)
26: qptr := qptr → next
27: end while
28: temp := f 3
29: f 3 := f 3 → next
30: free temp
31: return f 3
32: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 163 / 172
Polynomial Addition
Algorithm 77 Addition Function
1: procedure Addition(pptr, qptr, rptr)
2: if pptr ̸= NULL and qptr ̸= NULL then
3: node := new PolyNode
4: rptr → next := node
5: rptr := node
6: rptr → coef := pptr → coef + qptr → coef
7: rptr → exp := pptr → exp
8: rptr → next := NULL
9: return rptr
10: else if pptr ̸= NULL then
11: node := new PolyNode
12: rptr → next := node
13: rptr := node
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 164 / 172
Polynomial Addition
Algorithm 78 Addition Function (Cont.)
14: rptr → coef := pptr → coef
15: rptr → exp := pptr → exp
16: rptr → next := NULL
17: return rptr
18: else if qptr ̸= NULL then
19: node := new PolyNode
20: rptr → next := node
21: rptr := node
22: rptr → coef := qptr → coef
23: rptr → exp := qptr → exp
24: rptr → next := NULL
25: return rptr
26: end if
27: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 165 / 172
Outline
1 Linked List
Introduction
Linked List Types
2 Single Linked List
Static Implementation
Operations
Dynamic Implementation
Operations
3 Double Linked List
Dynamic Implementation
Operations
4 Circular Singly Linked List
Dynamic Implementation
Operations
5 Applications
Polynomial Representation
Polynomial Addition
Polynomial Multiplication
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 166 / 172
Polynomial Multiplication
Let n and m be the number of terms in the two polynomials. A
polynomial with nxm terms will be produced by this technique.
For instance, we can obtain the following linked list after multiplying
2 − 4x + 5x2 and 1 + 2x − 3x3:
(a) Linked list with all terms.
(b) Final multiplication result.
Figure 5.10: Polynomial multiplication using linked list.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 167 / 172
Polynomial Multiplication
Algorithm 79 Multiplication of Polynomials
1: procedure Poly Mult(start1, start2)
2: start := NULL
3: tail := NULL
4: p1 := start1
5: while p1 ̸= NULL do
6: p2 := start2
7: while p2 ̸= NULL do
8: exp := p1 → exp + p2 → exp
9: coef := p1 → coef ∗ p2 → coef
10: tail := APPEND(tail, exp, coef )
11: if start = NULL then
12: start := tail
13: end if
14: p2 := p2 → next
15: end while
16: p1 := p1 → next
17: end while
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 168 / 172
Polynomial Multiplication
Algorithm 80 Multiplication of Polynomials (Cont.)
18: start := MERGESORT(start)
19: MERGELIKETERMS(start)
20: return start
21: end procedure
Algorithm 81 Append a New Polynomial Node
1: procedure append(tail, exp, coef )
2: node := new PolyNode
3: node → exp := exp
4: node → coef := coef
5: node → next := NULL
6: if tail ̸= NULL then
7: tail → next := node
8: end if
9: return node
10: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 169 / 172
Polynomial Multiplication
Algorithm 82 Merge Like Terms
1: procedure MergeLikeTerms(start)
2: prev := NULL, p1 := start
3: while p1 ̸= NULL do
4: p2 := p1 → next
5: while p2 ̸= NULL and p2 → exp = p1 → exp do
6: p1 → coef := p1 → coef + p2 → coef
7: p2 := p2 → next
8: p1 → next := p2
9: end while
10: if p1 → coef = 0 and prev ̸= NULL then
11: prev → next := p2
12: else
13: prev := p1
14: end if
15: p1 := p2
16: end while
17: end procedure
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 170 / 172
Summary
Here, we have discussed
Introduction to linked list data structure and its types.
Static implementation of single linked list ADT and its operations.
Dynamic implementation of single linked list ADT and its operations.
Dynamic implementation of doubly linked list ADT and its operations.
Dynamic implementation of circular linked list ADT and its
operations.
Polynomial addition using single linked lists.
Polynomial multiplication using single linked lists.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 171 / 172
For Further Reading I
M. A. Weiss
Data Structures and Algorithm Analysis in C (2nd edition).
Pearson India, 2022.
E. Horowitz, S. Sahni and S. A. Freed.
Fundamentals of Data Structures in C (2nd edition).
Universities Press, 2008.
A. K. Rath and A. K. Jagadev.
Data Structures Using C (2nd edition).
Scitech Publications, 2011.
Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 172 / 172

More Related Content

PPTX
Linked list in Data Structure and Algorithm
PPTX
link list.pptx complete notes detailed ans
PPTX
linked list.pptx
PPTX
Linked lists linked lists vs Arrays.pptx
PPTX
LINKED LIST.pptx
PPTX
Link list
PPTX
Link list
Linked list in Data Structure and Algorithm
link list.pptx complete notes detailed ans
linked list.pptx
Linked lists linked lists vs Arrays.pptx
LINKED LIST.pptx
Link list
Link list

Similar to Linked List Data Structures . (20)

PPTX
Data Structures and Algorithms - Lec 05.pptx
PPT
Lecture 2b lists
PPTX
Linked list
PDF
2a-Linked Listsxcxxcxxcxcxcxcxcxcxcxcxx.pdf
PPT
Unit 1 linked list
ODP
Linked List
PPTX
Linked_List_Presentation_1.pptxjsjkskxjjdjekdlkdjmdkdk
PPTX
Linked List.pptx
PDF
Linked list (introduction) 1
DOCX
Linked list.docx
PDF
Data structure
DOCX
Linked List
PPTX
linked list_MODULE 3.pptx ppt on the linked list
PPTX
Dynamic data structures
PPTX
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
PDF
double linked list
PPTX
Different types of Linked list.
PPTX
Introduction to linked list in data structure.pptx
PDF
02. the linked lists (1)
PPTX
linked list in Data Structure, Simple and Easy Tutorial
Data Structures and Algorithms - Lec 05.pptx
Lecture 2b lists
Linked list
2a-Linked Listsxcxxcxxcxcxcxcxcxcxcxcxx.pdf
Unit 1 linked list
Linked List
Linked_List_Presentation_1.pptxjsjkskxjjdjekdlkdjmdkdk
Linked List.pptx
Linked list (introduction) 1
Linked list.docx
Data structure
Linked List
linked list_MODULE 3.pptx ppt on the linked list
Dynamic data structures
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
double linked list
Different types of Linked list.
Introduction to linked list in data structure.pptx
02. the linked lists (1)
linked list in Data Structure, Simple and Easy Tutorial
Ad

More from Ashutosh Satapathy (12)

PDF
Visual Aids for Exploratory Data Analysis.pdf
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
PDF
The Value of Business Intelligence .
PDF
Business Intelligence and Information Exploitation.pdf
PDF
Introduction to Data Structures .
PDF
Searching and Sorting Algorithms
PDF
Multidimensional Data
PDF
Time and Space Complexity
PDF
Algorithm Specification and Data Abstraction
PDF
Secure Multi-Party Computation
Visual Aids for Exploratory Data Analysis.pdf
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
The Value of Business Intelligence .
Business Intelligence and Information Exploitation.pdf
Introduction to Data Structures .
Searching and Sorting Algorithms
Multidimensional Data
Time and Space Complexity
Algorithm Specification and Data Abstraction
Secure Multi-Party Computation
Ad

Recently uploaded (20)

PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Geodesy 1.pptx...............................................
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
PPT on Performance Review to get promotions
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Current and future trends in Computer Vision.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Mechanical Engineering MATERIALS Selection
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Artificial Intelligence
PPT
introduction to datamining and warehousing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT 4 Total Quality Management .pptx
CYBER-CRIMES AND SECURITY A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
additive manufacturing of ss316l using mig welding
Geodesy 1.pptx...............................................
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT on Performance Review to get promotions
CH1 Production IntroductoryConcepts.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Current and future trends in Computer Vision.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mechanical Engineering MATERIALS Selection
UNIT-1 - COAL BASED THERMAL POWER PLANTS
bas. eng. economics group 4 presentation 1.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Artificial Intelligence
introduction to datamining and warehousing

Linked List Data Structures .

  • 1. Linked List Data Structures Dr. Ashutosh Satapathy Assistant Professor, Department of CSE VR Siddhartha Engineering College Kanuru, Vijayawada August 24, 2024 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 1 / 172
  • 2. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 2 / 172
  • 3. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 3 / 172
  • 4. Introduction Array is a data structure where elements are stored in consecutive memory location. It is simple to understand, time to access any clement from an array is constant. Some severe limitations can also be attributed to its simple structure. 1 The size of an array has to be defined when the program is being written and its space is reserved during compilation of the program. 2 During execution of the program, it may happen that the actual size is less than the defined size. So, a good amount of memory space is wasted. 3 In contrast, while executing with larger data, the size of actual data may exceed the limits of the defined size creating undesirable results. 4 During insertion operation in an array, the elements after the inserted element have to be shifted one position to the right. 5 Similarly, during deletion all elements to the right of the deleted element have to be shifted one position to the left. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 4 / 172
  • 5. Introduction Link representation of the list provides a sophisticated solution to the data mobility issue that arises in arrays. A dynamic data structure, or linked list, allows its memory requirements to be adjusted as needed. Adjacency between elements (nodes) in a linked list is preserved by links or pointers. A link or pointer is the address of the element (node) that comes after it. As a result, in a linked list, both the link and the data must be maintained. Two fields make up a node: info, which stores information, and next, which points to the node after it. Figure 1.1: Node structure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 5 / 172
  • 6. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 6 / 172
  • 7. Linked List Types A data structure called a single linked list is made up of a series of units known as nodes. Each node is made up of two components: data and a pointer, or reference, to the node after it in the sequence. Traversal in a single linked list is forward-only, which means that nodes can be traversed other than from the head node. Because there is no direct indexing, this structure can be less efficient for random access than arrays, but it can be efficient for insertion and deletion operations at the beginning and end of the list. Figure 1.2: Single linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 7 / 172
  • 8. Linked List Types A double linked list is a data structure used in computer science to store a collection of elements. In contrast to a singly linked list, where each node contains a reference to the next node, a double linked list contains references to both the next and the previous nodes. This structure allows for efficient traversal in both forward and backward directions. Insertions and deletions can also be performed efficiently at both ends or at any point within the list, given the reference to the previous and next nodes. Figure 1.3: Double linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 8 / 172
  • 9. Linked List Types A circular linked list is a variation of a linked list data structure where the last node of the list points back to the first node, creating a circular loop. Unlike a traditional linked list where the last node points to NULL, in a circular linked list, the last node’s pointer points back to the first node. The last node’s next pointer points to the first node, forming a circular loop that connects the last node to the first node. This circular structure enables seamless traversal from any node in the list to any other node. Figure 1.4: Circular singly linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 9 / 172
  • 10. Linked List Types A circular double linked list is a variation of the double linked list data structure in which the last node points to the first node, creating a circular loop, just like in a circular linked list. However, in a circular double linked list, each node also has a reference to the previous node, in addition to the reference to the next node. The last node’s next pointer points to the first node, and the first node’s previous pointer points to the last node, forming a circular loop with bidirectional connections. Each node in a circular double linked list contains three fields: previous pointer, data and next pointer. Figure 1.5: Circular double linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 10 / 172
  • 11. Linked List Types A header linked list is a variant of a linked list data structure that includes an additional ”header node” at the beginning of the list. This header node does not store any data but serves as a dummy node to simplify operations and improve code clarity. In a header linked list, the header node typically contains pointers to the first node of the linked list. A header-linked list typically includes the following components: a header node and data nodes. Like a single linked list, each data node contains pointers to the next node in the sequence. Figure 1.6: Header linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 11 / 172
  • 12. Linked List Types A circular header linked list is a variation of a linked list data structure that combines features of both circular linked lists and header linked lists. It includes a header node at the beginning of the list, which may contain metadata or attributes about the list. Like a circular linked list, the last node points back to the first node, creating a circular loop. The header node contains metadata about the list and pointers to the first data node. Data nodes are the actual nodes of the list, containing the data elements. Figure 1.7: Circular header linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 12 / 172
  • 13. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 13 / 172
  • 14. Static Implementation Another name for a single-linked list is a one-way list. Each node in a single linked list has a data field and a single pointer field that points to the node next to it in the list. A single linked list can be represented in memory in two different ways. Representations that are both static and dynamic. Two arrays are maintained in a static representation of a single linked list: one for links and one for data. The complete linked list should fit inside the two parallel arrays that are allotted, each of equal size. Figure 2.1: Static representation of a single linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 14 / 172
  • 15. Static Implementation A linked list can be thought of as an array of nodes, with each node containing a structure with two fields: info and next. We can declare a linked list containing 50 nodes as follows: An array index represents a pointer to a node. In other words, a pointer is a number that refers to a specific element in the array of nodes and ranges from 0 to SIZE − 1. As an illustration, the array index 5 points to the node[5]. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 15 / 172
  • 16. Static Implementation The notations that we employ are 1 node(p) : pth indexed node. (i.e., node[p]) 2 info(p) : info field value of pth node. (i.e., node[p].info) 3 next(p) : next field value of pth node. (i.e., node[p].next) 4 NULL is symbolized by -1. Figure 2.2: Static implementation of a single linked list The array’s first element, node[5], is indicated by the array index of 5. node[5].next has the value 10. node [10] is regarded as the array’s second element. node[10].next has the value 15, thus node [15] is regarded as the array’s third element. Nodes [30] and [40] are the array’s fourth and fifth items, respectively. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 16 / 172
  • 17. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 17 / 172
  • 18. Operations Available List Initialization: Create an array of nodes available for building a single linked list. Figure 2.3: Array of 50 available nodes Algorithm 1 Available List Initialization 1: procedure List-initialization(node, MAX) 2: for i ← 0 to MAX − 2 do 3: node[i].next := i + 1 4: end for 5: node[SIZE − 1].next := −1 6: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 18 / 172
  • 19. Operations Empty Node Selection: A node can be acquired from the available list when it is required for use in a linked list. Index position of the node that was removed from the available list is returned by the GetNode() function. Algorithm 2 Empty Node Selection 1: procedure NodeSelection(avail) 2: if (avail = −1) then 3: write ”overflow” 4: return -1 5: end if 6: p := avail 7: avail := node[avail].next 8: return p 9: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 19 / 172
  • 20. Static Implementation Empty Node Selection: Figure 2.4: List of available nodes In the available list above, there are four nodes. p stores avail value while node selection, i.e., we store 11 into p. After removing node[11] from the available list, the avail must be made to point to the next node[31] in the available list. For this, we write avail = node[avail].next. Now, the first node[11] is removed, with its index position available with p. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 20 / 172
  • 21. Operations Insert After a Given Node: Insert a node next to the node with index p, i.e., node[p]. Fetch the first node from the available list and update the avail. Algorithm 3 Insert After a Node 1: procedure Insert(p, avail, x) 2: if p = −1 then 3: write ”void insertion” 4: return -1 5: end if 6: q := NODESELECTION(avail) 7: node[q].info := x 8: node[q].next := node[p].next 9: node[p].next := q 10: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 21 / 172
  • 22. Operations Insert After a Given Node: (a) Single linked list before insert operation (b) Single-linked list after insert operation (p = 39, x = 55). Figure 2.5: Insert after a given node Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 22 / 172
  • 23. Operations Deletion After a Given Node: Delete node next to the node with index p, i.e., node[p]. Add the removed node to the available list and update the avail. Algorithm 4 Insert After a Node 1: procedure Deletion(p) 2: q := node[p].next 3: x := node[q].info 4: node[q].info := ∞ 5: node[p].next := node[q].next 6: node[q].next := −1 7: FREENODE(q, avail) 8: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 23 / 172
  • 24. Operations Insert After a Given Node: (a) Single linked list before deletion (b) Single linked list before FREENODE execution (p = 7, q = 13). Figure 2.6: Deletion after a given node Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 24 / 172
  • 25. Operations Returning Node to Available List: FREENODE function accepts the index position of a node and returns that node to the available list. Add the removed node at the beginning of the available list. Algorithm 5 Add Node to Available List 1: procedure Freenode(q) 2: node[q].next := avail 3: avail := q 4: return avail 5: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 25 / 172
  • 26. Operations (a) Available list before FREENODE execution. (b) Available list after FREENODE execution (q = 13). (c) Single linked list after FREENODE execution (q = 13). Figure 2.7: Single linked list after deletion. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 26 / 172
  • 27. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 27 / 172
  • 28. Dynamic Implementation The number of elements in an array must be specified by the C language during compilation. This could result in memory space being wasted. Dynamic data structures can be used to handle such circumstances. Dynamic memory allocation management strategies enable the allocation of extra memory or the release of unnecessary space during runtime. During program execution, the following memory management functions can be used to allocate and free up memory: 1 malloc(): Allocate size of bytes 2 calloc(): Allocate an array of defined no. of elements of specified size. 3 realloc(): Resize the previous assigned space. 4 free(): Release space associated with a pointer. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 28 / 172
  • 29. Dynamic Implementation Single Block of Memory Allocation A memory block can be allocated using the malloc() function. It returns a pointer of type void and reserves a certain amount of memory. ptr = (cast-type*)malloc(byte-size); The malloc() returns a pointer (of cast type) to an area of memory with size byte-size. y = (int*)malloc(100*sizeof(int)); In the above statement, 100 times the size of an integer variable of space is reserved. The pointer y is assigned the address of the first byte of the allocated memory. The malloc function is initialized to garbage values if no value given. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 29 / 172
  • 30. Dynamic Implementation Multi-Block of Memory Allocation At runtime, memory space for storing derived data types is sought after using the calloc() function. The calloc() function is used to allocate multiple storage blocks. ptr = (cast-type*)calloc(n, element-size); Contiguous space for n blocks, each of element-size bytes, is allocated. y = (int*)calloc(100, sizeof(int)); 100 blocks of integer bytes of space are allocated, and pointer y is assigned with the starting address. Memory blocks allocated by the calloc function are always initialized to zero. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 30 / 172
  • 31. Dynamic Implementation Resizing Allotted Memory The realloc() function makes an effort to modify the size of a memory block that was previously allocated. The new dimensions may be greater or lesser. Increases in block size result in memory being added to the end of the block while the previous contents stay the same. The remaining contents stay unaltered if the size is decreased. ptr = (cast-type*)realloc(ptr, new-size); y = (int*)realloc(y, sizeof(int)*50); Realloc() will try to assign a new block of memory and duplicate the contents of the previous block if the original block size cannot be changed. This will result in the return of a new pointer with a different address. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 31 / 172
  • 32. Dynamic Implementation Releasing Memory Memory allocated with the help of the calloc and malloc routines is not released on its own. To release memory that has already been allotted, use the free() function. It accepts a pointer as its parameter. For instance, free(ptr); ptr has to point to memory that was allocated using the malloc() function. This feature aids in preventing memory leaks, which might result in system slowdown or crash. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 32 / 172
  • 33. Dynamic Implementation Utilizing a free pool of storage for a linked list involves a Memory Manager program handling memory allocation requests by searching for available blocks in a Memory Bank. Each time a new node is required, the Memory Manager locates a suitable memory block from the bank and assigns it to the node. A Garbage Collector component is activated when nodes are no longer in use, returning their memory blocks to the bank for reuse. The Memory Bank serves as a list of available memory blocks for the programmer to manage efficiently. This approach streamlines memory usage, ensuring optimal allocation and deallocation processes for linked list construction and maintenance. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 33 / 172
  • 34. Dynamic Implementation One-way lists are another name for single linked lists. Pointers are used to indicate the linear order of this node-based, linear collection of data components. Every node has two components, information and pointer to next node fields. A linked list is made up of several structures that aren’t always connected. Every structure has a pointer to a structure that houses its successor as well as one or more information fields. A particular value known as the NULL pointer, or any invalid address, is present in the pointer field of the final node. A single linked list with five nodes is depicted in the accompanying figure. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 34 / 172
  • 35. Dynamic Implementation Figure 2.8: Single linked list with 5 nodes Node Structure struct Node{ int info; struct Node *next; }; Create a start pointer: struct Node *start = NULL; Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 35 / 172
  • 36. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 36 / 172
  • 37. Operations Operations on a Single Linked List 1 Traverse a linked list 2 Insert at beginning 3 Insert at end 4 Insert at a specific location 5 Insert after a given node 6 Deletion from beginning 7 Deletion at end 8 Delete at a specific location 9 Delete after a given node 10 Merge two linked lists 11 Search in a linked list 12 Sort a linked list 13 Reverse a linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 37 / 172
  • 38. Operations Traverse a Linked List Suppose we have a single linked list in memory and we wish to process each node exactly once by traversing the list. Starting at the first node and working our way down the list is our goal. Let us consider a pointer-type variable node, node→next, which points to the node after it, and which points to the node that is presently being processed. Figure 2.9: Single linked list with 5 data nodes Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 38 / 172
  • 39. Operations Algorithm 6 Traverse a Linked List 1: procedure Traverse(start) 2: node := start ▷ initialize pointer variable node 3: while node ̸= NULL do 4: write node → info ▷ print info[node] 5: node := node → next ▷ Move to next node 6: end while 7: end procedure Output: 11 22 33 44 55 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 39 / 172
  • 40. Operations Insert at Beginning An element can be added to an already-existing linked list using the insertion operation. The list could be in any order. An ordered list’s information fields can be arranged in either a decreasing or increasing order. In an ordered list, we must compare the information fields of every node till the end of the list in order to determine the proper location for each element to be inserted. Assume that a linked list that isn’t necessarily sorted is present in memory. The simplest method is to add a node at the start. Figure 2.10: Single linked list before insertion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 40 / 172
  • 41. Operations Algorithm 7 Insert at Beginning 1: procedure Insert Begin(start, x) 2: node := new Node ▷ Create a new node named as node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := start ▷ new node now points to first node 8: start := node 9: end procedure Figure 2.11: Single linked list after insertion (x = 11) Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 41 / 172
  • 42. Operations Insert at End We must iterate through the list and advance the pointer until the last node is reached in order to insert a node at the end. The new node is added as the final node, with an information field value of 77, as shown in figure 2.13. Figure 2.12: Single linked list before insertion Figure 2.13: Single linked list after insertion (x = 77) Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 42 / 172
  • 43. Operations Algorithm 8 Insert at End 1: procedure Insert End(start, x) 2: node := new Node ▷ Create a new node named as node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := NULL 8: curr := start 9: while curr ̸= NULL do 10: prev := curr 11: curr := curr → next 12: end while 13: prev → next := node 14: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 43 / 172
  • 44. Operations Insert at a Specific Location There are situations when placing things in a list at a specified location is necessary. Tracking the beginning node, current node, and prior node is required in order to insert an element at a specific point. This is accomplished by starting at the beginning of the list and scanning our way up to the target node, where the new node is to be inserted. Third place, loc = 3, is where the new node with 88 in its information field is inserted. Figure 2.14: Single linked list before insertion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 44 / 172
  • 45. Operations Algorithm 9 Insert at a Specific Location 1: procedure Insert at Location(start, x) 2: node := new Node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := NULL 8: read loc 9: i := 1 10: curr := start 11: while curr ̸= NULL and i < loc do 12: prev := curr 13: curr := curr → next 14: i := i + 1 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 45 / 172
  • 46. Operations Algorithm 10 Insert at a Specific Location (Cont.) 15: end while 16: if curr = NULL then 17: write ”Position not found” and exit 18: end if 19: prev → next := node 20: node → next := curr 21: end procedure Figure 2.15: Single linked list after insertion (loc = 3, x = 88) Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 46 / 172
  • 47. Operations Insert After a Given Node To insert a node after a given node, we need to traverse the list and move the pointer until we find that node. Let it be node A. Then we create a new node, i.e., node n, and it is inserted in between node A and the next node. The new node whose information field contains 88 is inserted after the given node, which is 33. Figure 2.16 and 2.17 illustrate the linked list before and after insertion, respectively. Figure 2.16: Single linked list before insertion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 47 / 172
  • 48. Operations Algorithm 11 Insert After a Given Node 1: procedure Insert After Node(start, x) 2: node := new Node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := NULL 8: read item 9: curr := start 10: while curr ̸= NULL and curr → info ̸= item do 11: curr := curr → next 12: end while Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 48 / 172
  • 49. Operations Algorithm 12 Insert After a Given Node (Cont.) 13: if curr = NULL then 14: write ”item is not in the list” and exit 15: end if 16: node → next := curr → next 17: curr → next := node 18: end procedure Figure 2.17: Single linked list after insertion (item = 33, x = 88) Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 49 / 172
  • 50. Operations Deletion from Beginning It is not feasible to delete anything from a list that is empty; the output should say ”Deletion is not possible”. Assume that a linked list that isn’t necessarily sorted is present in memory. Eliminating the initial node is the simplest method. The list will be empty if there is only one node on it after deletion. After being deleted, the pointer variable start should point to the second node instead of the first. The operating system gathers free space and creates a link to the node that has space available right now. Figure 2.18: Single linked list before deletion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 50 / 172
  • 51. Operations Algorithm 13 Deletion from Beginning 1: procedure Delete Begin(start) 2: temp := start ▷ temp points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: start := start → next ▷ start points to next node 7: free temp ▷ Free space associated with temp 8: end procedure Figure 2.19: Single linked list after deletion. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 51 / 172
  • 52. Operations Deletion at End Moving the pointer along the list until we reach the address of the last node is necessary in order to remove it. Keep the address of the preceding node in mind as you advance the pointer. Since the preceding node will be the last, enter NULL in its next pointer field and release the memory space occupied by the last node. Figure 2.20: Single linked list before deletion Figure 2.21: Single linked list after deletion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 52 / 172
  • 53. Operations Algorithm 14 Deletion at End 1: procedure Delete End(start) 2: node := start ▷ node points to the first node 3: temp := start ▷ temp points to the first node 4: if start = NULL then 5: write ”UNDERFLOW” and exit 6: end if 7: while node → next ̸= NULL do 8: temp := node 9: node := node → next 10: end while 11: temp → next := NULL ▷ Place NULL in second last node 12: free node ▷ Free space associated with node 13: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 53 / 172
  • 54. Operations Delete at a Specific Location Let’s say we wish to remove a node from a linked list at a particular position. To get the node’s position, we must iterate through the linked list. Transfer the pointer to that specific node, modify the pointer (i.e., the pointer of this node’s predecessor will now point to this node’s successor), and release the node. Verify if the list is empty or not before performing the deletion. If so, an ’UNDERFLOW’ message is displayed, and the program terminates. Figure 2.22: Single linked list before deletion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 54 / 172
  • 55. Operations Algorithm 15 Delete at a Specific Location 1: procedure Delete at Location(start) 2: node := start ▷ node points to the first node 3: temp := start ▷ temp points to the first node 4: if start = NULL then 5: write ”UNDERFLOW” and exit 6: end if 7: i := 1 8: read loc 9: while node ̸= NULL and i < loc do 10: temp := node 11: node := node → next 12: i := i + 1 13: end while Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 55 / 172
  • 56. Operations Algorithm 16 Delete at a Specific Location (Cont.) 14: if node = NULL then 15: write ’Position not Found’ and exit 16: end if 17: temp → next := node → next ▷ Next node addr. in temp next 18: free node ▷ Free space associated with node 19: end procedure Figure 2.23: Single linked list after deletion (loc = 3). Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 56 / 172
  • 57. Operations Delete After a Given Node We must go through the list and move the pointer until we locate the node we want to delete after a specific node. Select node A. Node n is the next node that needs to be removed. Once nodes A and B (next node of node n) are linked, delete node n. If the list is empty, an ’UNDERFLOW’ message is displayed, and the program terminates. Figure 4.16 shows the node whose information field contains 44 is deleted after the given node, which is 33. Figure 2.24: Single linked list before deletion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 57 / 172
  • 58. Operations Algorithm 17 Delete After a Given Node 1: procedure Delete After Node(start) 2: node := start ▷ node points to the first node 3: temp := start ▷ temp points to the first node 4: if start = NULL then 5: write ”UNDERFLOW” and exit 6: end if 7: read item 8: while node ̸= NULL and item ̸= node → info do 9: temp := node 10: node := node → next 11: end while 12: if node = NULL then 13: write ’INVALID DELETION’ and exit 14: end if Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 58 / 172
  • 59. Operations Algorithm 18 Delete After a Given Node (Cont.) 15: temp → next := node → next ▷ Next node addr. in temp next 16: free node ▷ Free space associated with node 17: end procedure Figure 2.25: Single linked list after deletion (item = 44). Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 59 / 172
  • 60. Operations Merge Two Linked Lists Memory contains two linked lists. To combine two linked lists into one bigger list, you must do this. It is required to connect the final node of the first list to the initial node of the second list in order to obtain the merged list. (a) Single linked list 1 (b) Single linked list 2 Figure 2.26: Linked lists before merge operation. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 60 / 172
  • 61. Operations Algorithm 19 Merge Two Linked Lists 1: procedure Merge Lists(start := NULL, start1, start2) 2: node1 := start1 3: if start1 = NULL then 4: write ’Out of Memory Space’ and exit 5: end if 6: node := new Node 7: start := node 8: start → info := node1 → info 9: start → next := NULL 10: node1 := node1 → next 11: while node1 ̸= NULL do 12: curr := new Node 13: if curr = NULL then 14: write ’Out of Memory Space’ and exit Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 61 / 172
  • 62. Operations Algorithm 20 Merge Two Linked Lists (Cont.) 15: end if 16: curr → info := node1 → info 17: curr → next := NULL 18: node1 := node1 → next 19: node → next := curr 20: node := curr 21: end while 22: node2 := start2 23: while node2 ̸= NULL do 24: curr := new Node 25: if curr = NULL then 26: write ’Out of Memory Space’ and exit 27: end if 28: curr → info := node2 → info Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 62 / 172
  • 63. Operations Algorithm 21 Merge Two Linked Lists (Cont.) 29: curr → next := NULL 30: node2 := node2 → next 31: node → next := curr 32: node := curr 33: end while 34: end procedure Figure 2.27: Linked list after merging linked lists 1 and 2. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 63 / 172
  • 64. Operations Search in a Linked List Finding an item in a list is the definition of searching. Assume we have the linked list shown in Figure 2.28 and we need to look for one item, let’s say 44. The item’s storage location (in this case, number 4) will be returned if it is located in the list. Else, ”Element not found.” will be displayed. Algorithm 22 Search in a Linked List 1: procedure Search List(start) 2: node := start ▷ node points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: count := 1 7: read item Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 64 / 172
  • 65. Operations Algorithm 23 Search in a Linked List (Cont.) 8: while node ̸= NULL do 9: if item = node → info then 10: write ’Element Found at Location’, count and exit 11: end if 12: count := count + 1 13: node := node → next 14: end while 15: write ’Element not Found’ 16: end procedure Figure 2.28: Search an element 44. Output: Element Found at Location 4 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 65 / 172
  • 66. Operations Sort a Linked List Arranging the list elements in either ascending or descending order is known as sorting. Figure 2.29 and 2.30 exhibits the linked list before and after sorting respectively. Figure 2.29: Linked list before sorting Algorithm 24 Sort a Linked List 1: procedure Sort Nodes(start) 2: node := start ▷ node points to the first node 3: while node → next ̸= NULL do 4: ptr := node → next 5: while ptr ̸= NULL do Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 66 / 172
  • 67. Operations Algorithm 25 Sort a Linked List (Cont.) 6: if node → info > ptr → info then ▷ If TRUE, Swap 7: temp := node → info 8: node → info := ptr → info 9: ptr → info := temp 10: end if 11: ptr := ptr → next 12: end while 13: node := node → next 14: end while 15: end procedure Figure 2.30: Linked list after sorting Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 67 / 172
  • 68. Operations Reverse a Linked List Iterate through the list, reversing the direction of pointers between nodes. Update the start pointer to point to the last node encountered during traversal. Figure 2.31: Original linked list Algorithm 26 Reverse a Linked List 1: procedure Reverse List(start) 2: curr := start ▷ node points to the first node 3: if curr → next = NULL then ▷ Check for single node linked list 4: exit 5: end if Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 68 / 172
  • 69. Operations Algorithm 27 Reverse a Linked List (Cont.) 6: prev := curr → next 7: curr → next := NULL 8: while prev → next ̸= NULL do 9: ptr := prev → next 10: prev → next := curr 11: curr := prev 12: prev := ptr 13: end while 14: prev → next := curr 15: start := prev 16: end procedure Figure 2.32: Reversed linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 69 / 172
  • 70. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 70 / 172
  • 71. Dynamic Implementation There is only one way to go from the first node to any other node in a single linked list, and that is left to right. A single linked list is also known as a one-way list for this reason. Conversely, a doubly linked list allows for bidirectional movement, allowing for left-to-right or right-to-left movement. Each node in a two-way list is a linear collection of data elements called nodes that are split into three sections. 1 A data-containing information field. 2 The next field is a pointer field that holds the address of the node that follows in the list. 3 The address of the node in the list that comes before it is stored in a pointer field called pre. Figure 3.1: Doubly linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 71 / 172
  • 72. Dynamic Implementation The pointers pre of the first node and next of the last node contain the value NULL, i.e., they do not store the address of any other node. This indicates the beginning and end of the list, respectively. The advantage of a doubly linked list over a single-linked list is that traversal is possible in both directions. This makes it an ideal data structure for applications like databases and word processors, in which moving in both directions is necessary. Node Structure struct dNode{ struct dNode *pre; int info; struct dNode *next; }; Create a start pointer: struct dNode *start = NULL; Create a last pointer: struct dNode *last = NULL; Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 72 / 172
  • 73. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 73 / 172
  • 74. Operations Operations on a Doubly Linked List 1 Traverse a linked list 2 Insert at beginning 3 Insert at end 4 Insert at a specific location 5 Insert after a given node 6 Deletion from beginning 7 Deletion at end 8 Delete at a specific location 9 Delete after a given node 10 Merge two linked lists 11 Search in a linked list 12 Sort a linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 74 / 172
  • 75. Operations Traverse a Linked List Let’s say we have a doubly linked list in memory and we want to process each node exactly once by traversing the list. Our goal is to iterate through the list, beginning at the beginning and ending at the conclusion. Let curr be a pointer-type variable that points to the node that is being processed at the moment. The next node is indicated by curr → next. Figure 3.2: Doubly linked list with 5 data nodes Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 75 / 172
  • 76. Operations Algorithm 28 Traverse a Linked List 1: procedure Traverse(start) 2: curr := start ▷ initialize pointer variable node 3: while curr ̸= NULL do 4: write curr → info ▷ print info[curr] 5: curr := curr → next ▷ Move to next node 6: end while 7: end procedure Output: 11 22 33 44 55 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 76 / 172
  • 77. Operations Insert at Beginning Figure 3.3a and 3.3b show the double-linked list both before and after the insertion. During insertion, the previous and next pointer fields should be changed. (a) Doubly linked list before insertion. (b) Doubly linked list after insertion (x = 66). Figure 3.3: Insert at the beginning. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 77 / 172
  • 78. Operations Algorithm 29 Insert at Beginning 1: procedure Insert Begin(start, x) 2: node := new Node ▷ Create a new node named as node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := start ▷ New node now points to first node 8: node → pre := NULL 9: start → pre := node ▷ First node’s pre points to new node 10: start := node 11: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 78 / 172
  • 79. Operations Insert at End Skip every node from the start to the finish in order to add a node at the end. Once It is possible to build a link if we know the address of the final node. The previous and next pointer fields should be changed appropriately. (a) Doubly linked list before insertion. (b) Doubly linked list after insertion (x = 55). Figure 3.4: Insert at the end. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 79 / 172
  • 80. Operations Algorithm 30 Insert at End 1: procedure Insert End(start, x) 2: node := new Node ▷ Create a new node named as node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := NULL 8: curr := start 9: while curr ̸= NULL do 10: prev := curr 11: curr := curr → next 12: end while 13: prev → next := node 14: node → pre := prev 15: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 80 / 172
  • 81. Operations Insert at Specific Location A node can be added at the desired location if its value and location are known. Figures 3.5a and 3.5b illustrate the list before and after insertion, respectively. (a) Doubly linked list before insertion. (b) Doubly linked list after insertion (loc = 3, x = 66). Figure 3.5: Insert at a specific location. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 81 / 172
  • 82. Operations Algorithm 31 Insert at a Specific Location 1: procedure Insert at Location(start, x) 2: node := new Node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := NULL 8: read loc 9: i := 1 10: curr := start 11: while curr ̸= NULL and i < loc do 12: prev := curr 13: curr := curr → next Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 82 / 172
  • 83. Operations Algorithm 32 Insert at a Specific Location (Cont.) 14: i := i + 1 15: end while 16: if curr = NULL then 17: write ”Position not found” and exit 18: end if 19: prev → next := node 20: node → pre := prev 21: node → next := curr 22: curr → pre := node 23: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 83 / 172
  • 84. Operations Insert After a Given Node To insert a node after a given node, an appropriate position has to be found after advancing the node pointer. Then a link can be established. Figures 3.6a and 3.6b illustrate the list before and after insertion, respectively. (a) Doubly linked list before insertion. (b) Doubly linked list after insertion (item = 22, x = 66). Figure 3.6: Insert after a given node. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 84 / 172
  • 85. Operations Algorithm 33 Insert After a Given Node 1: procedure Insert After Node(start, x) 2: node := new Node 3: if node = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node → info := x ▷ Copies new data into new node 7: node → next := NULL 8: read item 9: curr := start 10: while curr ̸= NULL and curr → info ̸= item do 11: curr := curr → next 12: end while Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 85 / 172
  • 86. Operations Algorithm 34 Insert After a Given Node (Cont.) 13: if curr = NULL then 14: write ”item is not in the list” and exit 15: end if 16: temp := curr → next 17: node → next := temp 18: temp → pre := node 19: curr → next := node 20: node → pre := curr 21: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 86 / 172
  • 87. Operations Deletion from Beginning Figures 3.7a and 3.7b illustrate the list before and after deletion, respectively. The node pointer should be adjusted accordingly after deletion. (a) Doubly linked list before deletion. (b) Doubly linked list after deletion. Figure 3.7: Deletion from beginning. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 87 / 172
  • 88. Operations Algorithm 35 Deletion from Beginning 1: procedure Delete Begin(start) 2: temp := start ▷ temp points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: start := start → next ▷ start points to next node 7: start → pre := NULL 8: free temp ▷ Free space associated with temp 9: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 88 / 172
  • 89. Operations Deletion at End Figures 3.7a and 3.7b illustrate the list before and after deletion, respectively. The node pointer should be adjusted accordingly after deletion. (a) Doubly linked list before deletion. (b) Doubly linked list after deletion. Figure 3.8: Deletion at end. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 89 / 172
  • 90. Operations Algorithm 36 Deletion at End 1: procedure Delete End(start) 2: node := start ▷ node points to the first node 3: temp := start ▷ temp points to the first node 4: if start = NULL then 5: write ”UNDERFLOW” and exit 6: end if 7: while node → next ̸= NULL do 8: temp := node 9: node := node → next 10: end while 11: temp → next := NULL ▷ Place NULL in second last node 12: free node ▷ Free space associated with node 13: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 90 / 172
  • 91. Operations Delete at a Specific Location Illustrated in Figures 3.9a and 3.9b are the list before and after deletion. The previous and next pointer fields must be adjusted to maintain the link. (a) Doubly linked list before deletion. (b) Doubly linked list after deletion (loc = 3). Figure 3.9: Delete at a specific location. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 91 / 172
  • 92. Operations Algorithm 37 Delete at a Specific Location 1: procedure Delete at Location(start) 2: node := start ▷ node points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: i := 1 7: read loc 8: while node → next ̸= NULL do 9: node := node → next 10: c := c+1 11: end while 12: if loc = 1 and loc = c then 13: start := NULL and exit 14: else 15: start := node → next Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 92 / 172
  • 93. Operations Algorithm 38 Delete at a Specific Location (Cont.) 16: start → pre := NULL and go to 29 17: end if 18: while node ̸= NULL and i < loc do 19: prev := node 20: node := node → next 21: i := i + 1 22: end while 23: if node = NULL then 24: write ’Position not Found’ and exit 25: end if 26: temp := node → next ▷ temp points to Next node 27: prev → next := temp ▷ temp addr. in prev next 28: temp → pre := prev 29: free node ▷ Free space associated with node 30: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 93 / 172
  • 94. Operations Delete After a Given Node Figure 3.10a and 3.10b illustrate the list before and after deletion, respectively. The previous and next pointer fields should be adjusted accordingly to maintain the link. (a) Doubly linked list before deletion. (b) Doubly linked list after deletion (item = 22). Figure 3.10: Delete after a given node. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 94 / 172
  • 95. Operations Algorithm 39 Delete After a Given Node 1: procedure Delete After Node(start) 2: node := start ▷ node points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: read item 7: while node ̸= NULL and item ̸= node → info do 8: prev := node 9: node := node → next 10: end while Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 95 / 172
  • 96. Operations Algorithm 40 Delete After a Given Node (Cont.) 11: if node = NULL and node → next = NULL then 12: write ’INVALID DELETION’ and exit 13: end if 14: prev := node 15: node := node → next 16: temp := node → next ▷ temp points to the Next node 17: prev → next := temp ▷ temp addr. in prev next 18: temp → pre := prev 19: free node ▷ Free space associated with node 20: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 96 / 172
  • 97. Operations Merge Two Linked Lists Memory contains two linked lists. To combine two linked lists into one bigger list, you must do this. It is required to connect the final node of the first list to the initial node of the second list in order to obtain the merged list. (a) Single linked list 1 (b) Single linked list 2 Figure 3.11: Linked lists before merge operation. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 97 / 172
  • 98. Operations Algorithm 41 Merge Two Linked Lists 1: procedure Merge Lists(start := NULL, start1, start2) 2: node1 := start1 3: if start1 = NULL then 4: write ’Out of Memory Space’ and exit 5: end if 6: node := new Node 7: start := node 8: start → info := node1 → info 9: start → next := NULL 10: start → pre := NULL 11: node1 := node1 → next 12: while node1 ̸= NULL do 13: curr := new Node 14: if curr = NULL then Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 98 / 172
  • 99. Operations Algorithm 42 Merge Two Linked Lists (Cont.) 15: write ’Out of Memory Space’ and exit 16: end if 17: curr → info := node1 → info 18: curr → next := NULL 19: curr → pre := node 20: node → next := curr 21: node1 := node1 → next 22: node := curr 23: end while 24: node2 := start2 25: while node2 ̸= NULL do 26: curr := new Node 27: if curr = NULL then 28: write ’Out of Memory Space’ and exit Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 99 / 172
  • 100. Operations Algorithm 43 Merge Two Linked Lists (Cont.) 29: end if 30: curr → info := node2 → info 31: curr → next := NULL 32: curr → pre := node 33: node → next := curr 34: node2 := node2 → next 35: node := curr 36: end while 37: end procedure Figure 3.12: Linked list after merging linked lists 1 and 2. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 100 / 172
  • 101. Operations Search in a Linked List Finding an item in a list is the definition of searching. Assume we have the linked list shown in Figure 3.13 and we need to look for one item, let’s say 44. The item’s storage location (in this case, number 4) will be returned if it is located in the list. Else, ”Element not found.” will be displayed. Algorithm 44 Search in a Linked List 1: procedure Search List(start) 2: node := start ▷ node points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: count := 1 7: read item Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 101 / 172
  • 102. Operations Algorithm 45 Search in a Linked List (Cont.) 8: while node ̸= NULL do 9: if item = node → info then 10: write ’Element Found at Location’, count and exit 11: end if 12: count := count + 1 13: node := node → next 14: end while 15: write ’Element not Found’ 16: end procedure Figure 3.13: Search an element 44. Output: Element Found at Location 4 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 102 / 172
  • 103. Operations Sort a Linked List Arranging the list elements in either ascending or descending order is known as sorting. Figure 3.14 and 3.15 exhibits the linked list before and after sorting respectively. Figure 3.14: Linked list before sorting Algorithm 46 Sort a Linked List 1: procedure Sort Nodes(start) 2: node := start ▷ node points to the first node 3: while node → next ̸= NULL do 4: ptr := node → next 5: while ptr ̸= NULL do Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 103 / 172
  • 104. Operations Algorithm 47 Sort a Linked List (Cont.) 6: if node → info > ptr → info then ▷ If TRUE, Swap 7: temp := node → info 8: node → info := ptr → info 9: ptr → info := temp 10: end if 11: ptr := ptr → next 12: end while 13: node := node → next 14: end while 15: end procedure Figure 3.15: Linked list after sorting Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 104 / 172
  • 105. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 105 / 172
  • 106. Dynamic Implementation A circular linked list is one in which every node is connected to every other node to create a circle. The start and end nodes in a circular linked list are connected to one another to form a circle. The end does not contain a NULL. We traverse a circular singly linked list until we reach the same node where we started. Operating systems primarily use circular linked lists for task maintenance. Each node in a circular linked list is split into two sections. 1 A data-containing information field. 2 The next field is a pointer field that holds the address of the node that follows in the list. Figure 4.1: Circular singly linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 106 / 172
  • 107. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 107 / 172
  • 108. Operations Operations on a Circular Singly Linked List 1 Traverse a linked list 2 Insert at beginning 3 Insert at end 4 Insert at a specific location 5 Insert after a given node 6 Deletion from beginning 7 Deletion at end 8 Delete at a specific location 9 Delete after a given node 10 Merge two linked lists 11 Search in a linked list 12 Sort a linked list 13 Reverse a linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 108 / 172
  • 109. Operations Traverse a Circular Linked List A circular singly linked list traversal begins from any node and continues until the traversal reaches the starting point again. Traversal involves visiting each node in the list, starting from the first node, and moving to the next node until the first node is encountered again. Traversing a circular singly linked list ensures that every element in the list is visited once, maintaining the circular structure without getting stuck in an infinite loop. Figure 4.2: Circular singly linked list with 5 data nodes Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 109 / 172
  • 110. Operations Algorithm 48 Traverse a Circular Singly Linked List 1: procedure Traverse(start) 2: node := start ▷ initialize pointer variable node 3: write node → info ▷ print info[node] 4: node := node → next ▷ Move to next node 5: while node ̸= start do 6: write node → info ▷ print info[curr] 7: node := node → next ▷ Move to next node 8: end while 9: end procedure Output: 11 22 33 44 55 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 110 / 172
  • 111. Operations Insert at Beginning Figure 4.3a and 4.3b show the circular singly linked list both before and after the insertion. When inserting a node, the start pointer and next pointer of the end node should be updated. (a) Circular singly linked list before insertion. (b) Circular singly linked list after insertion (x = 66). Figure 4.3: Insert at the beginning. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 111 / 172
  • 112. Operations Algorithm 49 Insert at Beginning 1: procedure Insert Begin(start, x) 2: node := start ▷ initialize pointer variable node 3: node1 := new Node ▷ Create a new node named as node 4: if node1 = NULL then 5: write ”Out of memory space” and exit 6: end if 7: node1 → info := x ▷ Copies new data into new node 8: node1 → next := start ▷ New node now points to first node 9: while node → next ̸= start do 10: node := node → next 11: end while 12: node → next := node1 13: start := node1 14: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 112 / 172
  • 113. Operations Insert at End Adding a node at the end of a circular singly linked list involves traversing the list to reach the last node. If the list is empty, the new node becomes the first node, and its next pointer is set to itself, forming a circle. For non-empty lists, the new node is appended after the last node, and its next pointer is connected back to the start to maintain the circular structure. The process ensures that the new node becomes the new last node in the circular list. Figure 4.4: Circular single linked list before insertion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 113 / 172
  • 114. Operations Algorithm 50 Insert at End 1: procedure Insert End(start, x) 2: node1 := new Node ▷ Create a new node named as node 3: if node1 = NULL then 4: write ”Out of memory space” and exit 5: end if 6: node1 → info := x ▷ Copies new data into new node 7: node := start 8: if node = NULL then 9: node1 → next := node1 10: start := node1 and exit 11: end if Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 114 / 172
  • 115. Operations Algorithm 51 Insert at End (Cont.) 12: while node → next ̸= start do 13: node := node → next 14: end while 15: node → next := node1 16: node1 → next := start 17: end procedure Figure 4.5: Circular single linked list after insertion (x = 66). Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 115 / 172
  • 116. Operations Insert at a Specific Location Inserting a node at a specific location in a circular singly linked list involves updating pointers to maintain the circular structure. Determine the insertion point by traversing the list until the desired location is reached. Adjust pointers to link the new node to the list while preserving the circular nature of the structure. Special cases include inserting at the beginning (changing the start) and end (updating the last node’s pointer). Figure 4.6: Circular single linked list before insertion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 116 / 172
  • 117. Operations Algorithm 52 Insert at a Specific Location 1: procedure Insert at Location(start, x) 2: curr := start 3: node1 := new Node ▷ Create a new node named as node 4: if node1 = NULL then 5: write ”Out of memory space” and exit 6: end if 7: node1 → info := x ▷ Copies new data in to new node 8: node1 → next := NULL 9: read loc 10: if curr = NULL and loc > 1 then 11: write ”position not found” and exit 12: end if 13: count := 0 14: while curr → next ̸= start do Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 117 / 172
  • 118. Operations Algorithm 53 Insert at a Specific Location (Cont.) 15: count := count + 1 16: curr := curr → next 17: end while 18: count := count + 1 19: if count + 1 < loc then 20: write ”position not found” and exit 21: else if loc = 1 then 22: INSERT BEGIN(start, x) 23: else if count + 1 = loc then 24: INSERT END(start, x) 25: else 26: i := 1 27: curr := start 28: while i < loc do Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 118 / 172
  • 119. Operations Algorithm 54 Insert at a Specific Location (Cont.) 29: prev := curr 30: curr := curr → next 31: i := i + 1 32: end while 33: prev → next := node1 34: node1 → next := curr 35: end if 36: end procedure Figure 4.7: Circular single linked list after insertion (loc = 3, x = 66) Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 119 / 172
  • 120. Operations Insert After a Given Node To insert after a specific node in a circular linked list, the pointer of the new node must be adjusted to point to the node after the given node. The insertion operation involves updating pointers to maintain the circular structure while inserting the new node in the correct position. Insertion after a given node in a circular linked list may require special handling for inserting at the beginning or end of the list. Figure 4.8 and 4.9 illustrate the circular linked list before and after insertion, respectively. Figure 4.8: Circular single linked list before insertion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 120 / 172
  • 121. Operations Algorithm 55 Insert After a Given Node 1: procedure Insert After Node(start, x) 2: curr := start 3: if curr = NULL then 4: write ”item is not in the list” and exit 5: end if 6: node1 := new Node ▷ Create a new node named as node 7: if node1 = NULL then 8: write ”Out of memory space” and exit 9: end if 10: node1 → info := x ▷ Copies new data into new node 11: node1 → next := NULL 12: read item 13: count := 1 14: while curr → next ̸= start do Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 121 / 172
  • 122. Operations Algorithm 56 Insert After a Given Node (Cont.) 15: count := count + 1 16: curr := curr → next 17: end while 18: i := 1 19: curr := start 20: while i ≤ count and item ̸= curr → info do 21: i := i + 1 22: curr := curr → next 23: end while 24: if i = count then 25: INSERT END(start, x) 26: else if i > count then 27: write ”Item is not in the list” and exit 28: else Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 122 / 172
  • 123. Operations Algorithm 57 Insert After a Given Node (Cont.) 29: node1 → next := curr → next 30: curr → next := node1 31: end if 32: end procedure Figure 4.9: Circular single linked list after insertion (item = 22, x = 66) Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 123 / 172
  • 124. Operations Deletion from Beginning Figures 4.10a and 4.10b illustrate the circular list before and after deletion, respectively. (a) Circular single linked list before deletion. (b) Circular single linked list after deletion. Figure 4.10: Deletion from beginning. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 124 / 172
  • 125. Operations Algorithm 58 Deletion from Beginning 1: procedure Delete Begin(start) 2: temp := start ▷ temp points to the first node 3: node := start ▷ node points to the first node 4: if start = NULL then 5: write ”UNDERFLOW” and exit 6: else if start → next = start then 7: start := NULL and exit 8: else 9: while node → next ̸= start do 10: node := node → next 11: end while 12: start := start → next ▷ start points to next node 13: node → next := start 14: free temp ▷ Free space associated with temp 15: end if 16: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 125 / 172
  • 126. Operations Deletion at End Deleting at the end of a circular list involves updating the pointers of the last node and its predecessor. Deletion at the end of a circular list involves reconnecting the second last node to the first node. Figure 4.11: Circular single linked list before deletion Figure 4.12: Circular single linked list after deletion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 126 / 172
  • 127. Operations Algorithm 59 Deletion at End 1: procedure Delete End(start) 2: node := start ▷ node points to the first node 3: temp := start ▷ temp points to the first node 4: if start = NULL then 5: write ”UNDERFLOW” and exit 6: else if start → next = start then 7: start := NULL and exit 8: else 9: while node → next ̸= start do 10: temp := node 11: node := node → next 12: end while 13: temp → next := start ▷ Place 1st node addr. in next of temp 14: free node ▷ Free space associated with node 15: end if 16: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 127 / 172
  • 128. Operations Delete at a Specific Location Deleting a node at a specific location in a circular linked list involves reassigning pointers to maintain integrity. Identify the target node to delete by traversing the list until reaching the desired position. Adjust the pointers of neighboring nodes to bypass the node being deleted, effectively removing it from the sequence. Ensure proper handling of edge cases, such as deletion at the start or last of the circular linked list, to maintain consistency. Figure 4.13: Circular single linked list before deletion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 128 / 172
  • 129. Operations Algorithm 60 Delete at a Specific Location 1: procedure Delete at Location(start) 2: node := start ▷ node points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: read loc 7: count := 1 8: while node → next ̸= start do 9: count := count + 1 10: node := node → next 11: end while 12: if loc = 1 then 13: DELETE BEGIN(start) Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 129 / 172
  • 130. Operations Algorithm 61 Delete at a Specific Location (Cont.) 14: else if loc = count then 15: DELETE END(start) 16: else if loc > count then 17: write ”Position is not found” and exit 18: else 19: node := start 20: i := 1 21: while i < loc do 22: temp := node 23: node := node → next 24: i := i + 1 25: end while 26: temp → next := node → next ▷ Next node addr. in temp next Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 130 / 172
  • 131. Operations Algorithm 62 Delete at a Specific Location (Cont.) 27: free node ▷ Free space associated with node 28: end if 29: end procedure Figure 4.14: Circular single linked list after deletion (loc = 3). Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 131 / 172
  • 132. Operations Delete After a Given Node Deleting a node after a given node in a circular linked list involves updating the pointers to bypass the node to be deleted. To delete a node after a given node, we need to traverse the list until we find the given node. Once the given node is located, we adjust the pointers to skip the next node, effectively removing it from the list. Care must be taken to handle edge cases such as when the given node is the last node or when the list has only one node. Figure 4.15: Circular single linked list before deletion Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 132 / 172
  • 133. Operations Algorithm 63 Delete After a Given Node 1: procedure Delete After Node(start) 2: node := start ▷ node points to the first node 3: temp := start ▷ temp points to the first node 4: if start = NULL then 5: write ”UNDERFLOW” and exit 6: end if 7: read item 8: while node → next ̸= start and item ̸= node → info do 9: temp := node 10: node := node → next 11: end while 12: if node → next = start then 13: DELETE END(start) and exit 14: end if Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 133 / 172
  • 134. Operations Algorithm 64 Delete After a Given Node (Cont.) 15: temp := node 16: node := node → next 17: temp → next := node → next ▷ Next node addr. in temp next 18: free node ▷ Free space associated with node 19: end procedure Figure 4.16: Circular single linked list after deletion (item = 22). Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 134 / 172
  • 135. Operations Search in a Linked List Finding an item in a circular list is the definition of searching. Assume we have the linked list shown in Figure 4.17 and we need to look for one item, let’s say 44. The item’s storage location (in this case, number 4) will be returned if it is located in the list. Else, ”Element not found.” will be displayed. Algorithm 65 Search in a Linked List 1: procedure Search List(start) 2: node := start ▷ node points to the first node 3: if start = NULL then 4: write ”UNDERFLOW” and exit 5: end if 6: count := 1 7: read item Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 135 / 172
  • 136. Operations Algorithm 66 Search in a Linked List (Cont.) 8: while node → next ̸= start do 9: if item = node → info then 10: write ’Element Found at Location’, count and exit 11: end if 12: count := count + 1 13: node := node → next 14: end while 15: write ’Element not Found’ 16: end procedure Figure 4.17: Search an element 44. Output: Element Found at Location 4 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 136 / 172
  • 137. Operations Sort a Linked List Arranging the circular list elements in either ascending or descending order is known as sorting. Figure 4.18 and 4.19 exhibits the linked list before and after sorting respectively. Figure 4.18: Circular single linked list before sorting Algorithm 67 Sort a Linked List 1: procedure Sort Nodes(start) 2: node := start ▷ node points to the first node 3: while node → next ̸= start do 4: ptr := node → next 5: while ptr ̸= start do Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 137 / 172
  • 138. Operations Algorithm 68 Sort a Linked List (Cont.) 6: if node → info > ptr → info then ▷ If TRUE, Swap 7: temp := node → info 8: node → info := ptr → info 9: ptr → info := temp 10: end if 11: ptr := ptr → next 12: end while 13: node := node → next 14: end while 15: end procedure Figure 4.19: Circular single linked list after sorting Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 138 / 172
  • 139. Operations Reverse a Linked List Iterate through the list, reversing the direction of pointers between nodes. Update the start pointer to point to the last node encountered during traversal. Figure 4.20: Original linked list Algorithm 69 Reverse a Linked List 1: procedure Reverse List(start) 2: curr := start ▷ curr points to the first node 3: temp := start ▷ temp points to the first node 4: if curr → next = start then ▷ Check for single node linked list 5: exit Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 139 / 172
  • 140. Operations Algorithm 70 Reverse a Linked List (Cont.) 6: end if 7: prev := curr → next 8: while prev → next ̸= start do 9: ptr := prev → next 10: prev → next := curr 11: curr := prev 12: prev := ptr 13: end while 14: prev → next := curr 15: start := prev 16: temp → next := start 17: end procedure Figure 4.21: Reversed linked list Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 140 / 172
  • 141. Operations Merge Two Linked Lists Circular linked lists are merged by identifying the tail node of the first list and pointing it to the first node of the second list, and vice versa, creating a continuous chain. (a) Circular single linked list 1 (b) Circular single linked list 2 Figure 4.22: Circular single linked lists before merge operation. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 141 / 172
  • 142. Operations Algorithm 71 Merge Two Linked Lists 1: procedure Merge Lists(start := NULL, start1, start2) 2: node1 := start1 3: if start1 = NULL then 4: write ’Out of Memory Space’ and exit 5: end if 6: node := new Node 7: start := node 8: start → info := node1 → info 9: start → next := NULL 10: node1 := node1 → next 11: while node1 ̸= start1 do 12: curr := new Node 13: if curr = NULL then 14: write ’Out of Memory Space’ and exit 15: end if 16: curr → info := node1 → info 17: curr → next := NULL Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 142 / 172
  • 143. Operations Algorithm 72 Merge Two Linked Lists (Cont.) 18: node1 := node1 → next 19: node → next := curr 20: node := curr 21: end while 22: node2 := start2 23: curr := new Node 24: if curr = NULL then 25: write ’Out of Memory Space’ and exit 26: end if 27: curr → info := node2 → info 28: curr → next := NULL 29: node2 := node2 → next 30: node → next := curr 31: node := curr 32: while node2 ̸= start2 do 33: curr := new Node 34: if curr = NULL then Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 143 / 172
  • 144. Operations Algorithm 73 Merge Two Linked Lists (Cont.) 35: write ’Out of Memory Space’ and exit 36: end if 37: curr → info := node2 → info 38: curr → next := NULL 39: node2 := node2 → next 40: node → next := curr 41: node := curr 42: end while 43: node → next := start 44: end procedure Figure 4.23: Circular linked list after merging circular linked lists 1 and 2. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 144 / 172
  • 145. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 145 / 172
  • 146. Polynomial Representation A single variable polynomial equation can be generalized as follow. f (x) = n X i=0 ai xi (1) An example of a single variable polynomial equation is 2x5+4x3-9. The order of the polynomial equation is 5. Addition, subtraction, multiplication, division, differentiation, integration, etc. are the most common operations performed on polynomial equations. Hand-calculating polynomial operations can take a lot of time. E.g., d(5x7 + 9x5 − 3x4 + 13x2 − 4) dx = 35x6 + 45x4 − 12x3 + 26x (2) Polynomial ADTs can be implemented in two different ways. Arrays and Linked lists. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 146 / 172
  • 147. Polynomial Representation Array Implementation: f1(x) = 3x3 + 8x2 + 5x − 9, f2(x) = −4x5 + 7x2 + 10, f3(x) = −10x14 + 8x2 − 12x (3) (a) f1(x) (b) f2(x) (c) f3(x) Figure 5.1: Representation of polynomial equations using arrays. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 147 / 172
  • 148. Polynomial Representation Array Implementation: Indices and their values represent exponents and coefficients. This representation of a sparse polynomial equations causes a waste of memory space as shown in Figure 5.1c Advantages Good for non-sparse polynomial equations. Easy for storage and retrieval. Disadvantages Array size allotted during compilation time causes the storage of polynomial equations up to a certain order. Wastage of space and runtime in the case of the array implementation of sparse polynomial equations. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 148 / 172
  • 149. Polynomial Representation Linked List Implementation: f1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3 f2(x) = 4x6 + 10x4 + 12x + 8 (4) (a) f1(x) (b) f2(x) Figure 5.2: Representation of polynomial equations using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 149 / 172
  • 150. Polynomial Representation Linked List Implementation: A node represents a term, containing fields for coefficient, exponent, and a pointer to the next term in the polynomial. If the coefficients of the polynomial are floating-point numbers, the data type used in the term struct should be adjusted accordingly. Advantages Make it easy to maintain and save space (you don’t need to worry about sparse polynomials). Declaring nodes just when necessary, and there is no requirement to set a list size. Disadvantages Not able to traverse the list in reverse. Not able to go from the end of the list to the beginning. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 150 / 172
  • 151. Polynomial Representation Linked List Implementation: Node Structure f (x) = anxn + an−1xn−1 + an−2xn−2 + ... + a2x2 + a1x + a0 (5) struct PloyNode{ int coef; int exp; struct PolyNode *next; }; typedef struct PolyNode *polynode; If the coefficients of the polynomial are floating-point numbers, the data type used in the struct PolyNode should be adjusted accordingly. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 151 / 172
  • 152. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 152 / 172
  • 153. Polynomial Addition Using a linked list, add the two polynomial equations, f1 and f2, and store the result in the f3 linked list. In order to accomplish this, we must break down the process cases: Case 1: exponent of node from f1 = exponent of node from f2 Create a node in f3 with the same exponent and with the sum of the coefficients of f1 and f2 nodes. Case 2: exponent of node from f1 > exponent of node from f2 Copy node of f1 to end of f3. Case 3: exponent of node from f1 < exponent of node from f2 Copy node of f2 to end of f3 Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 153 / 172
  • 154. Polynomial Addition f1(x) = 3x8 + 14x5 + 7 (6) (a) f1(x) f2(x) = 6x8 − 9x3 + 5x2 (7) (b) f2(x) Figure 5.3: Representation of polynomial equations using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 154 / 172
  • 155. Polynomial Addition Step - 1 (a) Input polynomial equations (a → exp = 8, b → exp = 8). (b) Addition as per case 1 (d → coef = 9, d → exp = 8). Figure 5.4: Polynomial addition using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 155 / 172
  • 156. Polynomial Addition Step - 2 (a) Input polynomial equations (a → exp = 5, b → exp = 3) (b) Next term as per case 2 (d → coef = 14, d → exp = 5). Figure 5.5: Polynomial addition using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 156 / 172
  • 157. Polynomial Addition Step - 3 (a) Input polynomial equations (a → exp = 0, b → exp = 3) (b) Next term as per case 3 (d → coef = -9, d → exp = 3). Figure 5.6: Polynomial addition using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 157 / 172
  • 158. Polynomial Addition Step - 4 (a) Input polynomial equations (a → exp = 0, b → exp = 2) (b) Next term based on case 3 (d → coef = 5, d → exp = 2). Figure 5.7: Polynomial addition using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 158 / 172
  • 159. Polynomial Addition Step - 5 (a) Input polynomial equations (a → exp = 0, b = NULL) (b) Remaining term in f1 added at the end of f3. Figure 5.8: Polynomial addition using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 159 / 172
  • 160. Polynomial Addition Step - 5 (a) Input polynomial equations (a → exp = 0, b = NULL) (b) Remaining term in f1 added at the end of f3. Figure 5.9: Polynomial addition using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 160 / 172
  • 161. Polynomial Addition Algorithm 74 Addition of Polynomials 1: procedure Polynomial Addition(f 1, f 2) 2: pptr := f 1 3: qptr := f 2 4: f 3 := new PolyNode ▷ Create a header node 5: f 3 → next := NULL 6: rptr := f 3 7: while pptr ̸= NULL and qptr ̸= NULL do 8: if pptr → exp = qptr → exp then 9: rptr := ADDITION(pptr, qptr, rptr) 10: pptr := pptr → next 11: qptr := qptr → next Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 161 / 172
  • 162. Polynomial Addition Algorithm 75 Addition of Polynomials (Cont.) 12: else if pptr → exp > qptr → exp then 13: rptr := ADDITION(pptr, NULL, rptr) 14: pptr := pptr → next 15: else if pptr → exp < qptr → exp then 16: rptr := ADDITION(NULL, qptr, rptr) 17: qptr := qptr → next 18: end if 19: end while 20: while pptr ̸= NULL do 21: rptr := ADDITION(pptr, NULL, rptr) 22: pptr := pptr → next 23: end while Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 162 / 172
  • 163. Polynomial Addition Algorithm 76 Addition of Polynomials (Cont.) 24: while qptr ̸= NULL do 25: rptr := ADDITION(NULL, qptr, rptr) 26: qptr := qptr → next 27: end while 28: temp := f 3 29: f 3 := f 3 → next 30: free temp 31: return f 3 32: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 163 / 172
  • 164. Polynomial Addition Algorithm 77 Addition Function 1: procedure Addition(pptr, qptr, rptr) 2: if pptr ̸= NULL and qptr ̸= NULL then 3: node := new PolyNode 4: rptr → next := node 5: rptr := node 6: rptr → coef := pptr → coef + qptr → coef 7: rptr → exp := pptr → exp 8: rptr → next := NULL 9: return rptr 10: else if pptr ̸= NULL then 11: node := new PolyNode 12: rptr → next := node 13: rptr := node Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 164 / 172
  • 165. Polynomial Addition Algorithm 78 Addition Function (Cont.) 14: rptr → coef := pptr → coef 15: rptr → exp := pptr → exp 16: rptr → next := NULL 17: return rptr 18: else if qptr ̸= NULL then 19: node := new PolyNode 20: rptr → next := node 21: rptr := node 22: rptr → coef := qptr → coef 23: rptr → exp := qptr → exp 24: rptr → next := NULL 25: return rptr 26: end if 27: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 165 / 172
  • 166. Outline 1 Linked List Introduction Linked List Types 2 Single Linked List Static Implementation Operations Dynamic Implementation Operations 3 Double Linked List Dynamic Implementation Operations 4 Circular Singly Linked List Dynamic Implementation Operations 5 Applications Polynomial Representation Polynomial Addition Polynomial Multiplication Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 166 / 172
  • 167. Polynomial Multiplication Let n and m be the number of terms in the two polynomials. A polynomial with nxm terms will be produced by this technique. For instance, we can obtain the following linked list after multiplying 2 − 4x + 5x2 and 1 + 2x − 3x3: (a) Linked list with all terms. (b) Final multiplication result. Figure 5.10: Polynomial multiplication using linked list. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 167 / 172
  • 168. Polynomial Multiplication Algorithm 79 Multiplication of Polynomials 1: procedure Poly Mult(start1, start2) 2: start := NULL 3: tail := NULL 4: p1 := start1 5: while p1 ̸= NULL do 6: p2 := start2 7: while p2 ̸= NULL do 8: exp := p1 → exp + p2 → exp 9: coef := p1 → coef ∗ p2 → coef 10: tail := APPEND(tail, exp, coef ) 11: if start = NULL then 12: start := tail 13: end if 14: p2 := p2 → next 15: end while 16: p1 := p1 → next 17: end while Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 168 / 172
  • 169. Polynomial Multiplication Algorithm 80 Multiplication of Polynomials (Cont.) 18: start := MERGESORT(start) 19: MERGELIKETERMS(start) 20: return start 21: end procedure Algorithm 81 Append a New Polynomial Node 1: procedure append(tail, exp, coef ) 2: node := new PolyNode 3: node → exp := exp 4: node → coef := coef 5: node → next := NULL 6: if tail ̸= NULL then 7: tail → next := node 8: end if 9: return node 10: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 169 / 172
  • 170. Polynomial Multiplication Algorithm 82 Merge Like Terms 1: procedure MergeLikeTerms(start) 2: prev := NULL, p1 := start 3: while p1 ̸= NULL do 4: p2 := p1 → next 5: while p2 ̸= NULL and p2 → exp = p1 → exp do 6: p1 → coef := p1 → coef + p2 → coef 7: p2 := p2 → next 8: p1 → next := p2 9: end while 10: if p1 → coef = 0 and prev ̸= NULL then 11: prev → next := p2 12: else 13: prev := p1 14: end if 15: p1 := p2 16: end while 17: end procedure Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 170 / 172
  • 171. Summary Here, we have discussed Introduction to linked list data structure and its types. Static implementation of single linked list ADT and its operations. Dynamic implementation of single linked list ADT and its operations. Dynamic implementation of doubly linked list ADT and its operations. Dynamic implementation of circular linked list ADT and its operations. Polynomial addition using single linked lists. Polynomial multiplication using single linked lists. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 171 / 172
  • 172. For Further Reading I M. A. Weiss Data Structures and Algorithm Analysis in C (2nd edition). Pearson India, 2022. E. Horowitz, S. Sahni and S. A. Freed. Fundamentals of Data Structures in C (2nd edition). Universities Press, 2008. A. K. Rath and A. K. Jagadev. Data Structures Using C (2nd edition). Scitech Publications, 2011. Dr. Ashutosh Satapathy Linked List Data Structures August 24, 2024 172 / 172