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

Singly Linked List Assignment

The document appears to be an assignment submission for a data structures and algorithms course. It includes 5 questions related to linked lists and the student's answers. The questions cover topics like tracing the output of a recursive function on a linked list, reversing singly and doubly linked lists, and modifying a linked list by moving the last element to the front.

Uploaded by

ABESH Meena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Singly Linked List Assignment

The document appears to be an assignment submission for a data structures and algorithms course. It includes 5 questions related to linked lists and the student's answers. The questions cover topics like tracing the output of a recursive function on a linked list, reversing singly and doubly linked lists, and modifying a linked list by moving the last element to the front.

Uploaded by

ABESH Meena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Indian Institiute of Information Technology, Bhopal

Assignment: Singly Link List


Data Structure & Algorithms (CS-102)

ECE
2nd Semester

Submitted by: Submitted to:


Abesh Kumar meena Dr. Dharmendra Dangi
23U01071 Assistant Professor
ECE IIIT BHOPAL
1. Trace the output of the following code:

void fun2(struct Node* head)

if(head == NULL)

return;

printf("%d ", head->data);

if(head->next!= NULL)

fun2(head->next->next);

printf("%d ", head->data);

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.

This process continues until it reaches the end of the list.

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?

void fun1(struct node* head)

if(head == NULL)

return;

fun1(head->next);

printf("%d ", head->data);

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.

void fun(struct node **head_ref)

struct node *temp = NULL;

struct node *current = *head_ref;

while (current != NULL)

temp = current->prev;

current->prev = current->next;

current->next = temp;

current = current->prev;

if(temp != NULL )

*head_ref = temp->prev;

Assume that reference of head of following doubly linked list is passed to

above function 1 <–> 2 <–> 3 <–> 4 <–> 5 <–>6. What should be the

modified linked list after the function call?

Answer-

The given function is designed to reverse a doubly linked list.

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:

Initially: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6

After the calling the function:


During the iteration:

temp = NULL, current = 1

Swap prev and next of node 1: NULL <- 1 -> 2

Move current to node 2

temp = 1, current = 2

Swap prev and next of node 2: 1 <- 2 -> 3

Move current to node 3

Continue this process until reaching node 6

After the loop:

temp = 5 (last node), current = NULL

Therefore, after the function call, the modified linked list will be:

6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1

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 ?

void fun(struct node* start)

if(start == NULL)

return;

printf("%d ", start->data);

if(start->next != NULL )

fun(start->next->next);

printf("%d ", start->data);

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?

typedef struct node {

int value;

struct node* next;

} Node;

Node* move_to_front(Node* head) {

Node *p, *q;

if ((head == NULL: || (head->next == NULL))

return head;

q = NULL; p = head;

while (p-> next !=NULL) {

q = p;

p = p->next; }

return head; }

Answer-

Node* move_to_front(Node* head) {

Node *p, *q;

if ((head == NULL: | (head->next == NULL))

return head;

q =NULL;

p=head;

while (p-> next!=NULL) {

q =p;

p =p->next; }
q->next = p->next;

p->next = head;

head = p;

return head;

----------------------------------------------------------------------------------------------------------------------------- -------

You might also like