首先感谢帮忙,我先上代码了
代码如下:
#include
#include
typedef struct linkedlist
{
int num;
char arr[20];
struct linkedlist* next;
}node;
node* NODE()
{
int Num;
node* head;
head = (node*)malloc(sizeof(node));
node* newNode;
node* now = head;
while (1) {
int a;
printf("数字:\n");
scanf_s("%d", &Num);
if (Num != 0) {
//new = (node*)malloc(sizeof(node*));
newNode = (node*)malloc(sizeof(node));
printf("字符:\n");
scanf_s("%s", &newNode->arr);
newNode->num = Num;
newNode->next = NULL;
now->next = newNode;
now = newNode;
}
else goto a;
}
a:
return head;
}
void output(node* head) {
node* put = head->next;
while (put != NULL)
{
printf("%d\n", put->num);
put = put->next;
}
}
int main()
{
node* head = NULL;
head = NODE();
output(head);
}
错误如下:
但是要是我改成这样:
#include
#include
char gets(char* str);
typedef struct linkedlist
{
int num;
char arr[20];
struct linkedlist* next;
}node;
node* NODE()
{
int Num;
node* head;
head = (node*)malloc(sizeof(node))
node* newNode;
node* now = head;
while (1) {
int a;
printf("数字:\n");
scanf_s("%d", &Num);
if (Num != 0) {
newNode = (node*)malloc(sizeof(node));
printf("字符:\n");
gets(newNode->arr);//我在这里改用为gets
newNode->num = Num;
newNode->next = NULL;
now->next = newNode;
now = newNode;
}
else goto a;
}
a:
return head;
}
void output(node* head) {
node* put = head->next;
while (put != NULL)
{
printf("%d\n", put->num);
put = put->next;
}
}
int main()
{
node* head = NULL;
head = NODE();
output(head);
}
它就会直接跳过gets ,我实在搞不懂是为啥了
感觉链表好难学,老是出错啊!
scanf_s("%s", &newNode->arr);
改为
scanf_s("%s", &newNode->arr,20);
=======
gets不能连续读两行,中间要价格getchar()接收到换行符。