0% found this document useful (0 votes)
300 views12 pages

Doubly Linked Lists Questions and Answers

The document contains questions and answers related to doubly linked lists. Some key points: - A doubly linked list allows navigation in both directions, requires more space than a singly linked list, and insertion/deletion take longer than a singly linked list. - To insert a node at the tail of the list, the correct code is to set the new node's previous pointer to the current tail's previous node, and set the tail's previous pointer to the new node. - A memory efficient doubly linked list could use a single pointer for each node to traverse the list back and forth. - The given code sample correctly removes a node from a given position by adjusting the previous and next pointers of the

Uploaded by

Chotu111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
300 views12 pages

Doubly Linked Lists Questions and Answers

The document contains questions and answers related to doubly linked lists. Some key points: - A doubly linked list allows navigation in both directions, requires more space than a singly linked list, and insertion/deletion take longer than a singly linked list. - To insert a node at the tail of the list, the correct code is to set the new node's previous pointer to the current tail's previous node, and set the tail's previous pointer to the new node. - A memory efficient doubly linked list could use a single pointer for each node to traverse the list back and forth. - The given code sample correctly removes a node from a given position by adjusting the previous and next pointers of the

Uploaded by

Chotu111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

Ad removed. Details

Data Structure Questions and Answers – Doubly


Linked List
This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Doubly Linked
List”.

1. Which of the following is false about a doubly linked list?


a) We can navigate in both the directions
b) It requires more space than a singly linked list
c) The insertion and deletion of a node take a bit longer
d) Implementing a doubly linked list is easier than singly linked list
View Answer

2. Given the Node class implementation, select one of the following that correctly inserts a node at
the tail of the list.

public class Node


{
protected int data;
protected Node prev;
protected Node next;
public Node(int data)
{
this.data = data;
prev = null;
next = null;
}
public Node(int data, Node prev, Node next)
{
this.data = data;
this.prev = prev;
this.next = next;
}

https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 1/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

public int getData()


{
return data;
}
public void setData(int data)
{
this.data = data;
}
public Node getPrev()
{
return prev;
}
public void setPrev(Node prev)
{
this.prev = prev;
}
public Node getNext
{
return next;
}
public void setNext(Node next)
{
this.next = next;
}
}
public class DLL
{
protected Node head;
protected Node tail;
int length;
public DLL()
{
head = new Node(Integer.MIN_VALUE,null,null);
tail = new Node(Integer.MIN_VALUE,null,null);
head.setNext(tail);
length = 0;
}
}

a)

advertisement

https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 2/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

public void insertRear(int data)


{
Node node = new Node(data,tail.getPrev(),tail);
node.getPrev().setNext(node);
tail.setPrev(node);
length++;
}

b)

Note: Join free Sanfoundry classes at Telegram or Youtube

public void insertRear(int data)


{
Node node = new Node(data,tail.getPrev(),tail);
node.getPrev().getPrev().setNext(node);
tail.setPrev(node);
length++;
}

c)

advertisement

https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 3/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

public void insertRear(int data)


{
Node node = new Node(data,tail.getPrev(),tail);
node.getPrev().setNext(tail);
tail.setPrev(node);
length++;
}

d)

advertisement

public void insertRear(int data)


{
Node node = new Node(data,head,tail);
node.getPrev().setNext(node);
tail.setPrev(node);
length++;
}

View Answer

3. What is a memory efficient double linked list?


a) Each node has only one pointer to traverse the list back and forth
b) The list has breakpoints for faster traversal
c) An auxiliary singly linked list acts as a helper list to traverse through the doubly linked list
d) A doubly linked list that uses bitwise AND operator for storing addresses
View Answer

4. Which of the following piece of code removes the node from a given position?
a)
https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 4/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

public void remove(int pos)


{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
else
{
if(head == null)
return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;
for(int i=1; i<position; i++)
temp = temp.getNext();
}
temp.getNext().setPrev(temp.getPrev());
temp.getPrev().setNext(temp.getNext());
}
size--;
}

b)

public void remove(int pos)


{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
else
{
if(head == null)
return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;

https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 5/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

for(int i=1; i<position; i++)


temp = temp.getNext();
}
temp.getNext().setPrev(temp.getNext());
temp.getPrev().setNext(temp.getPrev());
}
size--;
}

c)

public void remove(int pos)


{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
else
{
if(head == null)
return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;
for(int i=1; i<position; i++)
temp = temp.getNext().getNext();
}
temp.getNext().setPrev(temp.getPrev());
temp.getPrev().setNext(temp.getNext());
}
size--;
}

d)

public void remove(int pos)


