鱼C论坛

 找回密码
 立即注册
查看: 2627|回复: 1

数据结构链表的locate函数有问题

[复制链接]
发表于 2021-3-28 15:18:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Ryan_Li 于 2021-3-28 15:21 编辑
  1.   #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct student{
  5.         char name[8];
  6.         int num;
  7. }STUD;

  8. typedef struct node{
  9.         STUD stu;
  10.         struct node *next;
  11. }LNode;

  12. STUD *CreatStudent(){
  13.         printf("请输入学生学号、姓名:");
  14.         STUD *stu;
  15.         scanf("%d %s",stu->num,stu->name);
  16.         return stu;
  17. }
  18. LNode *InitList(){
  19.         LNode *head = NULL;
  20.         head =(LNode*)malloc(sizeof(LNode));
  21.         head->next=NULL;
  22.         return head;
  23. }

  24. LNode *CreatList(){
  25.         LNode *head,*p;
  26.         int num;
  27.         char name[8];       
  28.         head =InitList();  //头结点生成
  29.         head->next=NULL;
  30.         printf("开始录入数据:\n");
  31.         scanf("%d %s",&num,name); //放外面是方便和下面形成循环
  32.         while(num!=-1){
  33.                  p=(LNode*)malloc(sizeof(LNode));
  34.                  p->stu.num=num;
  35.                  strcpy(p->stu.name,name);
  36.                  p->next=head->next;
  37.                  head->next=p;
  38.                  scanf("%d %s",&num,name);
  39.         }
  40.         printf("录入结束!\n");
  41.         return head;
  42. }

  43. LNode *DisplayList(LNode *l){
  44.         LNode *p=l->next;
  45.         while(p!=NULL){
  46.                 printf("%d %s\n",p->stu.num,p->stu.name);
  47.                 p=p->next;
  48.         }
  49.         return p;
  50. }

  51. LNode *LocateList(STUD *stu,LNode *l){
  52.         LNode *a=l->next;
  53.         while(a->stu.num!=stu->num){
  54.                 a=a->next;
  55.         }
  56.         return a;
  57. }



  58. int main(){
  59.         LNode* l=CreatList();
  60.         STUD stu = {"ada",123};
  61.         DisplayList(l);
  62.         LNode *p = LocateList(stu,l);

  63.         return 0;
  64. }


复制代码



locate函数哪里错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-28 15:21:04 | 显示全部楼层
在调试的时候 Display函数和Locate函数的前面明明是一样的,为什么a->next显示无法访问 而display的p->next可以访问啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 12:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表