#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();//创建链表
void print(struct student *head);//打印链表
struct student
{
char num;
struct student *next;
};
int n;//用来记录存放了多少数据
int main()
{
int i,j;
struct student *stu,*p;
while(1)
{
printf("请输入数字1创建链表:");
scanf("%d",&i);
if(i==1)
break;
}
while(i==1)
{
printf("请输入内容:(每个内容已回车隔开): \n");
stu=creat();
break;
}
while(1)
{
printf("请输入数字2显示链表:");
scanf("%d",&j);
if(j==2)
break;
}
while(j==2)
{
p=stu;
print(p);
break;
}
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
p1->num=getchar();
head=NULL;
n=0;
while(p1->num!='0')
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
p1->num=getchar();
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
{
do
{
printf("%3c",p->num);
p=p->next;
}while(p);
}
}
|