X_GeQAQ 2021-06-07 23:08 采纳率: 84.6%
浏览 66
已采纳

数据结构课的作业,求大神帮忙答一下,感谢QAQ

Using Sorted Linked List for Word Count

You are asked to write a program that implements a word counter by means of a linked list data structure.

Your program should read an English text file (call it textfile.txt) like this document and insert each word read into a linked list in ascending order.  Each node of the linked list must have three fields: string, integer and pointer fields.  When a word read from the textfile.txt is in the list, your program simply should increment the integer counter. If the word read does not exist, it should be inserted in its correct location to keep the words sorted in ascending order.

Once you reach end of file, your program should print the word and count pairs, one line at a time. 

Requirements:

The linked list must be implemented as a fully opaque type. You must only access the list by means of the appropriate class functions to insert, to increment the counter, to examine etc.

  • 写回答

1条回答 默认 最新

  • qfl_sdu 2021-06-08 00:19
    关注

    代码如下,如有帮助,请采纳一下,谢谢。

    #include <iostream>
    #include <string>
    using namespace std;
    struct WordNode
    {
    	string word;
    	int nmb;
    	WordNode* next;
    	WordNode(){next = 0;}
    };
    
    class WordCount
    {
    public:
    	WordCount(){head = 0;}
    	~WordCount()
    	{
    		WordNode* node = 0;
    		while(head)
    		{
    			node = head->next;
    			delete head;
    			head = node;
    		}
    	}
    	WordNode* findNode(string p);     //查找单词
    	void insertNode(WordNode* node); //插入单词
    	void addCount(WordNode* node);  //单词的数量加1
    	void display();   //显示
    private:
    	WordNode* head;
    };
    //查找单词
    WordNode* WordCount::findNode(string p)
    {
    	WordNode* node = head;
    	while(node)
    	{
    		if (p.compare(node->word) == 0)
    		{
    			return node;
    		}else
    		{
    			node = node->next;
    		}
    	}
    	return 0;
    }
    //插入单词
    void WordCount::insertNode(WordNode* node)
    {
    	if (head == 0)
    	{
    		head = node;
    		return;
    	}
    	WordNode* P = head;
    	WordNode* pre = head;
    	while(P)
    	{
    		if (P->word.compare(node->word) > 0 )
    		{
    			if(P == head)
    			{
    				node->next = head;
    				head = node; //插入头部
    			}else
    			{
    				pre->next = node;
    				node->next = P;
    			}
    			break;
    		}else
    		{
    			pre = P;
    			P = P->next;
    		}
    	}
    }
    //单词的数量加1
    void WordCount::addCount(WordNode* node)
    {
    	node->nmb += 1;
    }
    
    //显示
    void WordCount::display() 
    {
    	WordNode* node = head;
    	while(node)
    	{
    		cout << node->word << "\t" << node->nmb << endl;
    		node = node->next;
    	}
    }
    bool isctrl(char ch)
    {
    	if (ch < 0x1F || ch == 0x7F)
    	{
    		return true;
    	}
    	return false;
    }
    string toLower(char* tt)
    {
    	int len = strlen(tt);
    	for (int i = 0; i < len;i++)
    	{
    		tt[i] = tolower(tt[i]);
    	}
    	return tt;
    }
    //buf是存储文件的缓冲区,lSize是文件大小
    char* textFileRead(char* filename,int *lSize)
    {
    	char* buf;
    	FILE *pf = fopen(filename,"r");
    	fseek(pf,0,SEEK_END);
    	*lSize = ftell(pf);
    	// 用完后需要将内存free掉
    	rewind(pf); 
    	buf = new char[*lSize+1];
    	*lSize = fread(buf,sizeof(char),*lSize,pf);
    	buf[*lSize] = '\0';
    	return buf;
    }
    
    int main()
    {
    	int size,i=0,j= 0;
    	char* buf = textFileRead("textfile.txt",&size);
    	WordCount cc;
    	if(size <= 0)
    	{
    		cout << "文件打开失败,或者为空"<< endl;
    		return 0;
    	}
    	char text[50] = {0};
    	while( i <= size)
    	{
    		if (i == size)
    		{
    			text[j] = '\0';
    			string tt = toLower(text);
    			if(tt.empty() || tt == " ")
    			{
    				j = 0;
    				i++;
    				continue;
    			}
    			WordNode* node = cc.findNode(tt);
    			if (node)
    			{
    				cc.addCount(node);
    			}else
    			{
    				node = new WordNode;
    				node->word = tt;
    				node->nmb = 1;
    				node->next = 0;
    				cc.insertNode(node);
    			}
    			break;
    		}else
    		{
    			//此处不考虑中间含有.的单词,入地名,缩写等等
    			if (buf[i] == ' ' || buf[i] == '\r' || buf[i] == ',' || buf[i] == '.' || buf[i] == '\n'|| buf[i] == '!' || isctrl(buf[i]))
    			{
    				text[j] = '\0';
    				string tt = toLower(text);
    				if(tt.empty() || tt == " ")
    				{
    					j = 0;
    					i++;
    					continue;
    				}
    				WordNode* node = cc.findNode(tt);
    				if (node)
    				{
    					cc.addCount(node);
    				}else
    				{
    					node = new WordNode;
    					node->word = tt;
    					node->nmb = 1;
    					node->next = 0;
    					cc.insertNode(node);
    				}
    				j = 0;
    			}else
    			{
    				text[j++] = buf[i];
    			}
    			i++;
    		}
    	}
    
    	cc.display();
    	return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路