Ich schrieb vor kurzem ein Programm, das eine verkettete Liste alphabetisch sortiert. Es soll auch die Duplikate von Strings zählen. Aus irgendeinem Grund, wenn ich mein Programm teste, zählt es keinen der Strings, aber der Rest wird gut gezählt. Ich kann das Problem nicht finden. Jede Hilfe würde sehr geschätzt werden. Ich habe die folgenden Dateien hinzugefügt. Die Zeichenfolge "rrr" ist die Zeichenfolge, die nur einmal gezählt wird. Vielen Dank für Ihre Hilfe!Linked List Counter
main.cpp
#include <iostream>
#include <sstream>
#include <string>
#include "node.h"
int main() { ifstream fin;
string text;
string inputPath;
cout << "Enter the full path to the input file:";
cin >> inputPath;
fin.open(inputPath);
//Initiates and assigns pointers to zero
node* head = 0;
node* curr = 0;
node* next = 0;
//Assigns first word to the head object
//if(!fin.eof()) {
// fin >> text;
head = new node("", 0);
//}
if(!fin.eof()) {
fin >> text;
curr = new node(text, 1);
}
head->insertAfter(curr);
while(!fin.eof()) {
fin >> text;
next = new node(text, 1);
if(next->GetWord() < head->GetWord())
{
next->insertBefore(head);
head = next;
next = curr;
}
if(next->GetWord() > curr->GetWord())
{
//Insert after current
curr->insertAfter(next);
curr = next;
}
else
//Insert between two nodes
for(node* searchObj = head->GetNext(); searchObj != curr; searchObj = searchObj->GetNext())
{
if(next->GetWord() > searchObj->GetWord() && next->GetWord() < searchObj->GetNext()->GetWord())
{
next->insertBetween(searchObj, searchObj->GetNext());
break;
}
if(next->GetWord() == searchObj->GetWord())
{
searchObj->addcount();
}
}
}
// Print linked list
curr = head->GetNext();
while (curr != 0) {
curr->PrWordNodeData();
curr = curr->GetNext();
}
string i;
cin >> i;
return 0;
}
node.cpp
#include "node.h"
//Constructor
node::node(string nword, int ncount)
{
word = nword;
count = ncount;
nextNode = 0;
}
void node::insertAfter(node* nodeLoc) {
node* tmpNext = 0;
tmpNext = this->nextNode; // Remember next
this->nextNode = nodeLoc; // this -- node -- ?
nodeLoc->nextNode = tmpNext; // this -- node -- next
return;
}
// Print dataVal
void node::PrWordNodeData() {
cout << this->word <<": count=" <<this->count << endl;
return;
}
// Grab location pointed by nextNodePtr
node* node::GetNext() {
return this->nextNode;
}
//Returns word
string node::GetWord()
{
return word;
}
//Inserts between
void node::insertBetween(node* obj1, node* obj2)
{
obj1->nextNode = this;
this->nextNode = obj2;
}
//Inserts before the head
void node::insertBefore(node* head)
{
this->nextNode = head;
}
//Adds to counter
void node::addcount()
{
this->count = this->count + 1;
}
node.h
#ifndef wordNode_hpp
#define wordNode_hpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class node {
public:
node(string, int);
void insertAfter(node* nodePtr);
node* GetNext();
void PrWordNodeData();
string GetWord();
void insertBetween(node*, node*);
void insertBefore(node*);
void addcount();
void swap(node*, node*);
void sortByCount();
private:
string word;
node* nextNode;
int count;
};
#endif /* wordNode_h */
input.txt
aaa aaa eee ccc rrr rrr ccc ccc eee eee eee eee