Linked List Applications
Linked List Applications
Given the total number of persons N and the number M, which indicates that
M-1 persons are skipped and the Mth person is killed in the circle, your task
is to choose the place in the initial circle so that you are the last one
remaining (survive).
Understanding of Josephus Problem with Example
We will create a circular linked list of size N, and each node will contain
its position number in the list as its data.
Then, we will traverse the list and, every time delete the Mth node till we
are left with only one node.
The left node at last is our solution to the problem.
Algorithm of Josephus Problem using Circular Linked List
1. Create a new node and store its address in the head and also initialize
a prev variable with the head.
2. Run a loop and create a linked list of size N, attaching the nodes created in each
iteration to the end of prev and then update the prev.
3. After the loop is executed, connect the last node with the head of the list,
i.e., prev->next = head.
c. Inside the loop, copy ptr2 in ptr1, move ptr1 forward by one node and
increase count by one.
d. After the inner loop ends, remove the Mth node, i.e., ptr2->next = ptr1->next.
6. After the loop ends, print the data of the last node left, i.e., data of ptr1.
Algorithm of Josephus Problem using Circular Linked List
1. Create a new node and store its address in the head and also initialize
a prev variable with the head.
2. Run a loop and create a linked list of size N, attaching the nodes created in each
iteration to the end of prev and then update the prev.
3. After the loop is executed, connect the last node with the head of the list,
i.e., prev->next = head.
4. Initialize two pointers ptr1 and ptr2 with head.
5. Run a while loop till the next node of ptr1 is ptr1 itself and inside the loop,
a) Initialize variable count with 1.
b) Run a while loop till the count is not equal to M.
c) Inside the loop, copy ptr2 in ptr1, move ptr1 forward by one node and
increase count by one.
d) After the inner loop ends, remove the Mth node, i.e., ptr2->next = ptr1-
>next.
e) Update ptr1 with ptr2->next.
6. After the loop ends, print the data of the last node left, i.e., data of ptr1.
Polynomials
Polynomial is a mathematical expression that consists of variables and
coefficients.
Polynomials Representation
Adding two Polynomials using Linked List
Input: 1st number = 5x2 + 4x1 + 2x0 Input: 1st number = 5x3 + 4x2 + 2x0
2nd number = -5x1 - 5x0 2nd number = 5x1 - 5x0
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
Representation of Sparse Matrices
• There are two ways to represent the sparse matrix that are listed as
follows :
➢ Array representation
➢ Linked list representation
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
Array Representation
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
Linked List Representation
Node structure:
row col
value next
row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6
1 3 1 5 2 3 2 4 4 2 4 3
3 4 5 7 2 6 null
firstNode
One Linear List Per Row
Node structure:
next
col value
Array Of Row Chains
00304
00570 null
00000 3 3 5 4
02600 null
3 5 4 7
// Node to represent row - list
struct row_list
{
int row_number;
struct row_list *link_down; null
struct value_list *link_right;
};
Node structure:
row col value
down next
Row Lists
1 3 3 1 5 4
n
00304 2 3 5 2 4 7
00570 n
00000
null
02600
4 2 2 4 3 6
n
Column Lists
1 3 3 1 5 4
00304 n
00570 2 3 5 2 4 7
00000
02600
4 2 2 4 3 6
n n
Orthogonal Lists
1 3 3 1 5 4
00304 n n
00570 2 3 5 2 4 7
n
00000
02600 null
4 2 2 4 3 6
n n n
row[]