gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:08:28 27 4
gpt4 key购买 nike

#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()时出错。

最佳答案

您需要重新考虑 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

关于c - 读取txt,使用链表计算每个字母表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37361555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com