void Print()
{
struct stu *p=head;
while(p!=NULL)
{
printf("%s\n",p->name);
p=p->next;
}
}
倚天照海花无数,流水高山心自知。
确实有问题哦,大大
输入num=3(3个结点的链表),结点依次为1,2,3
但显示是1,3,2
颠倒了哦
附图:
http://www.photo-host.org/img/022809264102162921517.gif
完整程序段如下:#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct stu)
#define NULL 0
struct stu
{char name[10];
struct stu *next;
};
struct stu *head;
void print()
{
struct stu *p=head;
printf("输入的链表为:\n");
while(p!=NULL)
{
printf("%s\n",p->name);
p=p->next;
}
}
void *creat()
{
struct stu *p;
int n;
int num;
printf("请输入链表的结点个数:num=");
scanf("%d",&num);
printf("这是一个含有%d个结点的链表\n",num);
printf("请输入链表结点:name=\n");
head=(struct stu *)malloc(LEN);
head->next=NULL;
scanf("%s",&(head->name));
for(n=0;n<num-1;n++)
{
p=(struct stu *)malloc(LEN);
scanf("%s",&(p->name));
p->next=head->next; //头插法
head->next=p;
}
return 0;
}
main()
{creat();
print();
return 0;
}
[此贴子已经被作者于2006-11-5 19:46:25编辑过]