用c实现要求实现以下结构体或者函数(每个函数功能请严格按照要 求完成,不要修改,检查时会用统一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) //从小到大排序 示例main函数中调用顺序 Node *list_head_insert Node *list_head_insert Node *list_head_insert int list_tail_insert int list_tail_insert Node *list_specific_insert void print_list Node *sort void print_listnvoid reverse_print_list void change_specific_var 2 3 void print_list Node *del_specific_var 3 void print_list
时间: 2023-05-19 19:02:10 浏览: 99
非常抱歉,我可以回答你的问题。以下是用 C 实现要求的结构体和函数:
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;
return new_node;
}
Node *list_specific_insert(Node *list_head, int location, int var) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = var;
if (location == 1) {
new_node->next = list_head;
return new_node;
}
Node *p = list_head;
int i = 1;
while (p != NULL && i < location - 1) {
p = p->next;
i++;
}
if (p == NULL) {
return list_head;
}
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;
}
}
void reverse_print_list(Node *list_head) {
if (list_head == NULL) {
return;
}
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;
Node *r = NULL;
while (q != NULL) {
if (q->data < p->data) {
p->next = q->next;
q->next = p;
if (p == list_head) {
list_head = q;
} else {
r->next = q;
}
r = q;
q = p->next;
} else {
r = p;
p = q;
q = q->next;
}
}
return list_head;
}
int main() {
Node *list_head = NULL;
list_head = list_head_insert(list_head, 1);
list_head = list_head_insert(list_head, 2);
list_head = list_head_insert(list_head, 3);
list_tail_insert(list_head, 4);
list_tail_insert(list_head, 5);
list_specific_insert(list_head, 3, 6);
print_list(list_head);
printf("\n");
list_head = sort(list_head);
print_list(list_head);
printf("\n");
reverse_print_list(list_head);
printf("\n");
change_specific_var(list_head, 2, 3);
print_list(list_head);
printf("\n");
list_head = del_specific_var(list_head, 3);
print_list(list_head);
return 0;
}
阅读全文