Lab Assignment 2 (Linklist)
Lab Assignment 2 (Linklist)
1.
2.
3.
4.
5.
Print all the elements at the index of multiples of k with the first element assumed to have an index of
0. Do this for a single pass of the linked list.
Input:
k=3
12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output:
12 -> 17 -> 22 -> NULL
6.
Delete duplicate elements from a given linked list. Retain the earliest entries.
Input:
20 -> 18 -> 15 -> 20 -> 6 -> 18 -> 5 -> 3 -> NULL
Output:
20 -> 18 -> 15 -> 6 -> 5 -> 3 -> NULL
7.
8.
Remove in a linked list all the nodes that have a greater value to their right. Input:
10 -> 12 -> 15 -> 20 -> 5 -> 16 -> 25 -> 8 -> NULL
10 -> 12 -> 15 -> 20 -> 25 -> 26 -> 30 -> 40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL Output:
20 -> 25 -> 8 -> NULL
40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL
9.
Perform polynomial addition on two such polynomials represented by linked lists and generate a
new list representing the sum of those polynomials.
Input:
+------+------+
+------+------+
|
2 | 2
| -> | 3
| 1
| ->
+------+------+
+------+------+
+------+------+
+------+------+
|
1 | 3
| -> | 2
| 1
| ->
+------+------+
|
4 | 0
| -> NULL
+------+------+
+------+------+
|
1 | 0
| -> NULL
+------+------+
+------+------+
+------+------+ Output:
+------+------+
+------+------+
+------+------+
+------+------+
|
1 | 3
| -> | 2
| 2
| -> |
5 | 1
| -> |
5 |
0 | -> NULL
+------+------+
+------+------+
+------+------+
+------+------+
15. Create a linked list in which each node has two pointers: FRIEND and NEXT. FRIEND can point to any
other node in the linked list. Make a duplicate linked list with the same values and connections.
Input: (original linked list)
5 -> 1; 1 -> 4; 4 -> 2; 2 -> 5; 5 -> 5
________________________
|
___________ |
___________
|
|
| |
|
|
|
v
| v
|
v
+------+
+------+
+------+
+------+
+------+
|
5 | -> |
4 | -> |
1 | -> |
2 | -> |
5 | -> NULL
+------+
+------+
+------+
+------+
+------+
^
|
^
|
|
|________________________|
|
|_______________________________________________|