c语言 链表怎么调用,求教!!!链表,函数调用高手进!!!

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

(VC6.0环境下的编译)这个程序基本功能就是输入信息,然后可以浏览输入的信息,进行删除和插入操作。在链表插入函数中我有一个错误struct student *stu 这条语句编译时警告,我本意时用它做插入点,但系统提示必须事先定义一个struct student 型的变量然后将其地址*stu作为参数传入insert(p,stu)函数但是我无法解决这个问题。另外,在函数调用时也出现问题。当执行第一次main函数后,再次调用就失灵了。不知道哪里出现问题了。请教高手予以指点。一定感激万分!!!!或者提供方向,再次谢过了。。。

#include

#include

#include

#include

#define NULL 0

#define LEN sizeof(struct student)

struct student

{   char name[10];

int num;

int age;

struct student *next;

};

int n;char ch;/*全局变量*/

struct student *creat(void)/*定义函数,返回一个指向表头的指针*/

{

struct student *head;

struct student *p1,*p2;

n=0;

p1=p2=(struct student *)malloc(LEN);/*开辟新单元*/

scanf("%s %d %d",p1->name,&p1->num,&p1->age);

head=NULL;

while(strcmp(p1->name,"NULL"))

{

n++;

if(n==1)head=p1;

else p2->next=p1;

p2=p1;

p1=(struct student *)malloc(LEN);

scanf("%s %d %d",p1->name,&p1->num,&p1->age);/*结束输入时,必须输入两次空值:NULL */

}

p2->next=NULL;

return(head);

}

struct student *del(struct student *p)/*定义删除链表函数,返回一个处理过的链表头指针*/

{  printf("***********下面进行删除操作***********\n");

struct student *p1,*p2;int num;printf("输入要删除的学生的学号\n");scanf("%d",&num);

if(p==NULL)

p1=p;

while(num!=p1->num&&p->next!=NULL)/*p1不是所要找的结点,并且后面还有结点*/

{p2=p1;p1=p1->next;}/*p1后移一个结点*/

if(num==p->num)/*通过学号找删除结点,找到了*/

{

if(p1==p)p=p1->next;/*若p1指向首结点,

则把第二个结点的地址赋给p*/

else p2->next->next;/*若不是首个结点,则将下一结点的地址赋给前一结点p2*/

printf("deleted the:%d\n",num);

n=n-1;

}

else printf("%d id not be found!!\n",num);/*找不到该结点*/

return(p);

}

struct student *insert(struct student *p)/*定义插入函数,返回指针值*/

{   struct student *stu;scanf("%s %d %d",stu->name,stu->num,stu->age);

struct student *p0,*p1,*p2;

p0=stu;/*p0指向要插入的指针*/

p1=p;/*将首结点指针赋给p1*/

if(p==NULL)

{p=p0;p0->next=NULL;}/*如果是空表,将首元素的地址变为p0*/

else

{

while((p0->num>p1->num)&&(p1->next!=NULL))

{

p2=p1;/*让p2指向p1刚才指向的结点*/

p1=p1->next;}/*p1指向下一个结点*/

if(p0->num<=p1->num)

{

if(p==p1)p=p0;/*插到原来第一个结点之前*/

else p2->next=p0;/*插到p2结点之后*/

p0->next=p1;}/*这样就成功插入p1和p2结点之间*/

else

{p1->next=p0;p0->next=NULL;}/*作为最后一个结点插入*/

}

n=n+1;/*增加一个结点*/

return(p);

}

void print(struct student *p)/*定义输出函数*/

{   printf("********您所输入的学生信息*********\n");

while(p)

{

printf("Name:%s   Num:%d   Age:%d   \n",p->name,p->num,p->age);

p=p->next;

}

}

void main(struct student *p)/*定义主调函数*/

{

system("cls");

printf("********************************\n******  1:输入学生信息  *******\n******  2:查看学生信息  *******\n******  3:删除信息      *******\n******  4:插入操作      *******\n********************************\n");

printf("请输入操作代码(退出输入5):");

ch=getchar();

if(ch=='1')main(creat());

else if(ch=='2'){print(p);printf("返回:1;退出:2\n");if((ch=getchar())=='1')main(p);else exit(1);}

else if(ch=='3')main(insert(p));

else if(ch=='4')main(del(p));

else exit(1);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值