struct node* insert(struct node* head, int newData, int targetData) { struct node *temp, *p, *q; p = NULL; // 创建一个节点p p = (struct node*) malloc (sizeof(struct node)); p ->data = newData; p->next = NULL; // 查找存储targetData的节点 temp = head ; while (temp != NULL) { if (temp ->data == targetData){ return head;} temp = temp -> next;} temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); return head; } // 如果找到存储targetData的节点 if (temp != NULL) { // 将节点p插入到链表存储targetData节点temp的后面 p -> data = newData; p -> next = temp -> next; temp -> next = p; } return head; }
时间: 2024-02-14 17:15:19 浏览: 58
在insert函数中,查找存储targetData的节点的逻辑存在问题。如果找到该节点,则直接返回原链表的头指针head,没有进行插入操作。如果没有找到该节点,则会遍历整个链表并输出链表的值。正确的逻辑应该是,如果找到存储targetData的节点,则将新节点插入到该节点的后面;如果没有找到该节点,则返回原链表的头指针head,不进行插入操作。
以下是修正后的代码:
```
struct node* insert(struct node* head, int newData, int targetData) {
struct node *temp = head, *p;
p = (struct node*)malloc(sizeof(struct node));
p->data = newData;
p->next = NULL;
while (temp != NULL) {
if (temp->data == targetData) { // 找到存储targetData的节点
p->next = temp->next; // 将节点p插入到存储targetData节点temp的后面
temp->next = p;
return head;
}
temp = temp->next;
}
// 没有找到存储targetData的节点
return head;
}
```
相关问题
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node* create(int n) { struct node *head, *p, *temp; // 创建一个节点 temp = (struct node*) malloc(sizeof(struct node)); scanf("%d", &(temp->data)); temp->next = NULL; n -= 1; // head = temp; p = temp; while(n > 0) { // 创建一个节点 temp = (struct node*) malloc(sizeof(struct node)); scanf("%d", &(temp->data)); temp->next = NULL; // 当前链表最有一个节点的next指针指向新创建的节点 p->next = temp; p = p->next; // n -= 1; } // 返回链表的头指针 return head; } void dispaly(struct node* head){ struct node* temp = head; while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } struct node* insert(struct node* head, int newData, int targetData) { struct node *temp, *p, *q; p = NULL; // 创建一个节点p p = (struct node*) malloc (sizeof(struct node)); p ->data = newData; p->next = NULL; // 查找存储targetData的节点 temp = head ; while (temp != NULL) { if (temp ->data == targetData){ return head;} temp = temp -> next;} temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); return head; } // 如果找到存储targetData的节点 if (temp != NULL) { // 将节点p插入到链表存储targetData节点temp的后面 p -> data = newData; p -> next = temp -> next; temp -> next = p; } return head; } int main(){ struct node *head; int n, targetData, newData; scanf("%d", &n); head = create(n); // scanf("%d%d", &targetData, &newData); head = insert(head, newData, targetData); dispaly(head); return 0; }
这段代码是一个链表插入的实现,其中包括创建链表、遍历链表和插入节点的函数。用户输入节点个数n以及每个节点的值,程序会创建一个链表,并输出链表的值。用户还需要输入一个目标节点的值targetData和一个新节点的值newData,程序会将新节点插入到目标节点的后面,并输出插入后的链表。但是这段代码存在一些问题,比如create函数中没有将头指针head指向第一个节点,insert函数中查找targetData的逻辑有误等。
补齐以下代码#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void sort(struct node *head); //采用选择排序法思想,对结点数据进行交换处理 void destroy(struct node* head); struct node* creatlink(); void traverse(struct node* head); int m
代码中缺少函数 `main`,需要补充。
以下是完整代码:
```
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void sort(struct node *head);
void destroy(struct node* head);
struct node* creatlink();
void traverse(struct node* head);
int main() {
struct node *head = creatlink(); // 创建带头结点的链表
sort(head); // 对链表进行排序
traverse(head); // 遍历链表
destroy(head); // 销毁链表
return 0;
}
struct node *creatlink() {
struct node *head = (struct node*)malloc(sizeof(struct node)); // 创建头结点
struct node *p = head;
int num;
printf("请输入数据(输入0结束):\n");
scanf("%d", &num);
while (num) {
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = num;
newnode->next = NULL;
p->next = newnode;
p = p->next;
scanf("%d", &num);
}
return head;
}
void sort(struct node *head) {
struct node *p = head->next;
while (p != NULL) {
struct node *q = p->next;
while (q != NULL) {
if (p->data > q->data) {
int temp = p->data;
p->data = q->data;
q->data = temp;
}
q = q->next;
}
p = p->next;
}
}
void traverse(struct node *head) {
struct node *p = head->next;
printf("排序后的链表数据为:\n");
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void destroy(struct node *head) {
struct node *p = head;
while (p != NULL) {
struct node *q = p->next;
free(p);
p = q;
}
}
```
阅读全文