Ryan_Li 发表于 2021-3-28 15:18:30

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

本帖最后由 Ryan_Li 于 2021-3-28 15:21 编辑

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
        char name;
        int num;
}STUD;

typedef struct node{
        STUD stu;
        struct node *next;
}LNode;

STUD *CreatStudent(){
        printf("请输入学生学号、姓名:");
        STUD *stu;
        scanf("%d %s",stu->num,stu->name);
        return stu;
}
LNode *InitList(){
        LNode *head = NULL;
        head =(LNode*)malloc(sizeof(LNode));
        head->next=NULL;
        return head;
}

LNode *CreatList(){
        LNode *head,*p;
        int num;
        char name;       
        head =InitList();//头结点生成
        head->next=NULL;
        printf("开始录入数据:\n");
        scanf("%d %s",&num,name); //放外面是方便和下面形成循环
        while(num!=-1){
               p=(LNode*)malloc(sizeof(LNode));
               p->stu.num=num;
               strcpy(p->stu.name,name);
               p->next=head->next;
               head->next=p;
               scanf("%d %s",&num,name);
        }
        printf("录入结束!\n");
        return head;
}

LNode *DisplayList(LNode *l){
        LNode *p=l->next;
        while(p!=NULL){
                printf("%d %s\n",p->stu.num,p->stu.name);
                p=p->next;
        }
        return p;
}

LNode *LocateList(STUD *stu,LNode *l){
        LNode *a=l->next;
        while(a->stu.num!=stu->num){
                a=a->next;
        }
        return a;
}



int main(){
        LNode* l=CreatList();
        STUD stu = {"ada",123};
        DisplayList(l);
        LNode *p = LocateList(stu,l);

        return 0;
}





locate函数哪里错了

Ryan_Li 发表于 2021-3-28 15:21:04

在调试的时候 Display函数和Locate函数的前面明明是一样的,为什么a->next显示无法访问 而display的p->next可以访问啊
页: [1]
查看完整版本: 数据结构链表的locate函数有问题