📅  最后修改于: 2023-12-03 15:12:40.842000             🧑  作者: Mango
This question is about doubly linked lists.
Consider a doubly linked list containing n elements. Each element consists of two parts: a data part and a next part. The next part of an element $x$ contains a pointer to the element following it. The head pointer points to the first element of the list and the last element of the list has a next part equal to NULL.
The following functions operate on such lists:
struct node* insert(struct node *head, int x);
struct node* delete(struct node *head, struct node *z);
struct node* delete(struct node *head, int x);
int size(struct node *head);
where struct node
is a C-structure with data
and next
fields, x
is an integer and z
is a pointer to a node. The function insert
adds an element with value x
at the end of the list. The function delete(head, z)
deletes the node pointed to by z
from the list with head head
. The function delete(head, x)
deletes the first element of the list with head head
whose data part is equal to x
. The function size(head)
returns the number of elements in the list with head head
.
You are required to implement each of the functions above.
insert
functionThe insert
function accepts a doubly linked list and an integer x
. It adds a new node with value x
at the end of the list and returns the head of the modified list.
struct node* insert(struct node *head, int x) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = x;
new_node->next = NULL;
if (head == NULL) {
new_node->prev = NULL;
return new_node;
}
struct node *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
new_node->prev = temp;
return head;
}
delete(head, z)
functionThe delete(head, z)
function accepts a doubly linked list and a pointer z
to a node in the list. It deletes the node pointed to by z
from the list and returns the head of the modified list.
struct node* delete(struct node *head, struct node *z) {
if (z == NULL) {
return head;
}
if (z == head) {
head = z->next;
}
if (z->next != NULL) {
z->next->prev = z->prev;
}
if (z->prev != NULL) {
z->prev->next = z->next;
}
free(z);
return head;
}
delete(head, x)
functionThe delete(head, x)
function accepts a doubly linked list and an integer x
. It deletes the first element of the list with head head
whose data part is equal to x
and returns the head of the modified list.
struct node* delete(struct node *head, int x) {
struct node *temp = head;
while (temp != NULL) {
if (temp->data == x) {
return delete(head, temp);
}
temp = temp->next;
}
return head;
}
size(head)
functionThe size(head)
function accepts a doubly linked list and returns the number of elements in the list with head head
.
int size(struct node *head) {
int count = 0;
struct node *temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
In this question, we have implemented four functions that operate on a doubly linked list. These functions are insert
, delete(head, z)
, delete(head, x)
and size
. The implementation was provided in C programming language, with separate implementations for each function.