edited by
10,808 views
41 votes
41 votes

The following C function takes a singly-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. Some part of the code is left blank.

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;  
    
}    

Choose the correct alternative to replace the blank line.

  1. $q=NULL; p \rightarrow next = head; head = p$;
  2. $q \rightarrow next = NULL; head = p; p \rightarrow next = head$;
  3. $head = p; p \rightarrow next =q; q \rightarrow next = NULL$;
  4. $q \rightarrow next = NULL; p \rightarrow next = head; head = p$;
edited by

2 Answers

11 votes
11 votes

after the last iteration of the while loop, here's how the condition look like :

on selecting option D; we serve the purpose of moving the last node to the front of the linked list. this is how it works:

its first statement makes $q$ the last node
second statement makes the last node $p$ points to the first node
last statement declares $p$ as the $head$ of the linked list.

Answer:

Related questions

73 votes
73 votes
10 answers
1
go_editor asked Apr 21, 2016
26,930 views
A hash table of length $10$ uses open addressing with hash function $h(k) = k \: \mod \: 10$, and linear probing. After inserting $6$ values into an empty hash table, the...
23 votes
23 votes
3 answers
2
go_editor asked Sep 30, 2014
6,582 views
A hash table of length $10$ uses open addressing with hash function $h(k) = k \mod 10$, and linear probing. After inserting $6$ values into an empty hash table, the table...
57 votes
57 votes
12 answers
3
go_editor asked Sep 29, 2014
16,073 views
In a binary tree with $n$ nodes, every node has an odd number of descendants. Every node is considered to be its own descendant. What is the number of nodes in the tree ...
55 votes
55 votes
8 answers
4