{
if(pos<0 || pos>=size)
{
System.out.println("Invalid position");
return;
}
else
{
if(head == null)
https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 6/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

return;
if(pos == 0)
{
head = head.getNext();
if(head == null)
tail = null;
}
else
{
Node temp = head;
for(int i=1; i<position; i++)
temp = temp.getNext().getNext();
}
temp.getNext().setPrev(temp.getNext());
temp.getPrev().setNext(temp.getPrev());
}
size--;
}

View Answer

5. How do you calculate the pointer difference in a memory efficient double linked list?
a) head xor tail
b) pointer to previous node xor pointer to next node
c) pointer to previous node – pointer to next node
d) pointer to next node – pointer to previous node
View Answer

6. What is the worst case time complexity of inserting a node in a doubly linked list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
View Answer

7. How do you insert a node at the beginning of the list?


a)

public class insertFront(int data)


{
Node node = new Node(data, head, head.getNext());
node.getNext().setPrev(node);
head.setNext(node);
size++;
}

b)
https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 7/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

public class insertFront(int data)


{
Node node = new Node(data, head, head);
node.getNext().setPrev(node);
head.setNext(node);
size++;
}

c)

public class insertFront(int data)


{
Node node = new Node(data, head, head.getNext());
node.getNext().setPrev(head);
head.setNext(node);
size++;
}

d)

public class insertFront(int data)


{
Node node = new Node(data, head, head.getNext());
node.getNext().setPrev(node);
head.setNext(node.getNext());
size++;
}

View Answer

8. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list after performing
the given sequence of operations?

Node temp = new Node(6,head,head.getNext());


Node temp1 = new Node(0,tail.getPrev(),tail);
head.setNext(temp);
temp.getNext().setPrev(temp);
tail.setPrev(temp1);
temp1.getPrev().setNext(temp1);

a) head-0-1-2-3-4-5-6-tail
b) head-1-2-3-4-5-6-tail
c) head-6-1-2-3-4-5-0-tail
d) head-0-1-2-3-4-5-tail
View Answer

9. What is the functionality of the following piece of code?


https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 8/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

public int function()


{
Node temp = tail.getPrev();
tail.setPrev(temp.getPrev());
temp.getPrev().setNext(tail);
size--;
return temp.getItem();
}

a) Return the element at the tail of the list but do not remove it
b) Return the element at the tail of the list and remove it from the list
c) Return the last but one element from the list but do not remove it
d) Return the last but one element at the tail of the list and remove it from the list
View Answer

10. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list after
performing the given sequence of operations?

Node temp = new Node(6,head,head.getNext());


head.setNext(temp);
temp.getNext().setPrev(temp);
Node temp1 = tail.getPrev();
tail.setPrev(temp1.getPrev());
temp1.getPrev().setNext(tail);

a) head-6-1-2-3-4-5-tail
b) head-6-1-2-3-4-tail
c) head-1-2-3-4-5-6-tail
d) head-1-2-3-4-5-tail
View Answer

Sanfoundry Global Education & Learning Series – Data Structure.

To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and
Answers.

« Prev - Data Structure Multiple Choice » Next - Data Structure Questions and Answers –
Questions – Linked List Circular Linked List

Related Posts:

Practice Programming MCQs


Apply for Information Technology Internship
Apply for Computer Science Internship

https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 9/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

Check Computer Science Books


Apply for Data Structure Internship

Data Structure MCQ, DS MCQ - Abstract Datatype

advertisement

Recommended Articles:
1. C++ Program to Implement Sorted Doubly Linked List
2. C++ Program to Implement Sorted Circularly Doubly Linked List
3. C++ Program to Implement Triply Linked List
4. Data Structure Questions and Answers – Singly Linked List Operations – 3
5. Data Structure Multiple Choice Questions – Linked List
6. Data Structure Questions and Answers – Circular Linked List
7. C Program to Implement Doubly Linked List using Singly Linked List
8. Data Structure Questions and Answers – Singly Linked List Operations – 1
9. Java Program to Implement Doubly Linked List
10. C Program to Find the Largest Element in a Doubly Linked List

advertisement

https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 10/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

Additional Resources:
Linked List Programs in C
Linked List Programs in Python
Data Structures in C
Data Structure MCQ Questions
Data Structures in Java

Popular Pages:
Data Structures in C++
Python Programming Examples
C Tutorial
C Programs on Recursion
Data Science MCQ Questions

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to


get free Certificate of Merit. Join our social networks below and stay updated with latest contests,
videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at
Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies,
Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.


https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 11/12
28/11/2023, 09:25 Doubly Linked Lists Questions and Answers - Sanfoundry

About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact

     

© 2011-2023 Sanfoundry. All Rights Reserved.

https://www.sanfoundry.com/data-structure-questions-answers-doubly-linked-lists/ 12/12

You might also like