#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; }
时间: 2024-02-14 19:22:51 浏览: 28
这段代码是一个链表插入的实现,其中包括创建链表、遍历链表和插入节点的函数。用户输入节点个数n以及每个节点的值,程序会创建一个链表,并输出链表的值。用户还需要输入一个目标节点的值targetData和一个新节点的值newData,程序会将新节点插入到目标节点的后面,并输出插入后的链表。但是这段代码存在一些问题,比如create函数中没有将头指针head指向第一个节点,insert函数中查找targetData的逻辑有误等。
相关问题
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType da
`#include <stdio.h>`和`#include <stdlib.h>`是C语言中的两个头文件。`<stdio.h>`是标准输入输出头文件,提供了一些用于输入输出的函数和宏定义。`<stdlib.h>`是标准库头文件,提供了一些常用的函数和宏定义,如内存分配、随机数生成等。
在你的代码中,`typedef int ElemType;`定义了一个类型别名,将`int`类型命名为`ElemType`。
`typedef struct LNode`定义了一个结构体类型`LNode`,结构体是一种自定义的数据类型,可以包含多个不同类型的成员变量。在这里,`LNode`结构体中的成员变量还没有完整定义,因此代码截断了。
完善以下代码:#include <stdio.h> #include <stdlib.h> typedef struct node { void *data; struct node *next; }NODE; NODE * find_circular_of_list(NODE *head) { }
以下是完善后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
void *data;
struct node *next;
} NODE;
NODE* find_circular_of_list(NODE* head) {
if (head == NULL) { // 链表为空,无法找到环
return NULL;
}
NODE* fast = head;
NODE* slow = head;
while (fast != NULL && fast->next != NULL) { // 快慢指针找到相遇点
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
break;
}
}
if (fast == NULL || fast->next == NULL) { // 没有环
return NULL;
}
NODE* ptr1 = head;
NODE* ptr2 = fast;
while (ptr1 != ptr2) { // 从头指针和相遇点指针同时出发,相遇点即为环的入口
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return ptr1;
}
```
这个函数的目的是在给定的链表中查找环的入口节点。如果链表不包含环,则返回 `NULL`。在实现中,我们使用了快慢指针的技巧。快指针每次移动两步,慢指针每次移动一步,如果链表中存在环,则二者最终会在环上相遇。接着,我们让两个指针同时从头节点和相遇节点出发,每次移动一个节点,当它们再次相遇时,就是环的入口节点。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)