我写了个关于线性表的链式实现的程序,写好了创建和遍历链式表的程序,基本运行通过。可当我(在主函数case 1)输入“建立0个元
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
struct List{
ElemType data;
struct List *next;
};
typedef struct List LNode;
typedef LNode *LinkList;
void CreateList(LinkList head,int n)
{
printf("%d",&head);
int i;
LinkList p,q;
q=head;
for(i=1;i<=n;i++){
p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=q->next;
q->next=p;
q=p;
}
}
int ListTraverse(LinkList head)
{
int i;
LinkList p;
p=head;
if(p==NULL){printf("空表\n");return 0;}
p=head->next;
for(i=1;p->next!=NULL;i++){
printf("%d::%d ",i,p->data);
p=p->next;
}
printf("%d::%d ",i,p->data);
}
void WelcomeChoice()
{
printf(" Welcome to here\n");
printf(" 1. CreateList\n");
printf(" 2.ListTraverse\n");
printf(" 0.exit\n");
printf(" Please choice one of them!:)");
}
void main()
{
LinkList head;
head=NULL;
head=(LinkList)malloc(sizeof(LNode));
head->next=NULL;
int choice;
do{
WelcomeChoice();
scanf("%d",&choice);
switch(choice){
case 1 :{
int n;
printf("想建立的元素个数:");
scanf("%d",&n);
CreateList(head,n);}
break;
case 12:{printf("%d",&head);
ListTraverse(head);}break;
case 0 :printf("Thank you!Byebye...\n");break;
default:printf("输入错误!请重新输入:\n");
}
}while(choice!=0);
}