0% found this document useful (0 votes)
4 views3 pages

HW2-DS- 何娜娜

The document discusses the average number of node moves required for insertion or deletion in an array implementation of lists, which is approximately n/2. It also presents an algorithm to connect two singly linked lists and provides methods to swap adjacent elements in both singly and doubly linked lists by adjusting pointers. The time complexity of the connection algorithm is O(m+n), where m and n are the lengths of the two lists.

Uploaded by

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

HW2-DS- 何娜娜

The document discusses the average number of node moves required for insertion or deletion in an array implementation of lists, which is approximately n/2. It also presents an algorithm to connect two singly linked lists and provides methods to swap adjacent elements in both singly and doubly linked lists by adjusting pointers. The time complexity of the connection algorithm is O(m+n), where m and n are the lengths of the two lists.

Uploaded by

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

HW2

DATA STRUCTURE
何娜娜

1. How many nodes does it need to move on


average to insert or delete a node in an array
implementation of lists? The number of moves depends on
what two factors?
In an array implementation of lists, the average number of nodes that need to be moved for
insertion or deletion depends on the position at which the operation takes place
On average, about n/2 elements need to be moved, where n is the number of elements in the
list.

2. L1 and L2 point to the heads of two singly


linked lists, and their lengths are m and n respectively.
Write an algorithm to connect the two linked lists. Please
analyze the time complexity of your algorithm.
void connectLists(Node* L1, Node* L2) {

if (L1 == NULL) {

L1 = L2; // If L1 is empty, the result is just L2

return;

Node* temp = L1;

while (temp->next != NULL) {

temp = temp->next; // Traverse to the end of L1

temp->next = L2; // Connect the last node of L1 to the head of L2

3. (Page 85, Exercises 3.3) Swap two adjacent


elements by adjusting only the pointers (and not the data)
using
– a. singly linked lists,
– b. doubly linked lists.

a. Singly Linked List

void swapAdjacentSingly(Node** head, Node* prev) {

// prev -> A -> B -> nextB

Node* A, *B;

// If prev is NULL, A is the head

if (prev == NULL) {

A = *head;

} else {

A = prev->next;

if (A == NULL || A->next == NULL) return;

B = A->next;

// Swapping pointers

A->next = B->next;

B->next = A;

if (prev == NULL) {

*head = B;

} else {

prev->next = B;

}
}

b. Doubly Linked List

void swapAdjacentDoubly(Node** head, Node* A) {

Node* B = A->next;

if (B == NULL) return;

Node* prev = A->prev;

Node* next = B->next;

// Adjust B's links

B->prev = prev;

B->next = A;

// Adjust A's links

A->prev = B;

A->next = next;

// Update surrounding nodes

if (prev != NULL) {

prev->next = B;

} else {

*head = B; // Update head if A was head

if (next != NULL) {

next->prev = A;

You might also like