C链表插入不成功, 排查了一下午实在是查不出来了
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* next;
} Node;
void insert(Node* head, int data){
// create new node
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if(head->next == NULL){
head->next = newNode;
}else{
Node* temp = head->next;
while(temp){
printf("%x \n", temp);
temp = temp->next;
}
temp = newNode;
}
}
int main(){
Node head;
head.data = -1;
head.next = NULL;
insert(&head, 10);
insert(&head, 11);
insert(&head, 12);
Node* temp = head.next;
while(temp != NULL){
printf("%d \n", temp->data);
temp = temp->next;
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* next;
} Node;
void insert(Node* head, int data){
// create new node
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if(head->next == NULL){
head->next = newNode;
}else{
Node* temp = head->next;
//修改如下 输出结果为12 11 10
newNode->next=temp;
head->next=newNode;
while(temp){
printf("%x \n", temp);
temp = temp->next;
}
}
// temp = newNode;
}
int main(){
Node head;
head.data = -1;
head.next = NULL;
insert(&head, 10);
insert(&head, 11);
insert(&head, 12);
Node* temp = head.next;
while(temp != NULL){
printf("%d \n", temp->data);
temp = temp->next;
}
}
你源代码打算采用什么插入方式?我是看不出你是想用那种方式插入{:10_327:}
作者的思路呢应该是采用尾插法,但是在遍历链表时,temp最终会指向NULL,最后直接temp = newNode,相当于赋值,当然链表是连接不上的。
void insert(Node* head, int data){
// create new node
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if(head->next == NULL){
head->next = newNode;
}else{
Node* temp = head->next;
while(temp->next != NULL){
printf("%x \n", temp);
temp = temp->next;
}
temp->next = newNode;
}
} 你的 insert 函数的参数是 Node* 类型的,但是没有函数返回值
intsert 函数可以是这两种
1.参数是 Node**,函数没有返回值。
2.参数是 Node*,函数的返回值 是 Node* 把头指针返回或者把头指针删除了(free函数),返回第一个有数据的结点
通过你的 insert 函数看出 你对单链表和指针还不是很清楚 @Max472 @love_qj@大马强感谢三位朋友的指点, 已经通过刷B站视频解决了, 还是链表这一块基础太差了, 想根据自己的想法写出链表, 结果... #include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node* next;
} Node;
int main(){
// 初始化一个头节点
Node head;
head.data = -1;
head.next = NULL;
// 循环5次并创建五个节点插入到链表中
int i;
for(i=0; i<5; i++){
Node* newNode = (Node*)malloc(sizeof(Node));
newNode -> data = i;
// 核心算法: 下面的两句代码
newNode -> next = head.next; // 1. 新节点指向头结点指向的节点
head.next = newNode; // 2, 再让头结点指向新节点
}
// 根据头结点遍历链表
Node* temp = head.next;
while(temp != NULL){
printf("%d \n", temp->data);
temp = temp->next;
}
} #include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node* next;
} Node;
int main(){
Node* head;
Node* rear;
head = rear = (Node*)malloc(sizeof(Node));
head->data = -1;
head->next = NULL;
rear = head;
// 利用尾插法循环创建五个节点
int i;
for(i=0; i<5; i++){
// 创建一个新节点
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = i;
// 尾插法的核心三行代码
newNode->next = rear->next;
rear->next = newNode;
rear = newNode;
}
// 遍历并输出已生成好的链表
Node* temp = head->next;
while(temp){
printf("%d \n", temp->data);
temp = temp->next;
}
}
加油加油,一起进步
页:
[1]