数据结构链表和串的初始化问题

失明后的世界 2016-05-12 10:41:53
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define MAXKEYNUM 2500 //引索表的最大容量--
#define MAXLINELEN 500 //数目串的最大长度 缓冲区长度--
#define MAXWORDNUM 10 //词表的最大容量--
#define MAXWORDLEN 100//关键词的最大容量--
typedef int Status;
typedef struct{
char *ch;
int length;
}Hstring;
typedef struct{
char *item[MAXWORDNUM];
int last;
}Wordlisttype; //词表类型

typedef int Elemtype;

typedef struct Lnode{
Elemtype data[3];
struct Lnode *next;
}Lnode,*Link,*Linklist;//*号放在前边

typedef struct{
Hstring key;//关键词
Linklist bnolist;//存放书号索引的链表
}Idextermtype;//索引项类型

typedef struct {
Idextermtype item[MAXKEYNUM+1];
int last;
}Idexlisttype;

Idexlisttype idxlist;

void Initidxlist(Idexlisttype *idxlist);
void Init_Hstring(Hstring *wd);
Status Initlist(Linklist L);
int main()
{
Initidxlist(&idxlist);
return 0;
}
Status Initlist(Linklist L)
{
int ct;
if(!(L = (Link)malloc(sizeof(Lnode))))
return OVERFLOW;
for(ct=0;ct<3;ct++)
if(!(L->data[ct] = (int)malloc(sizeof(int))))
return OVERFLOW;
L->next = NULL;
return OK;
}//initlist;
void Init_Hstring(Hstring *wd)
{
int ct;
wd->ch = (char *)malloc(MAXWORDLEN*sizeof(char));
for(ct=0;ct<MAXWORDLEN;ct++){
wd->ch[ct] = '\0';
}
wd->length = 0;
}
void Initidxlist(Idexlisttype *idxlist)
{
int ct;
for(ct=0;ct<MAXKEYNUM;ct++){
Init_Hstring(&(idxlist->item[ct].key));
Initlist(idxlist->item[ct].bnolist);
}
printf("????????????\n");
if(idxlist->item[0].bnolist->next==NULL)//--------------------------------这里有问题
printf("????????????\n");
idxlist->last = 0;
}
...全文
227 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-05-12
  • 打赏
  • 举报
回复
将函数参数名起得和调用它时传的实参的变量名一样也没用。而且还造成概念混淆。
paschen 2016-05-12
  • 打赏
  • 举报
回复
单步调试可以发现 idxlist->item[0].bnolist 已经是NULL了,取idxlist->item[0].bnolist->next导致崩溃
小灸舞 版主 2016-05-12
  • 打赏
  • 举报
回复
Initlist(idxlist->item[ct].bnolist);这样的传参方式(一级指针)是没有用的,在Initlist函数里操作的只是一个指针的拷贝 改成传二级指针或者一级指针的引用

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define MAXKEYNUM 2500 //引索表的最大容量--
#define MAXLINELEN 500 //数目串的最大长度 缓冲区长度--
#define MAXWORDNUM 10 //词表的最大容量--
#define MAXWORDLEN 100//关键词的最大容量--
typedef int Status;
typedef struct{
    char *ch;
    int length;
}Hstring;
typedef struct{
    char *item[MAXWORDNUM];
    int last;
}Wordlisttype; //词表类型

typedef int Elemtype;

typedef struct Lnode{
    Elemtype data[3];
    struct Lnode *next;
}Lnode,*Link,*Linklist;//*号放在前边

typedef struct{
    Hstring key;//关键词
    Linklist bnolist;//存放书号索引的链表
}Idextermtype;//索引项类型

typedef struct {
    Idextermtype item[MAXKEYNUM+1];
    int last;
}Idexlisttype;

Idexlisttype idxlist;

void Initidxlist(Idexlisttype *idxlist);
void Init_Hstring(Hstring *wd);
Status Initlist(Linklist *L);
int main()
{
    Initidxlist(&idxlist);
    return 0;
}
Status Initlist(Linklist *L)
{
    int ct;
    if(!(*L = (Link)malloc(sizeof(Lnode))))
        return OVERFLOW;
    for(ct=0;ct<3;ct++)
        if(!((*L)->data[ct] = (int)malloc(sizeof(int))))
            return OVERFLOW;
    (*L)->next = NULL;
    return OK;
}//initlist;
void Init_Hstring(Hstring *wd)
{
    int ct;
    wd->ch = (char *)malloc(MAXWORDLEN*sizeof(char));
    for(ct=0;ct<MAXWORDLEN;ct++){
        wd->ch[ct] = '\0';
    }
    wd->length = 0;
}
void Initidxlist(Idexlisttype *idxlist)
{
    int ct;
    for(ct=0;ct<MAXKEYNUM;ct++){
        Init_Hstring(&(idxlist->item[ct].key));
        Initlist(&idxlist->item[ct].bnolist);
    }
    printf("????????????\n");
    if(idxlist->item[0].bnolist->next==NULL)//--------------------------------这里有问题
        printf("????????????\n");
    idxlist->last = 0;
}
赵4老师 2016-05-12
  • 打赏
  • 举报
回复
printf("????????????\n"); if(idxlist.item[0].bnolist->next==NULL) printf("????????????\n"); idxlist.last = 0;

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