|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 bin554385863 于 2019-7-17 19:42 编辑
- #include <stdio.h>
- #include <stdlib.h>
- /*链表结构体 */
- typedef struct Node
- {
- int value;
- struct Node *next;
- } node;
- node *creatlinklist(node *arg)
- {
- node *p1, *p2;
- p1 = p2 = (node *)malloc(sizeof(node));
- if (p2 == NULL)//[u][i][color=Magenta]这里应该是!=抄错了[/color][/i][/u]
- {
- /*输入节点的值 */
- scanf("%d ", &p2->value);
- /*新节点指针成员赋值为空 */
- p2->next = NULL;
- }
- /*以-1作为结束标志 */
- while (p2->value != -1)
- {
- /*若为空表,接入表头 */
- if (arg == NULL)
- {
- arg = p2;
- }
- /*反之接入表尾 */
- else
- {
- p1->next = p2;
- }
- p1 = p2;
- p2 = (node *)malloc(sizeof(node));
- if (p2 != NULL)
- {
- /*输入节点的值 */
- scanf("%d", &p2->value);
- p2->next = NULL;
- }
- }
- return arg;
- }
- void printarg(node *arg)
- {
- node *temp;
- temp = arg;
- while (temp != NULL)
- {
- printf("%d ", temp->value);
- temp = temp->next;
- }
- }
- int main(int argc, char const *argv[])
- {
- node *number = NULL;
- number = creatlinklist(number);
- printarg(number);
- return 0;
- }
复制代码
-----------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-b122q2fj.rea --stdout=Microsoft-MIEngine-Out-25x3wrx1.pqm --stderr=Microsoft-MIEngine-Error-4ob1pzrn.ig5 --pid=Microsoft-MIEngine-Pid-vmxselmy.nn1 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
1 2 3 4 5 6 7 8 9 10 11 12 13 -1
-1163005939 1 2 3 4 5 6 7 8 9 10 11 12 13
E:\Administrator\Documents\My C>
这是我抄书本上的一段代码,不明白为什么会输出一个垃圾数值,弄了一下午也没搞明白哪里有问题?
问题找到了
第15行:
if (p2 == NULL)//这里应该是!=抄错了 |
|