Data Structure Questions and Answers-Circular Linked List

YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

CLICK HERE TO DOWNLOAD

Data Structure Questions and Answers-Circular Linked List

Question 6 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Choose the code snippet which inserts a node to the head of the list?
A

public void insertHead(int data) { 	Node temp = new Node(data); 	Node cur = head; 	while(cur.getNext() != head) 		cur = cur.getNext()
B

public void insertHead(int data) { 	Node temp = new Node(data); 	while(cur != head) 		cur = cur.getNext() 	if(head == null) 	
C

public void insertHead(int data) { 	Node temp = new Node(data); 	if(head == null) 	{ 		head = temp; 		head.setNext(head)
D

public void insertHead(int data) { 	Node temp = new Node(data); 	if(head == null) 	{ 		head = temp; 		head.setNext(head.getNext()
Question 6 Explanation: 
If the list is empty make the new node as 'head', otherwise traverse the list to the end and make its 'next' pointer point to the new node, set the new node's next point to the current head and make the new node as the head.

Question 7 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the functionality of the following code? Choose the most appropriate answer.

public int function() { 	if(head == null) 		return Integer.MIN....VALUE; 	int var; 	Node temp = head; 	while(temp.getNext() != head) 		temp = temp.getNext(); 	if(temp == head) 	{ 		var = head.getItem(); 		head = null; 		return var; 	} 	temp.setNext(head.getNext()); 	var = head.getItem(); 	head = head.getNext(); 	return var; }
A
Return data from the end of the list
B
Returns the data and deletes the node at the end of the list
C
Returns the data from the beginning of the list
D
Returns the data and deletes the node from the beginning of the list
Question 7 Explanation: 
First traverse through the list to find the end node, then manipulate the 'next' pointer such that it points to the current head's next node, return the data stored in head and make this next node as the head.

Question 8 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the functionality of the following code? Choose the most appropriate answer.

public int function() { 	if(head == null) 		return Integer.MIN....VALUE; 	int var; 	Node temp = head; 	Node cur; 	while(temp.getNext() != head) 	{ 		cur = temp; 		temp = temp.getNext(); 	} 	if(temp == head) 	{ 		var = head.getItem(); 		head = null; 		return var; 	} 	var = temp.getItem(); 	cur.setNext(head); 	return var; }
A
Return data from the end of the list
B
Returns the data and deletes the node at the end of the list
C
Returns the data from the beginning of the list
D
Returns the data and deletes the node from the beginning of the list
Question 8 Explanation: 
First traverse through the list to find the end node, also have a trailing pointer to find the penultimate node,

make this trailing pointer's 'next' point to the head and return the data stored in the 'temp' node.

Question 9 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which of the following is false about a circular linked list?
A
Every node has a successor
B
Time complexity of inserting a new node at the head of the list is O(1)
C
Time complexity for deleting the last node is O(n)
D
None of the mentioned
Question 9 Explanation: 
Time complexity of inserting a new node at the head of the list is O(n) because you have to traverse through the list to find the tail node.

Question 10 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Consider a small circular linked list. How to detect the presence of cycles in this list effectively?
A
Keep one node as head and traverse another temp node till the end to check if its 'next points to head
B
Have fast and slow pointers with the fast pointer advancing two nodes at a time and slow pointer advancing by one node at a time
C
Cannot determine, you have to pre-define if the list contains cycles
D
None of the mentioned
Question 10 Explanation: 
Advance the pointers as mentioned in 'b', check to see if at any given instant of time if the fast pointer points to slow pointer or if the fast pointer's 'next' points to the slow pointer. Note that this trick is useful only if the list is small.

There are 10 questions to complete.