首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单词出现次数和行号

单词出现次数和行号
EN

Stack Overflow用户
提问于 2011-03-28 13:00:47
回答 2查看 434关注 0票数 0

我正在编写一个程序,它生成一个文本文件,其中包含单词的出现次数和另一个文本文件中每次出现的行号。我使用了一个包含单词的AVL树结构和一个包含每个行号一个节点的链表结构。下面是struct的定义:

代码语言:javascript
复制
struct llnode {
    struct llnode *next;
    int num;
};
struct node {
    char *word;
    struct llnode *head;
    struct node *left;
    struct node *right;
    int height;
};

当我尝试使用下面的函数打印到文本文件时,我得到了一个分段错误。

代码语言:javascript
复制
void listprint(struct llnode *p) {
    if(p->next == NULL) {
        printf("%d", p->num);
    } else {
        printf("%d, ", p->num);
        listprint(p->next);
    }
}
void treeprint(struct node *p) {
    if(p != NULL) {
        treeprint(p->left);
        printf("%s: ", p->word);
        listprint(p->head);
        treeprint(p->right);
    }
}

具体地说,问题就是这一行

代码语言:javascript
复制
if(p->next == null) {

gdb为我提供了

代码语言:javascript
复制
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000

谢谢你的帮助。

编辑:

代码语言:javascript
复制
void listinsert(struct llnode *p) {
struct llnode *prev = p;
while(p != NULL) {
    prev = p;
    p = p->next;
}
p = lalloc();
p->num = line;
p->next = NULL;
prev->next = p;

struct node *addtree(struct node *p, char *w) {
int cond;
if(p == NULL) {
    p = talloc();
    p->head = NULL;
    p->word = mystrdup(w);
    p->head = listinsert(p->head);
    p->left = p->right = NULL;
} else if((cond = strcmp(w, p->word)) == 0) {
    listinsert(p->head);
} else if(cond < 0) {
    p->left = addtree(p->left, w);
    if(height(p->left)-height(p->right) == 2) {
        if(strcmp(w, p->left->word) < 0) {
            p = singleleft(p);
        } else {
            p = doubleleft(p);
        }
    }
} else {
    p->right = addtree(p->right, w);
    if(height(p->right)-height(p->left) == 2) {
        if(strcmp(w, p->right->word) > 0) {
            p = singleright(p);
        } else {
            p = singleleft(p);
        }
    }
}
return p;

int getword(char *word, int lim) {
int c;
char *w = word;
while(isspace(c = getch()));
if(c == '\n') {
    line++;
}
if(c != EOF) {
    *w++ = c;
}
if(!isalpha(c)) {
    *w = '\0';
    return c;
}
for( ; --lim > 0; w++) {
    if(!isalnum(*w = getch())) {
        ungetch(*w);
        break;
    }
}
*w = '\0';
return word[0];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-28 13:03:42

listprint中,在检查p是否为空之前,您不会检查p->next是否为空。

票数 2
EN

Stack Overflow用户

发布于 2011-03-28 15:27:48

尝尝这个。

代码语言:javascript
复制
llnode*  listinsert(struct llnode *p) {
    struct llnode *prev = p;
    if( NULL == prev ) {
            prev = lalloc();
            prev->num= line;
            prev->next = NULL;
            return prev;
    }
    while(prev->next != NULL) {
        prev = prev->next;
    }
    prev->next = lalloc();
    prev = prev->next;
    prev->num = line;
    prev->next = NULL;
    return p;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5454874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档