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

Linked List Applications

The document describes Josephus's problem, which involves N people standing in a circle to be executed. Starting from some point in the circle, every Mth person is executed as you go around the circle until only one person remains. The problem is to determine the position you should start in to be the last surviving person. An example with N=5 and M=3 is given. An algorithm is then presented to solve the problem using a circular linked list, where each node stores a person's position number. The algorithm involves creating the linked list, then traversing it and removing the Mth node each iteration until one node remains, whose position is the solution.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Linked List Applications

The document describes Josephus's problem, which involves N people standing in a circle to be executed. Starting from some point in the circle, every Mth person is executed as you go around the circle until only one person remains. The problem is to determine the position you should start in to be the last surviving person. An example with N=5 and M=3 is given. An algorithm is then presented to solve the problem using a circular linked list, where each node stores a person's position number. The algorithm involves creating the linked list, then traversing it and removing the Mth node each iteration until one node remains, whose position is the solution.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Josephus’s Problem

In Josephus’s problem, N people are standing in a circle waiting to get


executed. The counting begins at some point in the circle and proceeds
around the circle in a fixed direction. In each step, a certain number of
people are skipped, and the next person is executed. The elimination
proceeds around the circle (which is becoming smaller and smaller as the
executed people are removed), until only the last person remains, who is
given freedom.

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

Let us understand the problem with the help of an example:


Let’s take an input, where N = 5 and M = 3.
Approach of Josephus’s Problem Using Circular Linked List

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.

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.
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

Output: 5x2-1x1-3x0 Output: 5x3 + 4x2 + 5x1 - 3x0


struct node
{
float coef;
int expo;
struct node *link;
};
int main()
{
struct node *start1 = NULL, *start2 = NULL, *start3 = NULL;
printf(“Enter Polynomial 1”);
start1 = create(start1);

printf(“Enter Polynomial 2”);


start2 = create(start2);

// Display polynomials List


printf("\n polynomial 1 is: ");
display(start1);

printf("\n polynomial 2 is: ");


display(start2);

// Function add two polynomial numbers


poly_add(start1, start2);

// Function multiply two polynomial numbers


poly_multi(start1, start2);
return 0;
}
// Function to create a new node
struct node *create(struct node *start)
{
float coeff;
int expo, i, n;
printf(“\nEnter the number of terms you want”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“/nEnter the coefficient and exponent of %d term:”, i);
scanf(“%d%d”, &coeff, &expo);
start = insert_s(start, coeff, expo);
}
return start;
}
// Function to insert new node
struct node *insert_s(struct node *start, float co, int ex)
{
struct node *tmp, *ptr;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->coef = co;
tmp-> expo = ex;
if(start = =NULL || ex > start->expo) /* If list is empty or exp greater than first node */
{
tmp->link = start;
start = tmp;
}
else
{
ptr = start;
while(ptr->link !=NULL && ptr->link->expo >= ex)
ptr = ptr->link;
tmp->link = ptr->link;
ptr->link = tmp;
}
return start;
}
// Function to insert new node
struct node *insert(struct node *start, float co, int ex)
{
struct node *tmp, *ptr;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->coef = co;
tmp-> expo = ex;
if(start = =NULL) /* If list is empty */
{
tmp->link = start;
start = tmp;
}
else /* insert at the end of the list*/
{
ptr = start;
while(ptr->link !=NULL)
ptr = ptr->link;
tmp->link = NULL; */ or tmp->link = ptr->link
ptr->link = tmp;
}
return start;
}
// Function to insert new node (SKIP)
struct node *insert(struct node *start, float co, int ex)
{
struct node *tmp, *q;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->coef = co;
tmp-> expo = ex;
if(start = =NULL || ex > start->expo) /* If list is empty i.e. inserting first term*/
{
tmp->link = start;
start = tmp;
}
else /* Insert a per appropriate position*/
{
q = start;
while(q!=NULL)
{
if(q->link = = NULL)
{
q->link = temp;
tmp -> link = NULL;
}
else
{
if(ex < q->expo && ex > q->link->expo)
{
tmp->link = q->link;
q->link = tmp;
}
}
q= q->link;
}
}
return start;
}
// Display Linked list
void display(struct node* ptr)
{
if(ptr = = NULL)
{
printf(“Zero polynomial”);
return;
}
while (ptr!= NULL)
{
printf("%.1fx^%d", ptr->coef, ptr->expo);
ptr = ptr->link;
if (ptr!=NULL)
printf("+");
else
printf(“\n”);
}
}
// Addition of two polynomials
void poly_add(struct node *p1, struct node *p2)
{
struct node *start3;
start3 = NULL:
while (p1 != NULL && p2 != NULL)
{
if(p1->expo > p2->expo)
{
start3 = insert(start3, p1->coef, p1->expo);
p1 = p1->link;
}
else if(p2->expo > p1->expo)
{
start3 = insert(start3, p2->coef, p2->expo);
p2 = p2->link;
}
else if(p1->expo == p2->expo)
{
start3 = insert(start3, p1->coef+p2->coef, p1->expo);
p1 = p1->link;
p2 = p2->link;
}
}
while (p1 != NULL) /*If poly2 has finished and elements left in poly1*/
{
start3 = insert(start3, p1->coef, p1->expo);
p1 = p1->link;
}
while (p2 != NULL) /*If poly2 has finished and elements left in poly1*/
{
start3 = insert(start3, p2->coef, p2->expo);
p2 = p2->link;
}
printf(“Added Polynomial is : );
display(start3);
}
Multiplication of two Polynomials using Linked List
Input: Poly1: 3x^2 + 5x^1 + 6,
Poly2: 6x^1 + 8
Output: 18x^3 + 54x^2 + 76x^1 + 48
On multiplying each element of 1st polynomial with elements of 2nd polynomial,
we get:
18x^3 + 24x^2 + 30x^2 + 40x^1 + 36x^1 + 48

On adding values with same power of x,


18x^3 + 54x^2 + 76x^1 + 48
// Multiplication of two polynomials
void poly_multi(struct node *p1, struct node *p2)
{
struct node *start3;
struct node *p2_beg = p2;
start3 = NULL:
if (p1 != NULL && p2 != NULL)
{
printf(“Multiplied Polynomial is zero polynomial \n: );
return;
}
while (p1 != NULL)
{
p2 = p2_beg;
while (p2 != NULL)
{
start3 = insert_s(start3, p1->coef * p2->coef, p1->expo+ p2->expo);
p2 = p2->link;
}
p1 = p1->link;
}
display(start3);
}
Sparse Matrices

sparse … many elements are zero


dense … few elements are zero

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

int sparseMatrix[5][5] = row 1 1 2 2 4 4


{ column 3 5 3 4 2 3
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 }, value 3 4 5 7 2 6
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }, int compactMatrix[3][size];
{0 , 0 , 0 , 0 , 0 },
};
Linear list Representation of Sparse Matrices

Single linear list in row-major order.

• Scan the nonzero elements of the sparse matrix in row-major order


• Each nonzero element is represented by a triple
(row, column, value)

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

•The advantage of using a linked list to represent the sparse


matrix is that the complexity of inserting or deleting a node in
a linked list is less than the array.
Single Linear List
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

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

00304 row1 = [(3, 3), (5,4)]


00570 row2 = [(3,5), (4,7)]
00000 row3 = []
02600 row4 = [(2,2), (3,6)]
Array of Row Chains

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 to represent triples null


struct value_list
{
row[] 2 2 3 6
int column_index;
int value;
struct value_list *next;
};
Orthogonal List Representation
or Dictionary List

Both row and column lists.

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[]

May use circular lists instead of chains.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy