新建链表并填充数据的问题
我想新建立一个链表并在里面添加数字输出,但是总是崩溃,求高手给指点以下那里错了麻烦。#include<stdio.h>
#include<stdlib.h>
struct Day
{
int number;
struct Day *next;
};
void addnumber(struct Day **my);
void my_scanf(struct Day *may);
void my_print(struct Day *my);
int main(void)
{
int i;
struct Day *my; //创建一个头节点
/*my->next = NULL;//清空头节点中的信息域*/
for(i = 0; i < 2; i++)
{
addnumber(&my);
}
my_print(my);
return 0;
}
void addnumber(struct Day **my)
{
struct Day *may; //创建一个首节点
struct Day *temp; //创建一个临时节点
may = (struct Day *)malloc(sizeof(struct Day));
if(may == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
my_scanf(may);
if(*my != NULL)
{
temp = *my;
*my = may;
may->next = temp;
}
else
{
*my = may;
may->next = NULL;
}
}
void my_scanf(struct Day *may)
{
printf("请输入一个数字");
scanf("%d", may->number);
}
void my_print(struct Day *my)
{
while (my != NULL)
{
printf("%d", my->number);
my = my->next;
}
} 来个人回答一下问题阿{:5_94:} 1.
typedef struct Day // 我觉得这里用tyepedef会更方便~
{
int number;
struct Day *next;
}DAY, *PDAY;
2.
for(i = 0; i < 2; i++)
{
addnumber(&my); // 这里执行两次是什么意思?
//发送两次头结点的地址?
//那么你的两个地址都是挂到头结点上?
}
3.
may = (PDAY)malloc(sizeof(DAY)); // 按照我的理解, 这个才应该是头结点
然后生成后一个结点应该挂到这个后面,而不是在main函数里面发送两次
其他的待会回家了再发。。。 风过无痕丶 发表于 2018-2-18 17:59
1.
这个是我注解我自己认为写的更详细的一个,还麻烦您给看看看那里错了,麻烦了,实在是不知道错误出在那里了,但是就是不能执行,快崩溃了。
#include<stdio.h>
#include<stdlib.h>
//创建一个节点类型
typedef struct Kong
{
int num; //数据域
struct Kong *next; //指针域
}NODE, *PNODE;
void my_add(PNODE *lib);
void my_scanf(PNODE num2);
void my_print(PNODE lib);
int main(void)
{
int i = 3;
PNODE head = NULL;//创建一个头节点
/*my_add(&head);*/
while(i--)
{
my_add(&head);
}
my_print(head);
return 0;
}
//创建一个链表
void my_add(PNODE *lib)
{
PNODE num2; //创建首个节点
PNODE num3; //创建临时节点
num2 = (PNODE)malloc(sizeof(NODE));//给首节点分配内存空间
if(num2 == NULL)
{
printf("内存空间分配失败!\n");
exit(1);
}
my_scanf(num2);//调用输入函数scanf
num3 = *lib; //把头节点的地址传给临时节点
*lib = num2;
num2->next = num3;
}
//给此链表付值
void my_scanf(PNODE num2)
{
printf("请输入一个数字:");
scanf("%d", num2->num);
}
//打印链表
void my_print(PNODE lib)
{
PNODE prin;
prin = lib;
while(prin != NULL)
{
printf("打印链表:");
printf("%d", prin->next);
prin = prin->next;
}
} 风过无痕丶 发表于 2018-2-18 17:59
1.
这里我是想向链表中输入两次数据所以才这么写的
or(i = 0; i < 2; i++)
{
addnumber(&my); // 这里执行两次是什么意思?
//发送两次头结点的地址?
//那么你的两个地址都是挂到头结点上?
} 1>------ 已启动全部重新生成: 项目: tmp, 配置: Debug Win32 ------
1>main.c
1>c:\visualstudioprojects\tmp\tmp\main.c(54): warning C4477: “scanf”: 格式字符串“%d”需要类型“int *”的参数,但可变参数 1 拥有了类型“int”
1>c:\visualstudioprojects\tmp\tmp\main.c(54): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\ucrt\stdio.h(1272): note: 参见“scanf”的声明
1>c:\visualstudioprojects\tmp\tmp\main.c(66): warning C4477: “printf”: 格式字符串“%d”需要类型“int”的参数,但可变参数 1 拥有了类型“Kong *”
1>tmp.vcxproj -> C:\VisualStudioProjects\tmp\Debug\tmp.exe
1>已完成生成项目“tmp.vcxproj”的操作。
========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========
c:\visualstudioprojects\tmp\tmp\main.c(54): warning C4477: “scanf”: 格式字符串“%d”需要类型“int *”的参数,但可变参数 1 拥有了类型“int”
我相信你的编译器也一定有这个警告
54行 scanf 有一个 %d,这个 %d 需要一个 int 指针的参数,但现在这个参数不是int指针,而是int类型
#include<stdio.h>
struct Test
{
int num;
char str;
};
int main(void)
{
struct Test test = {0};
struct Test *p = &test;
scanf("%d", &test.num);
scanf("%d", &p->num);
return 0;
}
多留意警告,编译器不会平白无故报警告
人造人 发表于 2018-2-18 19:58
c:\visualstudioprojects\tmp\tmp\main.c(54): warning C4477: “scanf”: 格式字符串“%d”需要类型 ...
我用的是linux的GCC,毛都没有提示一个,气死我了,谢谢阿,我都在这网上等了,快三个小时了。 奥普瓯江 发表于 2018-2-18 21:06
我用的是linux的GCC,毛都没有提示一个,气死我了,谢谢阿,我都在这网上等了,快三个小时了。
这链表就不是人学的,太虐人了累心 奥普瓯江 发表于 2018-2-18 21:06
我用的是linux的GCC,毛都没有提示一个,气死我了,谢谢阿,我都在这网上等了,快三个小时了。
$ gcc -Wall main.c
main.c: In function 'my_scanf':
main.c:54:23: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
scanf("%d", num2->num);
^
main.c:54:23: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
main.c: In function 'my_print':
main.c:66:40: warning: format '%d' expects argument of type 'int', but argument 2 has type 'struct Kong *' [-Wformat=]
printf("%d", prin->next);
^
main.c:66:40: warning: format '%d' expects argument of type 'int', but argument 2 has type 'struct Kong *' [-Wformat=] 奥普瓯江 发表于 2018-2-18 21:07
这链表就不是人学的,太虐人了累心
如果说链表很难,那后面的树和图怎么办?
本帖最后由 风过无痕丶 于 2018-2-18 21:53 编辑
哈哈 来晚了啊~
#pragma warning(disable : 4996)
#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
//创建一个节点类型
typedef struct Kong
{
int num; //数据域
struct Kong *next; //指针域
}NODE, *PNODE;
PNODE my_add(void);
void my_print(PNODE phead);
int main(void)
{
PNODE head = NULL;//创建一个头节点
head = my_add();
my_print(head);
while (1) {
getchar();
}
return 0;
}
//创建一个链表
PNODE my_add(void)
{
int n;// 临时存放节点的值
int len; // 节点的个数
PNODE num2 = (PNODE)malloc(sizeof(NODE));//给首节点分配内存空间
if (num2 == NULL)
{
printf("内存空间分配失败!\n");
exit(-1);
}
PNODE num3 = num2; // 让他永远指向尾节点
num3->next = NULL;// 尾节点的特性是指针域为空
printf("请输入您需要生成的链表节点的个数:");
scanf("%d", &len);
for (int i = 0; i < len; i++) {
printf("请输入第%d个节点的值", i + 1);
scanf("%d", &n);
PNODE pNew = (PNODE)malloc(sizeof(NODE)); //这里是循环一次生成一个节点
//下面要做的就是给节点赋值,并且把他们连接起来
//这里再判断一下 是不是每个节点都生成成功了
if (pNew == NULL)
{
printf("内存空间分配失败!\n");
exit(-1);
}
pNew->num = n; // 给新的节点赋值
num3->next = pNew; // 把尾节点挂到后面去
pNew->next = NULL;// 清空
num3 = pNew; // 这一步是关键,保证num3永远是尾节点
}
return num2;
}
//打印链表
void my_print(PNODE phead)
{
PNODE p = phead->next;
// 这个逻辑很简单,就是判断指针域是不是为空,为空肯定就是尾节点了,不为空的话 就打印数据域
while (p != NULL) {
printf("%d ", p->num);
p = p->next; //把p移到下一个节点去~
}
// 。。。美观一点 再换个行~
printf("\n");
return;
}
我帮你改造了一下 风过无痕丶 发表于 2018-2-18 21:51
哈哈 来晚了啊~
谢谢谢谢,不晚不晚,麻烦你了,我这边对这个C语言或者说算法不怎么熟悉,因为刚学,算是给了我另一种解题的思路谢谢。 本帖最后由 奥普瓯江 于 2018-2-19 23:26 编辑
人造人 发表于 2018-2-18 21:26
如果说链表很难,那后面的树和图怎么办?
暂时不知道怎么回答,谢谢,我刚学编程,反正对我都是一样的难,至于后面的要是难怎么办,现在对我来说,就是学,难的学会就不难了,不学会就都是难的,我在努力,在这里先说谢谢。麻烦了,以后还有问题的化还望多指点 奥普瓯江 发表于 2018-2-19 23:24
暂时不知道怎么回答,谢谢,我刚学编程,反正对我都是一样的难,至于后面的要是难怎么办,现在对我来说 ...
^_^
页:
[1]