很简单但就是不知道哪里出了问题。
链表总是打印出来乱码。
求各位帮忙,只用看两个函数,非常感谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "lab52.h"
void welcome(){
printf("format of statistical information:id name number");
printf("please enter the corresponding code number\n");
printf("1.insert\n");
printf("2.delete\n");
printf("3.search\n");
printf("4.change\n");
printf("5.store\n");
printf("6.all display\n");
printf("7.single display\n");
printf("8.sort\n");
printf("else.don't save the disk and exit the system\n");
}
void insert(list *f){
printf("please enter them in order: id name number\n");
list *newnode=(list *)malloc(sizeof(list));
newnode->next=NULL;
if(NULL==f)f=newnode;
else{
newnode->next=f;
f=newnode;}
scanf("%s %s %d",&newnode->data.id,&newnode->data.name,&newnode->data.num);
printf("the entry was successful\n");
system("pause");
system("cls");
}
void prin(list *f){
list *p=f;
while(1){
printf("%s\t%s\t%d\n",p->data.id,p->data.name,p->data.num);
p=p->next;
if(p==NULL)break;
}
system("pause");
system("cls");
}
int main()
{ list *first=(list *)malloc(sizeof(list));
first=NULL;
while(1){
welcome();
char a=getch();
switch(a)
{
case '1':
insert(first);
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
prin(first);
break;
case '7':
break;
default:
exit(0);
}
}
return 0;
}
以下是lab52.h
#define MAX_ID_LEN 30
#define MAX_NAME_LEN 30
typedef struct {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
int num;
} info;
typedef struct node
{
info data;
struct node *next;
} list;