我创建了一个数据包结构,我的文本文件由短篇小说和诗歌组成。因此,第一次插入字符串实例时,dataValue包含字符串,dataCount设置为1。如果插入了同一字符串的另一个实例,则dataCount会递增。但由于某种原因,它不断地反复输出相同的单词,并且由于某种原因,所有字符串都包含1的计数。我有什么可以调整的建议吗?
struct BagNode
{
string dataValue;
string dataCopy;
int dataCount;
BagNode * next;
};
class Bag{
private:
BagNode * head;
public:
Bag()
{
head = NULL;
}
void insert(string v)
{
if(head == NULL){ //empty list
head = new BagNode;
removePunct(v);
head->dataValue = v;
transform(v.begin(), v.end(), v.begin(), ::tolower);
head->dataCopy = v;
head->next = NULL;
}
else
{
BagNode * n = new BagNode; // new node
removePunct(v);
n->dataValue = v;
transform(v.begin(), v.end(), v.begin(), ::tolower);
n->dataCopy = v;
BagNode * current = head; //for traversal
n->dataCount = 1;
if(current->dataCopy > v)
{
n->next = head;
head = n;
}
else{ //mid and tail insert
while(current->next && current->next->dataCopy < v){
current = current->next;
}
//Check For Identical Strings
if(current->dataCopy != v)
{
n->next = current->next;
current->next = n;
}
else
{
current->dataCount++;
}
}
}
}
输出:10Annette(1)1805(1)7(1)a(1)a(1)a(1)a(1)a(1)a(1)全部(1)全部(1)一个(1)和(1)和(1)和(1)和(1)和(1)和(1)和(4)Anna(1)Anna(1)AntichristI(1)AntichristI(1)是(1) )(1)到达(1)为(1)为(1)为(1)在(1)攻击(1)为(1)为(1)相信(1)更好(1)之间(1)Buonapartes (1)但(1)但(1)由(1)由(1)由(1)调用(1)