Singly Linked List Assignment
Singly Linked List Assignment
ECE
2nd Semester
if(head == NULL)
return;
if(head->next!= NULL)
fun2(head->next->next);
Answer -
To trace the output of the given code, let's consider a linked list with values 1->2->3->4->5.
Initially, the function fun2 is called with the head pointing to the first node (1).
The function checks if the head is not NULL and prints the data of the current node (1).
It then checks if the next node exists and calls fun2 recursively with head->next->next, skipping one
node.
Once it reaches the end, it starts printing the data of each node in reverse order as it backtracks
through the recursive calls.
The output trace for this code with a linked list 1->2->3->4->5 would be: 1 3 5 5 3 1.
----------------------------------------------------------------------------------------------------------------------------- --------
2. What does the following function do for a given Linked List with first node as head?
if(head == NULL)
return;
fun1(head->next);
Answer –
The given function fun1 recursively traverses a linked list starting from the head node and prints the
data of each node in reverse order. Here's how it works:
If the current node (head) is NULL (end of the list), the function returns.
Otherwise, it recursively calls fun1 with the next node (head->next), effectively moving to the end of
the list.
Once it reaches the end of the list, it starts printing the data of each node as it backtracks through
the recursive calls, effectively reversing the order of output.
Therefore, for a given Linked List with the first node as head, this function will print the data of
each node in reverse order.
----------------------------------------------------------------------------------------------------------------------------- --------
3. Consider the following function that takes a reference to head of a Doubly Linked List as a
parameter. Assume that a node of a doubly linked list has the previous pointer as Prev and the
next pointer as next.
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
if(temp != NULL )
*head_ref = temp->prev;
above function 1 <–> 2 <–> 3 <–> 4 <–> 5 <–>6. What should be the
Answer-
It initializes two pointers, temp and current, where temp is set to NULL and current is set to the head
of the linked list.
It then iterates through the linked list, swapping the previous and next pointers of each node.
After the loop, it checks if temp is not NULL and updates the head pointer to point to the last node
(temp->prev).
Applying this function to the provided doubly linked list we get: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6:
temp = 1, current = 2
Therefore, after the function call, the modified linked list will be:
This result demonstrates that the function successfully reversed the original doubly linked list.
----------------------------------------------------------------------------------------------------------------------------- --------
4. What is the output of following function in which start is pointing to the first node of the
following linked list 1->2->3->4->5->6 ?
if(start == NULL)
return;
if(start->next != NULL )
fun(start->next->next);
Answer -
The output of the given function, where start points to the first node of the linked list 1->2->3->4-
>5->6, will be: 1 3 5 5 3 1.
The function prints the data of each node in the list in a specific order due to the recursive call
fun(start->next->next), which skips every other node during the recursive call. This leads to the
pattern observed in the output.
----------------------------------------------------------------------------------------------------------------------------- --------
5. The following C function takes a simply-linked list as input argument. It modifies the list by
moving the last element to the front of the list and returns the modified list. Fill up the blank
space?
int value;
} Node;
return head;
q = NULL; p = head;
q = p;
p = p->next; }
return head; }
Answer-
return head;
q =NULL;
p=head;
q =p;
p =p->next; }
q->next = p->next;
p->next = head;
head = p;
return head;
----------------------------------------------------------------------------------------------------------------------------- -------