要求实现以下结构体或者函数(每个函数功能请严格按照要求完成,不要修改,检查时会用统一main函数调用执行):struct Node; int list_tail_insert(Node *list_head, int var) //单个数据插入,尾插法; Node *list_head_insert(Node *list_head, int var) //单个数据插入,头插法; Node *list_specific_insert(Node *list_head, int location, int var) //指定位置插入,可以插入头,尾,或者头尾之间任意位置; void print_list(Node *list_head) //输出链表,循环方式,空格隔开; void reverse_print_list(Node *list_head) //逆序输出,递归方式,空格隔开; void change_specific_var(Node *list_head, int old_var, int new_var) //修改链表中的指定元素值; Node *del_specific_var(Node *list_head, int del_var) //删除链表中的指定元素值; Node *sort(Node *list_head) //从小到大排序。用C语言完成。
时间: 2023-05-15 22:07:46 浏览: 123
非常抱歉,我可以回答这个问题。以下是实现要求的代码:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int list_tail_insert(Node *list_head, int var) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = var;
new_node->next = NULL;
if (list_head == NULL) {
list_head = new_node;
} else {
Node *p = list_head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
}
return 0;
}
Node *list_head_insert(Node *list_head, int var) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = var;
new_node->next = list_head;
list_head = new_node;
return list_head;
}
Node *list_specific_insert(Node *list_head, int location, int var) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = var;
new_node->next = NULL;
if (location == 1) {
new_node->next = list_head;
list_head = new_node;
} else {
Node *p = list_head;
int i = 1;
while (p != NULL && i < location - 1) {
p = p->next;
i++;
}
if (p == NULL) {
printf("Error: location out of range.\n");
} else {
new_node->next = p->next;
p->next = new_node;
}
}
return list_head;
}
void print_list(Node *list_head) {
Node *p = list_head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void reverse_print_list(Node *list_head) {
if (list_head != NULL) {
reverse_print_list(list_head->next);
printf("%d ", list_head->data);
}
}
void change_specific_var(Node *list_head, int old_var, int new_var) {
Node *p = list_head;
while (p != NULL) {
if (p->data == old_var) {
p->data = new_var;
break;
}
p = p->next;
}
}
Node *del_specific_var(Node *list_head, int del_var) {
Node *p = list_head;
Node *prev = NULL;
while (p != NULL) {
if (p->data == del_var) {
if (prev == NULL) {
list_head = p->next;
} else {
prev->next = p->next;
}
free(p);
break;
}
prev = p;
p = p->next;
}
return list_head;
}
Node *sort(Node *list_head) {
if (list_head == NULL || list_head->next == NULL) {
return list_head;
}
Node *p = list_head;
Node *q = list_head->next;
while (q != NULL) {
if (q->data < p->data) {
int temp = p->data;
p->data = q->data;
q->data = temp;
}
p = p->next;
q = q->next;
}
list_head->next = sort(list_head->next);
return list_head;
}
int main() {
Node *list_head = NULL;
list_tail_insert(list_head, 1);
list_tail_insert(list_head, 2);
list_tail_insert(list_head, 3);
list_head = list_head_insert(list_head, 0);
list_head = list_specific_insert(list_head, 3, 4);
print_list(list_head);
reverse_print_list(list_head);
printf("\n");
change_specific_var(list_head, 2, 5);
list_head = del_specific_var(list_head, 3);
list_head = sort(list_head);
print_list(list_head);
return 0;
}
阅读全文