学链表的时候不知道为什么会有一行乱码
(学编程真快乐,就是搞不懂)
代码如下:
#include<stdio.h>
#include<stdlib.h>
char* gets(char* str);
typedef struct NODE {
char name[9];
int num;
struct NODE* next;
}node;
node* putin() {
int num = 1;
node* head ;
node* newnode;
head = (node*)malloc(sizeof(node));
node* nownode = head;
while (1)
{
int a = 1;
printf("数字:\n");
scanf_s("%d", &num);
if (num != 0) {
newnode = (node*)malloc(sizeof(node));
newnode->next = NULL;
printf("字符:\n");
gets(nownode->name,getchar());
newnode->num = num;
nownode->next = newnode;
nownode = newnode;
}
else {
goto a;
}
}a:
printf("-----分割线-----\n");
printf("-----分割线-----\n");
return head;
}
void put(node* head)
{
node* temp = head->next;
while (temp != NULL) {
printf("数字:");
printf("%d\n", temp->num);
printf("字符:");
printf("%s\n", temp->name);
}
}
int main() {
node* head=NULL;
head = putin();
put(head);
return 0;
}
运行的时候就出现了乱码:
就是第一次输入的字符会在最后变成乱码输出