存储在链表

时间:2017-03-21 07:42:17

标签: c

我想在双链表中存储char字符串指针中的单词。我在char字符串中存储单词的功能非常完美,但是当它存储在dll元素中时它不再起作用。我无法理解列表的声明区域是否存在问题(我是列表的新手,我们只是在类中对它们做了一些理论)或者是节点更改指针。

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>

int number_of_words (FILE *f) {
char x[1024];
int i=0;
while (fscanf(f, " %1023s", x) == 1) {
    i++;
}
return i;
}

void words (FILE *f, char *words[]) {
char x[1024];
int i=0;
while (fscanf(f, " %1023s", x) == 1) {
    words[i]=strdup(x);
    i++;
}

}

typedef struct node{
int freq;
char *word_string;
struct node *next;
struct node *prev;
}node;

int main(int argc, const char * argv[]) {

FILE *input=fopen(argv[1], "r+");
if(input==NULL) printf("error in reading from file");
else printf("reading works.\n");
int k=number_of_words(input);

char *word[k];
char *word_unique[k];

rewind(input);
words(input, word);

int j=0,l=0,s=0;

for(j=0;j<k;j++) {
    for (l=0; l<j; l++){
        if (strcmp(word[j],word[l])==0)
            break;
    }

    if (j==l){
        word_unique[s]=word[j];
        s++;
    }

}

int *word_freq[s];

for(j=0;j<s;j++){
    word_freq[j]=0;
}

for(j=0;j<s;j++) {
    for (l=j; l<k; l++){
        if (strcmp(word_unique[j],word[l])==0)
            word_freq[j]++;
    }
}

char *aux=malloc(30*sizeof(char));

for(j=0;j<s;j++){
    for(l=j+1;l<s-1;l++){
        if(strcasecmp(word_unique[j], word_unique[l])>0)
        {
            strcpy(aux,word_unique[j]);
            strcpy(word_unique[j],word_unique[l]);
            strcpy(word_unique[l],aux);

        }
    }
}

node *head, *curr=NULL;
int i=0;
head=NULL;

for(i=0;i<k;i++){
    curr=(node *)malloc(sizeof(node));
    curr->word_string=word_unique[i];
    curr->freq=word_freq[i];
    curr->next=head;
    head=curr;

}

while(curr) {

    if(curr->word_string!=NULL) printf("%s:%d\n", curr->word_string, curr->freq);
    curr = curr->next;
}

return 0;
}

输入文件是一个文本文件,如下所示:
您的所有LaTeX数字都有一个与之相关的计数器。柜台的名称 与生成数字的环境或命令的名称相同,除外 没有。下面列出了LaTeX标准文档样式中使用的一些计数器 控制编号。

当我尝试按频率按字母顺序打印独特元素时,它实际上以相反的顺序打印出来,实际上具有4倍的频率。它还将编号分开。&#34;编号。来自其他人+一开始的新行,我不知道它来自哪里。这是它打印的内容:

reading works.
0- :2098416
numbering.:4 
you:4
with:4  
used:4
to:4
the:4
The:4
that:4
styles:4
standard:4
some:4
same:4
produces:4
or:4
of:4
numbers:4
number,:4
no:4
name:4
list:4
LaTeX’s:4
LaTeX:4
it.:4
is:4
in:8
has:24
for:16
except:8
Everything:4
environment:4
document:8
counters:4
counter:8
control:8
command:4
Below:4
associated:4
as:4
a:4
\.:4
Program ended with exit code: 0

0 个答案:

没有答案