2. Polynomial Expression using Linked
List
• linked list is a data structure that stores each
element as an object in a node of the list.
• Polynomial is a mathematical expression that
consists of variables and coefficients.
• For example: 5x^2 - 4x + 7
• 3x+ 3
• result = 5x^2-x+10
3. Polynomial Expression using Linked
List
• In the Polynomial linked list, the coefficients and
exponents of the polynomial are defined as the
data node of the list.
• Implementation of polynomial Expression,
struct node
{ int power;
float coef;
struct node *next;
};
7. Adding two polynomials using Linked List
Algorithm:
• Create Linked polynomials
• Read the co-efficient and exponent values
• Set the pointers list1 and list2 to both the polynomials
to traverse them.
• Start from the first node and compare the exponent of
polynomials.
• If(list1->exp == list2-> exp)
list1->coef= list1->coef + list2->coef
list3->exp= list1->exp
(move your pointer to next node)
8. Adding two polynomials using Linked List
Algorithm:
• If(list1->exp == list2-> exp)
list1->coef= list1->coef + list2->coef
list3->exp= list1->exp
(move both the pointer to next node)
• If(list1->exp > list2-> exp)
list3->coef= list1->coef
list3->exp= list1->exp
(move list1 pointer to next node)
• If(list1->exp < list2-> exp)
list3->coef= list2->coef
list3->exp= list2->exp
(move list2 pointer to next node)
9. Josephus Circle using circular linked list
• There are n people standing in a circle waiting to be executed.
• The counting out 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 a number m which
indicates that m-1 persons are skipped and m-th person is killed in
circle.
• The task is to choose the place in the initial circle so that you are
the last one remaining and so survive.
.
10. Josephus Circle using circular linked list
Algorithm:
1) Create a circular linked list of size n.
2) Traverse through linked list and one by
one delete every m-th node until there is one
node left.
3) Return value of the only left node.
11. Josephus Circle using circular linked list
Description:
• The Josephus problem involves n people standing in a
circle. Each person kills the next person until there is
only one person remaining. We will use a circular
linked list to solve the Josephus problem.
For example: n = 6,
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 1
• Node 1 kills node 2. Node 3 kills node 4. Node 5 kills
node 6.
1 -> 3 -> 5 -> 1
• Node 1 kills node 3. Node 5 kills node 1.
5 ->
• Therefore 5 is the last person remaining.
12. Josephus Circle using circular linked list
Example 1: n = 10, Rotate the list to the left by
one position and then delete the first element.
Find who is the lone survivor.
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7-> 8 -> 9 -> 10
13. Josephus Circle using circular linked list
Example 1: n = 10
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7-> 8 -> 9 -> 10