0% found this document useful (0 votes)
34 views10 pages

Dsa Midquestions Unit2

Uploaded by

2303A54054
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)
34 views10 pages

Dsa Midquestions Unit2

Uploaded by

2303A54054
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/ 10

Important Questions from UNIT II

What is a Singly Linked List (SLL)?

• Answer: A Singly Linked List (SLL) is a type of linked list where each node contains a data
field and a reference (link) to the next node in the sequence. The list is terminated with a
NULL reference in the last node.

• Logic:

o Each node in an SLL consists of:

1. Data: Stores the actual value or data of the node.

2. Next Pointer: Points to the next node in the sequence.

o Head: Refers to the first node, and the list is traversed from the head until a node
whose next pointer is NULL is encountered (the tail node).

2. Explain the algorithm for insertion at the beginning of a Singly Linked List.

• Answer: Inserting a node at the beginning of a Singly Linked List involves creating a new
node and updating the head pointer to point to this new node.

• Logic:

public Node insertBeginning(int data) {

Node newNode = new Node(data);

newNode.next = head; // Point new node to the current head

head = newNode; // Update head to the new node

System.out.println("Inserted: " + data);

return head;

o Explanation:

1. A new node is created.

2. The next pointer of the new node is set to the current head.

3. The head pointer is updated to point to this new node, making it the new
first node.

3. Explain the algorithm for insertion at the end of a Singly Linked List.

• Answer: Inserting a node at the end involves traversing the list to the last node and updating
its next pointer to point to the new node.
• Logic:

public static LinkedList insertEnd(LinkedList list, int data) {

Node new_node = new Node(data);

if (list.head == null) {

list.head = new_node; // Set the new node as the head if the list is empty

} else {

Node last = list.head;

while (last.next != null) {

last = last.next; // Traverse to the last node

last.next = new_node; // Set the last node's next to the new node

return list;

o Explanation:

1. Traverse the list from the head to the last node.

2. Once the last node is found (its next is NULL), update its next pointer to the
new node.

4. What is a Doubly Linked List (DLL)?

• Answer: A Doubly Linked List (DLL) is a type of linked list where each node contains data
and two pointers: one pointing to the next node and another pointing to the previous node.
DLL allows traversal in both directions (forward and backward).

• Logic:

o Each node in a DLL consists of:

1. Data: Stores the actual value.

2. Next Pointer: Points to the next node.

3. Prev Pointer: Points to the previous node.

o The head node has its prev pointer set to NULL, and the tail node has its next
pointer set to NULL.

5. Explain the algorithm for insertion at the beginning of a Doubly Linked List.
• Answer: Inserting a node at the beginning involves updating the prev pointer of the current
head to point to the new node and setting the next pointer of the new node to the current
head.

• Logic:

static Node insertBegin(Node head, int data) {

Node new_node = new Node(data);

new_node.next = head; // Make next of new node as head

if (head != null) {

head.prev = new_node; // Set previous of head as the new node

head = new_node; // Update head to new node

return head;

o Explanation:

1. A new node is created, and its next is set to the current head.

2. The prev pointer of the current head is updated to point to the new node.

3. The head pointer is updated to the new node.

6. Explain the algorithm for deletion of a node from a Doubly Linked List at a specific position.

• Answer: Deletion from a specific position involves traversing the list to the required node
and updating the next and prev pointers of its surrounding nodes.

• Logic:

public static Node delPos(Node head, int pos) {

if (head == null) return head; // If the list is empty

Node curr = head;

for (int i = 1; curr != null && i < pos; ++i) {

curr = curr.next; // Traverse to the node at position

if (curr == null) return head; // Position out of bounds

if (curr.prev != null) curr.prev.next = curr.next; // Update previous node's next

if (curr.next != null) curr.next.prev = curr.prev; // Update next node's prev

if (curr == head) head = curr.next; // If it's the head node


return head;

o Explanation:

1. Traverse the list to the node at the desired position.

2. Update the next pointer of the previous node and the prev pointer of the
next node to remove the current node.

3. If the node to be deleted is the head, update the head pointer.

7. What are the advantages and disadvantages of Circular Linked Lists (CLL)?

• Advantages:

o Efficient for cyclic tasks where the last node points back to the first node.

o Traversal from any node is possible, as any node can act as the starting point.

• Disadvantages:

o Traversal requires extra care, as the list does not have an explicit end (the tail node
points back to the head).

o More complex to implement than singly linked lists.

8. How are polynomials represented using Linked Lists?

• Answer: Polynomials are represented using a linked list where each node represents a term
in the polynomial. Each node contains:

1. Coefficient: The coefficient of the term.

2. Exponent: The exponent of the term.

3. Next Pointer: Points to the next term in the polynomial.

• Example: For the polynomial P(x)=7x4+15x3−2x2+9P(x) = 7x^4 + 15x^3 - 2x^2 +


9P(x)=7x4+15x3−2x2+9, each node will store a coefficient and exponent, such as (7, 4), (15,
3), etc.

9. Explain the algorithm for adding two polynomials represented by linked lists.

• Answer: To add two polynomials, we traverse both lists, comparing the exponents of the
terms and adding the coefficients for terms with matching exponents.

• Logic:

static Node addPolynomial(Node head1, Node head2) {

if (head1 == null) return head2;


if (head2 == null) return head1;

if (head1.pow > head2.pow) {

head1.next = addPolynomial(head1.next, head2);

return head1;

} else if (head1.pow < head2.pow) {

head2.next = addPolynomial(head1, head2.next);

return head2;

} else {

head1.coeff += head2.coeff;

head1.next = addPolynomial(head1.next, head2.next);

return head1;

o Explanation:

1. Compare the exponents of the current terms in both polynomials.

2. If the exponents are equal, add the coefficients and move to the next terms
in both polynomials.

3. If the exponents are different, move the term with the larger exponent to
the result list and continue comparing.

1. What is a Doubly Circular Linked List?

• Answer: A Doubly Circular Linked List is a type of linked list that combines the features of
both Doubly Linked List and Circular Linked List. Each node contains a data element and two
pointers, one pointing to the next node and another pointing to the previous node. The last
node in this list points to the first node, creating a circular structure.

• Logic:

o A node contains three fields:

1. Data: Stores the data value.

2. Next: Points to the next node.

3. Prev: Points to the previous node.

o The next pointer of the last node points to the first node, and the prev pointer of the
first node points to the last node, allowing traversal in both directions.
2. Explain the algorithm for insertion at the beginning of a Doubly Circular Linked List.

• Answer: Insertion at the beginning involves creating a new node and adjusting pointers to
include this new node at the start of the list.

• Logic:

static void insertBegin(int value) {

// Case 1: If the list is empty

if (start == null) {

Node new_node = new Node();

new_node.data = value;

new_node.next = new_node.prev = new_node;

start = new_node;

return;

// Case 2: List is not empty

Node last = (start).prev;

Node new_node = new Node();

new_node.data = value;

new_node.next = start;

new_node.prev = last;

// Update the previous and next pointers

last.next = (start).prev = new_node;

start = new_node;

o Explanation:

1. If the list is empty, create a node where next and prev point to itself.

2. If the list is not empty, link the new node between the last node and the first
node (start), updating all pointers accordingly.

3. Finally, update the start pointer to point to the new node.


3. Explain the algorithm for insertion at the end of a Doubly Circular Linked List.

• Answer: Inserting a node at the end involves adding the new node after the current last
node and updating the circular links.

• Logic:

static void insertEnd(int value) {

// Case 1: If the list is empty

if (start == null) {

Node new_node = new Node();

new_node.data = value;

new_node.next = new_node.prev = new_node;

start = new_node;

return;

// Case 2: List is not empty

Node last = (start).prev;

Node new_node = new Node();

new_node.data = value;

new_node.next = start; // Next of new node will be start

new_node.prev = last; // Previous of new node will be last

// Update the last node and start pointers

last.next = new_node;

start.prev = new_node;

o Explanation:

1. If the list is empty, a new node is created and its next and prev pointers
refer to itself.

2. If the list is non-empty, the prev pointer of the start node (i.e., first node)
and the next pointer of the last node (before insertion) are updated to
include the new node at the end.

3. The new node’s next points to start, and its prev points to the last node.
4. What is the time complexity of insertion in a Doubly Circular Linked List?

• Answer:

o Insertion at Beginning: The time complexity is O(1) since only a constant number of
operations are needed to adjust the pointers, irrespective of the number of nodes.

o Insertion at End: The time complexity is also O(1) since the pointer to the last node
is always available via the prev pointer of the start node, allowing constant-time
insertion.

5. Explain the algorithm for deletion of a node from a specific position in a Doubly Circular Linked
List.

• Answer: To delete a node from a specific position, we locate the node and adjust the next
and prev pointers of its neighboring nodes to remove it from the list.

• Logic:

static Node deleteNode(Node start, int key) {

if (start == null) return null;

Node curr = start, prev_1 = null;

// Search for the node with the key

while (curr.data != key) {

if (curr.next == start) {

System.out.printf("Node not found.");

return start;

prev_1 = curr;

curr = curr.next;

// If the node to be deleted is the only node

if (curr.next == start && prev_1 == null) {

start = null;

return start;
}

// If the node to be deleted is the first node

if (curr == start) {

prev_1 = start.prev;

start = start.next;

prev_1.next = start;

start.prev = prev_1;

// If the node to be deleted is the last node

else if (curr.next == start) {

prev_1.next = start;

start.prev = prev_1;

// If the node is in between

else {

Node temp = curr.next;

prev_1.next = temp;

temp.prev = prev_1;

return start;

o Explanation:

1. The algorithm first searches for the node with the given key.

2. If the node is found:

▪ If it is the only node, we set the start to null.

▪ If it is the first node, we adjust the prev pointer of the last node and
the next pointer of the second node.

▪ If it is the last node, we adjust the prev pointer of the second-last


node and the next pointer of the first node.

▪ If it is in the middle, we simply update the next and prev pointers of


the surrounding nodes.
6. What are the advantages and disadvantages of Doubly Circular Linked Lists?

• Advantages:

o Allows traversal in both forward and backward directions.

o Insertion and deletion at both ends (beginning and end) are efficient, with O(1) time
complexity.

• Disadvantages:

o Requires more space than singly linked lists due to the additional pointer (prev) in
each node.

o More complex to implement than singly linked or circular singly linked lists.

You might also like