链接列表计数器

时间:2016-03-30 17:47:35

标签: linked-list

我最近编写了一个程序,按字母顺序对链表进行排序。它也应该计算字符串的重复数。出于某种原因,当我测试我的程序时,它不会计算其中一个字符串,但其余字符计算正常。我似乎无法找到问题所在。任何帮助将不胜感激。我已经包含了以下文件。字符串," rrr"是仅计算一次的字符串。非常感谢你的帮助!

的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 eee ccc rrr aaa rrr ccc ccc eee eee eee eee

1 个答案:

答案 0 :(得分:0)

这是因为<?php require_once 'connection.php'; header('Content-Type: application/json '); class produtos { private $db; private $connection; function __construct(){ $this->db = new DB_Connection(); $this->connection = $this->db->get_connection(); } //__construct function returnProducts($parameter) { if ($parameter != 'Todos') $query = "SELECT * FROM produtos WHERE pdstatus = '$parameter'"; else if ($parameter == 'Todos') $query = "SELECT * FROM produtos"; $result = mysqli_query($this->connection,$query); if (!mysqli_num_rows($result)) echo json_encode("Nenhum resultado encontrado."); else { while($res = mysqli_fetch_assoc($result)){ //transforma em array os resultados da query $data[] = $res; } //while echo json_encode(array("products"=>$data)); } //else } //function } //produtos $product = new Produtos(); $parameter = array("status"=>$_POST['pdstatus']); $product -> returnProducts($parameter); ?> 是字母表中的最后一个。

添加第一个"rrr"后,

curr设置为node("rrr",1)。然后,对于下一个"rrr"直到"rrr"的循环不是searchObj。因此curr不适用if(next->GetWord() == searchObj->GetWord()),因为"rrr"不会成为searchObj