//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執行呼叫函式命令之后,終端要求我在鏈表中??輸入資料,但是在輸入一些資料后,它再次期待一些輸入。我真的不知道該嘗試什么了。
uj5u.com熱心網友回復:
問題是你如何閱讀輸入create:
scanf("%d ",&num);
格式字串中的空格匹配任意數量的空白字符。因此,如果您輸入一個數字后跟一個換行符,scanf將等到您輸入一個非空白字符。
這可以通過從格式字串中洗掉空格來解決。
scanf("%d",&num);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348673.html
