2014-10-20 40 views
0

從文件中讀取字符串並按字母順序將它們插入LinkedList(C++)。我製作了節點和列表類,但是它們有問題。我在Java中完成了這項工作,它可以100%正常工作,沒有任何問題。這使我相信我一定在某個地方弄錯了指針。這也是我第二次使用' - >'符號。所以我可能在某處錯誤地使用了它。一些有用的提示表示讚賞。提前致謝。沒有錯誤,但輸出不正確。指針問題可能

//NODE CLASS 
#include <string> 
#include <iostream> 
using namespace std; 

class Node { 
    string word; 
    int count; 
    Node* next; 

    public: 
    Node (string aWord) { 
     word = aWord; 
     count = 1; 
    } 

    Node (string aWord, Node* theNext) { 
     word = aWord; 
     next = theNext; 
    } 

    void increaseCount() { 
     count++; 
    } 

    string getWord() { 
     return word; 
    } 

    int getCount() { 
     return count; 
    } 

    Node* getNext() { 
     return next; 
    } 

    void setNext(Node* theNext) { 
     next = theNext; 
    } 
}; 

//LIST CLASS 
#include<iostream> 
using namespace std; 

class LinkedList { 
    Node* head; 

    public: 
    LinkedList() { 
     head = new Node(" "); 
    } 

    void insert(string word) { 
     Node* temp = head; 
     Node* previous = head; 

    while (temp != NULL && temp->getWord() < word) { 
     previous = temp; 
     temp = temp->getNext(); 
    } 

    if (temp == NULL) { 
     Node* node= new Node(word); 
     previous-> setNext(node); 
    } else { 
     if (temp-> getWord() == word) { 
      temp->increaseCount(); 
     } else if (temp->getWord() > word) { 
      Node* node = new Node(word, temp); 
      previous->setNext(node); 
     } 
     } 
    } 

    void print() { 
     Node* temp = head->getNext(); 
     while (temp != NULL) { 
      cout<< temp; 
      temp=temp->getNext(); 
     } 
    } 
}; 

//MAIN 
#include <iostream> 
#include <iostream> 
#include <fstream> 
#include "Node.h" 
#include "LinkedList.h" 
using namespace std; 

int main(int argc, const char * argv[]) { 

     ifstream inFile("WordsStatisticData1.txt"); 

     if (!inFile.is_open()) 
     cout<< "Could not open the file"<< endl; 

     else { 
      string readData; 
      LinkedList list = *new LinkedList(); //Probably a problem here 

      while (inFile >> readData) { 
       list.insert(readData); 
       inFile.close(); 

       list.print(); 
      } 
     } 
    } 

我可能會在main中聲明一些完全錯誤的東西。 我的輸出看起來像一個帶有隨機字符的地址'0x'。

+0

'LinkedList list = * new LinkedList();'是一個內存泄漏。你如何釋放它?事實上,我認爲你根本不需要一個指針。 – 2014-10-20 20:01:14

+0

是的。我的錯誤。這部分不需要分配 – Sgacedas 2014-10-20 20:18:53

+0

@ DSib13 - '我在Java中完成了這項工作,它可以毫無問題地工作100%因爲Java以垃圾收集的形式爲您完成所有動態內存管理。事實上,由於沒有對'delete'的調用,你的代碼會在整個地方泄漏內存,這是你在Java中不用擔心的。 – PaulMcKenzie 2014-10-20 20:22:53

回答

2

您正在打印temp其中tempNode*。指針只是一個對象的地址,因此你爲什麼要在你的輸出中得到一個地址。

好像你想要得到Node包含的字符串。如果是這樣,你想:

cout << temp->getWord(); 

你有另一個問題是,你閉上你的文件和打印循環內的列表,這意味着第一個字被讀取後,它會發生的權利。您可能的意思是在循環後執行,因此可以讀取文件中的所有單詞。

你也有問題,你標記爲這樣的行。使用new關鍵字將動態分配一個對象。這些對象需要稍後使用delete刪除。但是,您取消引用動態分配的對象(使用*)並複製它,失去對動態分配對象的任何引用 - 這是典型的內存泄漏。這裏的動態分配完全沒有必要。只是做:

LinkedList list; 
+0

沒錯。那確實刪除了地址,但它只輸出文件中的第一個單詞。 – Sgacedas 2014-10-20 20:02:23

+0

@ DSib13還有其他問題。你是否打算關閉文件並在循環後打印列表? – 2014-10-20 20:03:40

+0

我實際上已經以這種方式定義了我的列表,但XCode迴應了一條建議,所以我接受了它,並且從未將它切回。我的輸出大部分是正確的,謝謝。但重複的單詞出現時,他們應該只出現一次,然後出現的次數 – Sgacedas 2014-10-20 20:15:18