【发布时间】:2021-12-18 22:37:23
【问题描述】:
//linked list
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *create(struct node *head);
int main()
{
int n;
printf("enter your slection");
printf("\n 1.create a linked list");
scanf("%d", &n);
switch (n)
{
case 1:
head = create(head);
printf("linked list has been created");
break;
default:
break;
}
return 0;
}
struct node *create(struct node *head)
{
struct node *newnode, *ptr;
int num;
printf("enter data of node");
scanf("%d ",&num);
newnode = (struct node *)malloc(sizeof(struct node *));
if (newnode != NULL)
{
head=newnode;
newnode->data=num;
newnode->next=NULL;
}
return head;
}
我不知道为什么,但在调用函数printf 命令执行后,终端要求我在链表中输入数据,但在输入一些数据后,它再次需要一些输入。我真的不知道该尝试什么了。
【问题讨论】:
-
无论如何,我建议使用您的调试器并检查您的程序的去向。您应该学习如何使用您的调试器,因为它会在大多数情况下为您提供帮助。
-
如果在每个
printf()格式化字符串的末尾(而不是开头)放置一个\n换行符,会更容易看到发生了什么。 -
Rishi Patel,谁或什么文字建议在
"%d "中编码一个空格? -
您的所有建议都已得到妥善记录,我现在将认真学习使用调试器
标签: c linked-list