Link List Data Structure Mock Test – 1


Question 1
The function DELETE(head, element) is used to delete a node from the linked list by finding the node value with a given element. The parameter head is the first node of the list. Find the missing statements XX and YY in the following "DELETE" function to delete the node?
(Assume all elements are distinct in the list and the function returns pointers that points to the first node of the list.)
Node DELETE (Node head, int element)
{
    Node x=head;
    if(x.data==element )
    return head.next;
    while(x.next!=NULL)
    {
        if(_XX__)
        {
            __YY__
            return head;
        }
        x=x.next;
    }
} 
A
XX : x.data == element
YY : x.next = x.next.next
B
XX : x.next.data == element
YY : x.next = x.next.next
C
XX : x.data == element
YY : x.next.next = x.next
D
XX : x.next.data == element
YY : x.next.next = x.next
   Data Structure
Question 1 Explanation: 
Consider that we want to delete node with value 99.

 if(x.next.data==99)   //XX
        {
            x.next=x.next.next;    //YY
            return head;
        } 


Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 2
Identify the functionality of the following code when function return the value 1.
("head" pointer point to the first node of the non-empty linked list)
 int find(Node *head)
{
    Node *P1=head, *P2=head;
    if(head -> next != NULL)
    {
        P1=head -> next;
        P2=(head -> next)?head -> next -> next:NULL;
    }
    while((P1!= NULL) && (P2!=NULL)
    {
        if (P1==P2) return 1;
        P1=P1 -> next;
        P2= (P2-> next !=NULL)?P2 -> next -> next:NULL;
    }
    return 0;
}
A
It finds the duplicate element in the list.
B
It finds the middle node in th list.
C
It finds the cycle in the list.
D
It finds the last node in the lsit.
   Data Structure
Question 2 Explanation: 
P1 is slower pointer, which moves to the next node in every iteration of while loop.
P2 is faster pointer, which moves to the next to next node in every iteration.
If there exist a cycle in the linked list (non-empty), it return 1 by checking P1 == P2.
otherwise it return 0.

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE


Question 3
Consider the following code:
Node * find (Node *head)
{
    Node *P1=head,*P2=head;
    while(P2)
    {
        P1=P1-> next;
        P2=(P2 -> next)?P2 -> next -> next:NUll;
    }
    printf("%d",P1 -> value);
}
Assume Node is the structure type with two parameters : 'value' and 'next'. Identify the node printed by the above code if non-empty linked list header is passed to the function find?
A
Value of the first node of the list
B
Value of the second node of the list
C
Value of the middle node of the list
D
Value of the last node of the list
   Data Structure
Question 3 Explanation: 
P1 traverses node by node.
P2 traverses by skipping one node. The node pointed by P1 is the middle node in the linked list when P2 reaches to NULL.
Therefore, middle element of the list is printed by P1 -> value.

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 4
Consider the following code which is used to detect the loop in the linked list. Find the missing statement X?
p=head;
q=head -> next;
while(X)
{
    if(p==q) exit(0); //loop detected
    p=p -> next;
    q=(q -> next)?(q -> next -> next):q -> next;
    
} 
A
p != NULL
B
q != NULL
C
(p!=NULL) && (q!=NULL)
D
(p!=NULL) || (q!=NULL)
   Data Structure
Question 4 Explanation: 
Both p and q are not NULL then there is a loop in the linked list and it will be detected by slow pointer (p) and fast pointer (q).
Therefore, X: (p !=NULL) && (q != NULL)

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 5
Let 'temp' be the pointer pointing to the node which needs to be deleted from a doubly linked list. Then find the correct option from the following to delete the node.
A
temp -> next = (temp -> next) -> prev
(temp -> next) -> prev = temp -> prev
B
(temp -> prev) -> next = temp -> next
(temp -> next) -> prev = temp -> prev
C
temp -> next =(temp -> next) ->next
(temp -> prev) -> next = temp -> prev
D
temp -> next = (temp -> next) -> prev
(temp -> next) -> prev = temp -> next
   Data Structure


There are 5 questions to complete.

Leave a Comment