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

失明后的世界 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;
}
...全文
218 4 打赏 收藏 转发到动态 举报
写回复
用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;
第一章 走进linux 1.1 GNU与Linux的成长 1.2 Linux的开发模式和运作机制 1.3走进Linux内核 1.4 分析Linux内核的意义 1.5 Linux内核结构 1.6 Linux内核源代码 1.7 Linux内核源代码分析工具 第二章 Linux运行的硬件基础 2.1 i386的寄存器 2.2 内存地址 2.3 段机制和描述符 2.4 分页机制 2.5 Linux中的分页机制 2.6 Linux中的汇编语言 第三章中断机制 3.1 中断基本知识 3.2中断描述符表的初始化 3.3异常处理 3.4 中断处理 3.5中断的后半部分处理机制 第四章 进程描述 4.1 进程和程序(Process and Program) 4.2 Linux中的进程概述 4.3 task_struct结构描述 4.4 task_struct结构在内存中的存放 4.5 进程组织的方式 4.6 内核线程 4.7 进程的权能 4.8 内核同步 第五章进程调度 5.1 Linux时间系统 5.2 时钟中断 5.3 Linux的调度程序-Schedule( ) 5.4 进程切换 第六章 Linux内存管理 6.1 Linux的内存管理概述 6.2 Linux内存管理的初始化 6.3 内存的分配和回收 6.4 地址映射机制 6.5 请页机制 6.6 交换机制 6.7 缓存和刷新机制 6.8 进程的创建和执行 第七章 进程间通信 7.1 管道 7.2 信号(signal) 7.3 System V 的IPC机制 第八章 虚拟文件系统 8.1 概述 8.2 VFS中的数据结构 8.3 高速缓存 8.4 文件系统的注册、安装与拆卸 8.5 限额机制 8.6 具体文件系统举例 8.7 文件系统的系统调用 8 .8 Linux2.4文件系统的移植问题 第九章 Ext2文件系统 9.1 基本概念 9.2 Ext2的磁盘布局和数据结构 9.3 文件的访问权限和安全 9.4 链接文件 9.5 分配策略 第十章 模块机制 10.1 概述 10.2 实现机制 10.3 模块的装入和卸载 10.4 内核版本 10.5 编写内核模块 第十一章 设备驱动程序 11.1 概述 11.2 设备驱动基础 11.3 块设备驱动程序 11.4 字符设备驱动程序 第十二章 网络 12.1 概述 12.2 网络协议 12.3 套接字(socket) 12.4 套接字缓冲区(sk_buff) 12.5 网络设备接口 第十三章 启动系统 13.1 初始化流程 13.2 初始化的任务 13.3 Linux 的Boot Loarder 13.4 进入操作系统 13.5 main.c中的初始化 13.6 建立init进程 附录: 1 Linux 2.4内核API 2.1 驱动程序的基本函数 2.2 双向循环链表的操作 2.3 基本C库函数 2.4 Linux内存管理中Slab缓冲区 2.5 Linux中的VFS 2.6 Linux的连网 2.7 网络设备支持 2.8 模块支持 2.9 硬件接口 2.10 块设备 2.11 USB 设备

69,336

社区成员

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

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