Assignment 1
Assignment 1
Figure 1: Usual structure of a doubly linked list with two pointers per node.
Let us tweak this basic structure of a doubly linked list as follows. Instead of storing two pointers per
node, let us store only XOR of these two pointers in every node thereby saving (in every node) the space
required to store one pointer. Please refer to ?? for a pictorial overview.
Figure 2: Modified structure of a doubly linked list with one pointer per node.
The XOR function is defined in the following manner. Let X = (x1 , x2 , . . . , xn ) and
Y = (y1 , y2 , . . . , yn ) be two Boolean strings of length n bits each. Then X XOR Y =
(x1 XOR y1 , x2 XOR y2 , . . . , xn XOR yn ) where 0 XOR 0 = 0, 1 XOR 1 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1.
One can easily verify the following properties of the XOR function.
B X XOR Y = Y XOR X
B X XOR X = 0̄
Convince yourself that all the usual operations on a doubly linked list can still be carried out correctly
in our modified representation. In this exercise, you first take the length n of a doubly linked list from the
1
user. Then create a doubly linked list of length n with random data (integers). Needless to say that you
should only store the XOR of the addresses of the previous and the next node in every node as discussed
above. For every doubly linked list, you maintain a head and a tail pointer pointing respectively to the
first and the last node of the list. In this doubly linked list, you perform the following operations:
1. Traverse the doubly linked list both from the front to the end and from the end to the front and
print data values in that order. The prototype of your functions should be as follows.
2. Reverse your doubly linked list. Once the doubly linked list is created, you SHOULD NOT create
any new node and change data field of any node. The prototype of your function should be as
follows.
void reverse(struct node **head, struct node **tail);
Observe that you need to pass double pointers here since the doubly linked list is going to change
unlike traversal functions.
3. Sort the doubly linked list in non-decreasing order of data values. Once the doubly linked list
is created, you SHOULD NOT create any new node and change data field of any node. The
prototype of your function should be as follows.
Here too you need to pass double pointers for the same reason.
Sample Output
n = 10
Doubly linked list traversed from front to end: 3, 10, −12, 34, −10, −50, 25, 1, −18, −71
Doubly linked list traversed from end to front: − 71, −18, 1, 25, −50, −10, 34, −12, 10, 3
Reversed doubly linked list traversed from front to end: − 71, −18, 1, 25, −50, −10, 34, −12, 10, 3
Sorted doubly linked list traversed from front to end: − 71, −50, −18, −12, −10, 1, 3, 10, 25, 34