读取txt,使用链表计算每个字母

时间:2016-05-21 10:14:05

标签: c

#pragma warning (disable:4996)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>

#define NUM_OF_ALPHABET 26
#define MAX_HEAP_SIZE 100


typedef struct _CharFrequency
{
    char character;
    int frequency;
    struct _CharFrequency * next;
}CharFrequency;

typedef CharFrequency* pCharFrequency;

pCharFrequency pHead = NULL;

void initList();
void addNode(char ch);
void printAllNode(pCharFrequency pHead);

int main()
{
    int i = 0, cnt = 0;
    FILE *pFile;
    char readLine[1024], *ptr;
    char *token = " \t\n.";

    pFile = fopen("C:\\Users\\Home\\Desktop\\dataset.txt", "r");
    if (pFile == NULL)
    {
        printf("File open failed.\n");
        return 0;
    }

    while (fgets(readLine, 1024, pFile) != NULL)
    {
        ptr = strtok(readLine, token);
        while (ptr != NULL)
        {
            for (i = 0; i < strlen(ptr); i++)
            {
                addNode(ptr[i]);
            }

            ptr = strtok(NULL, token);
        }
    }
    printAllNode(pHead);


    return 0;
}

void initList()
{

    pHead = (CharFrequency*)malloc(sizeof(CharFrequency));

    if (!pHead)
    {
        printf("Fault\n");
        return;
    }

    pHead->character = '\0';
    pHead->frequency = 0;
    pHead->next = NULL;
}

void addNode(char ch)
{
    int i = 0;
    pCharFrequency pNode = NULL;
    pCharFrequency pCurrent= NULL;

    if (isalpha(ch) == 0)
        return;

    if (ch >= 'A' && ch <= 'Z')
        ch = ch + 32;

    printf("%c ", ch);

    for (pCurrent = pHead; pCurrent != NULL ; pCurrent = pCurrent->next)
    {
        if (pCurrent->character == ch)
        {
            pCurrent->frequency++;
        }
        else
        {
            pNode = (CharFrequency*)malloc(sizeof(CharFrequency));
    pNode->frequency = 0;
    pNode->next = NULL;

    pNode->character = ch;
    pNode->frequency++;

    pCurrent->next = pNode;
        }
    }

    pNode = (CharFrequency*)malloc(sizeof(CharFrequency));
    pNode->frequency = 0;
    pNode->next = NULL;

    pNode->character = ch;
    pNode->frequency++;

    pCurrent->next = pNode;

}

void printAllNode(pCharFrequency pHead)
{
    pCharFrequency pCurrent;
    pCurrent = pHead;

    pCurrent = pHead;

    while (pCurrent->next != NULL) {
        printf("%c %d", pCurrent->character, pCurrent->frequency);
        pCurrent = pCurrent->next;
    }

}

我想构建一个程序来读取txt文件,只计算字母表,并使用链表计数它们。我创建了一个名为CharFrequency的结构来计算字母表。 addNode函数获取字符,检查它是否在列表中,并计算它们。 在addNode函数中执行for()时会出错。

1 个答案:

答案 0 :(得分:1)

您需要重新考虑addNode方法中的逻辑。每次在列表中找不到字符时,它都会添加一个新节点,即使找到匹配项,循环也会继续,直到最后一个节点每次都添加一个新节点。

你可以做这样的事情来帮助你开始并尝试使它更有效率。

pCharFrequency pNode = NULL;
pCharFrequency pCurrent= NULL;
pCharFrequency pTail= NULL;//this will keep track of the last node in the list 
                          //so that we use it to insert a new node
....//your other code

pCurrent = pHead;//start from the head
while (pCurrent!=NULL) 
{
    if (pCurrent->character == ch)
    {
        pCurrent->frequency++;
        return;//if a match was found, count and return
    }

    if(pCurrent->next == NULL)
        pTail=pCurrent;//save the pointer to the last node in the list if we reach to it

    pCurrent=pCurrent->next;//get the next node
}

//if we reach here, then we need to create a new node
pNode = (CharFrequency*)malloc(sizeof(CharFrequency));

if(pNode==NULL)
{ 
    //show error message
    return;
}
pNode->frequency = 1;
pNode->next = NULL;
pNode->character = ch;

if(pHead==NULL)
   pHead=pNode;//for the very first node,we just assign to head
else
  pTail->next = pNode;//otherwise set the last node's next to the node we just created
相关问题