帮我捉一下虫子(关于c 链表部分)
看一下输出结果PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107
PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107 106 105
PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107 106 105
PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107 106 105
PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107 106 105 104 103 102 101 100
PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107 106
PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107 106
PS D:\001> ./w4
19 18 17 16 15 14 13 12 11 10
109 108 107 106 105 104 103 102 101 100
PS D:\001>
红色部分为正常输出(期待输出)其余的 都是 有问题的,明显是指针飞了,可是不知道哪里错了呢
代码很简单,创建2个链表,分别输出这两个链表
代码如下:
#include <stdio.h>
#include <malloc.h>
struct Node
{
int data;
struct Node *next;
};
//带头节点的 头插法
struct Node * init_link(struct Node * s)
{
struct Node * head=NULL;
for(int x=10;x<20;x++)
{
struct Node * node1=NULL;
node1=(struct Node*)malloc(sizeof(struct Node*));
node1->data=x;
node1->next=head;
head=node1;
}
return head;
}
//带头节点的 头插法
struct Node * init_link2(struct Node * s)
{
struct Node * head=NULL;
for(int x=100;x<110;x++)
{
struct Node * node1=NULL;
node1=(struct Node*)malloc(sizeof(struct Node*));
node1->data=x;
node1->next=head;
head=node1;
}
return head;
}
int main()
{
struct Node * head=NULL;
head=(struct Node*)malloc(sizeof(struct Node*));
head->data=0;
head->next=NULL;
//输出链表一
struct Node *pmove=init_link(head);
while (pmove)
{
printf("%4d",pmove->data);
if(pmove->next==NULL) break;
pmove=pmove->next;
}
printf("\n");
//输出链表二
struct Node * head2=NULL;
head2=(struct Node*)malloc(sizeof(struct Node*));
head2->data=0;
head2->next=NULL;
struct Node *pmove2=init_link2(head2);
while (pmove2)
{
printf("%4d",pmove2->data);
if(pmove2->next==NULL) break;
pmove2=pmove2->next;
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <malloc.h>
struct Node
{
int data;
struct Node *next;
};
//带头节点的 头插法
struct Node * init_link(struct Node * s)
{
struct Node * head=NULL;
for(int x=10;x<20;x++)
{
struct Node * node1=NULL;
node1=(struct Node*)malloc(sizeof(struct Node));/////////////////////////////////////////////是结构体的大小,不是结构体指针的大小
node1->data=x;
node1->next=head;
head=node1;
}
return head;
}
//带头节点的 头插法
struct Node * init_link2(struct Node * s)
{
struct Node * head=NULL;
for(int x=100;x<110;x++)
{
struct Node * node1=NULL;
node1=(struct Node*)malloc(sizeof(struct Node));////////////////////////////////////
node1->data=x;
node1->next=head;
head=node1;
}
return head;
}
int main()
{
struct Node * head=NULL;
head=(struct Node*)malloc(sizeof(struct Node));///////////////////////////
head->data=0;
head->next=NULL;
//输出链表一
struct Node *pmove=init_link(head);
while (pmove)
{
printf("%4d",pmove->data);
if(pmove->next==NULL) break;
pmove=pmove->next;
}
printf("\n");
//输出链表二
struct Node * head2=NULL;
head2=(struct Node*)malloc(sizeof(struct Node));///////////////////////////////
head2->data=0;
head2->next=NULL;
struct Node *pmove2=init_link2(head2);
while (pmove2)
{
printf("%4d",pmove2->data);
if(pmove2->next==NULL) break;
pmove2=pmove2->next;
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <malloc.h>
struct Node {
int data;
struct Node *next;
};
//带头节点的 头插法
//struct Node *init_link(struct Node *s) {
struct Node *init_link(void) {
struct Node *head = NULL;
for(int x = 10; x < 20; x++) {
struct Node *node1 = NULL;
//node1 = (struct Node *)malloc(sizeof(struct Node *));
//node1 = malloc(sizeof(struct Node));
node1 = malloc(sizeof(*node1));
node1->data = x;
node1->next = head;
head = node1;
}
return head;
}
//带头节点的 头插法
//struct Node *init_link2(struct Node *s) {
struct Node *init_link2(void) {
struct Node *head = NULL;
for(int x = 100; x < 110; x++) {
struct Node *node1 = NULL;
//node1 = (struct Node *)malloc(sizeof(struct Node *));
node1 = malloc(sizeof(*node1));
node1->data = x;
node1->next = head;
head = node1;
}
return head;
}
void list_deinit(struct Node *l) {
if(!l) return;
list_deinit(l->next);
free(l);
}
int main() {
/*
struct Node *head = NULL;
//head = (struct Node *)malloc(sizeof(struct Node *));
head = malloc(sizeof(*head));
head->data = 0;
head->next = NULL;
*/
//输出链表一
//struct Node *pmove = init_link(head);
struct Node *pmove = init_link();
struct Node *bak = pmove;
while(pmove) {
printf("%4d", pmove->data);
//if(pmove->next == NULL)
//break;
pmove = pmove->next;
}
pmove = bak;
list_deinit(pmove);
printf("\n");
//输出链表二
/*
struct Node *head2 = NULL;
//head2 = (struct Node *)malloc(sizeof(struct Node *));
head2 = malloc(sizeof(*head2));
head2->data = 0;
head2->next = NULL;
*/
//struct Node *pmove2 = init_link2(head2);
struct Node *pmove2 = init_link2();
bak = pmove2;
while(pmove2) {
printf("%4d", pmove2->data);
if(pmove2->next == NULL)
break;
pmove2 = pmove2->next;
}
pmove2 = bak;
list_deinit(pmove2);
printf("\n");
return 0;
}
人造人 发表于 2022-5-10 13:09
谢了 人造人 发表于 2022-5-10 13:09
void list_deinit(struct Node *l) {
if(!l) return;
list_deinit(l->next);
free(l);
}
??????释放所有指针???
我还以为链表里的所有指针都不能释放 wp231957 发表于 2022-5-10 13:26
void list_deinit(struct Node *l) {
if(!l) return;
list_deinit(l->next);
嗯,申请了内存得释放
页:
[1]