2011-12-30 27 views
1

我試圖實現一個簡單的單向鏈表,在Visual Studio C++ 2010 express中插入時進行排序。在C++中實現鏈表時遇到困難

問題是,當我創建一個新節點並對其調用.getValue()函數時,會返回正確的數字,但不知何故,當我嘗試在已存在的節點上調用getValue名單。該節點可能沒有正確插入到列表中,但我無法找到原因。顯示一些看起來像參考值或其他值的其他值,而不是正確的值。

我在調試時向監視窗口添加了當前的內容,但仍然無法看到除要賦予的值以外的任何其他變量。我對視覺工作室很陌生,所以我不確定是否在那裏丟失了某些東西。這裏是我的代碼:

#include "Node.h"; 
#include <iostream> 

//namespace Linked{ 
//The first two constructors would be the first in the linked list. 
Node::Node(void){ 
    value = 0; 
    next = 0; 
} 
Node::Node(int setValue){ 
    value = setValue; 
    next = 0; 
} 
Node::Node(int setValue,Node *nextNode){ 
    value = setValue; 
    next = nextNode; 
} 
Node * Node::getNext(){ 
    return next; 
} 
void Node::setNext(Node newNext){ 
    next = &newNext; 
} 
int Node::getValue(){ 
    return value; 
} 
bool Node::isEqual(Node check){ 
    return value==check.getValue()&&next == check.getNext(); 
} 

/* 
int main(){ 
    int firstInt, secondInt; 
    std::cin>>firstInt; 
    Node first = Node(firstInt); 
    std::cout<<"Enter second int: "; 
    std::cin>>secondInt; 
    Node second = Node(secondInt, &first); 
    std::cout<<"Second: "<<second.getValue()<<"\nFirst: "<<(*second.getNext()).getValue(); 

    system("pause"); 
}*/ 

這裏是鏈表:

//LinkedList.cpp 

    LinkedList::LinkedList(void) 
    { 
     head = 0; 
     size = 0; 
    } 

    LinkedList::LinkedList(int value) 
    { 
     head = &Node(value); 
     size = 1; 
    } 

    void LinkedList::insert(int value){ 
     if(head == 0){ 

      Node newNode = Node(value); 
      head = &newNode; 
      std::cout<<"Adding "<<(*head).getValue()<<" as head.\n"; 
     }else{ 
      std::cout<<"Adding "; 
      Node current = *head; 
      int numChecked = 0; 
      while(size<=numChecked && (((*current.getNext()).getValue())<value)){ 
       current = (*(current.getNext())); 
       numChecked++; 
      } 

      if(current.isEqual(*head)&&current.getValue()<value){ 
       Node newNode = Node(value, &current); 
       std::cout<<newNode.getValue()<<" before the head: "<<current.getValue()<<"\n"; 
      }else{ 
       Node newNode = Node(value,current.getNext()); 
       current.setNext(newNode); 
       std::cout<<newNode.getValue()<<" after "<<current.getValue()<<"\n"; 
      } 

     } 
     size++; 
    } 
    void LinkedList::remove(int){ 

    } 
    void LinkedList::print(){ 
     Node current = *head; 
     std::cout<<current.getValue()<<" is the head"; 
     int numPrinted = 0; 
     while(numPrinted<(size-1)){ 
      std::cout<<(current.getValue())<<", "; 
      current = (*(current.getNext())); 
      numPrinted++; 
     } 
    } 
    int main(){ 
     int a[5] = {30,20,25,13,2}; 
     LinkedList myList = LinkedList(); 
     int i; 
     for(i = 0 ; i<5 ; i++){ 
      myList.insert(a[i]); 
     } 
     myList.print(); 
     system("pause"); 
    } 

任何指導,將不勝感激!

回答

2

當您在插入中創建節點時,您將從堆棧中分配它們,這意味着在函數返回後它們將丟失。

得到他們與堆:

Node * newNode=new Node(value); 

當你使用:

Node newNode=Node(value); 

你在堆棧上分配該對象,這意味着指針:

&newNode 

只有在該函數返回時纔有效。如果你使用堆內存,這不再是一個問題,但它的確意味着你必須爲你的列表實現一個析構函數並刪除每個節點。

+0

謝謝你的幫助。我提出了修改建議,現在我得到這個錯誤: 「錯誤C2100:非法間接尋址」 每次我參考* newNode。 – 2011-12-30 18:07:50

+0

你不應該使用'* newNode'。這樣做會將'newNode'指向的節點的內容複製到堆棧上的一個變量中。您應該使用' - >'在'newNode'上執行同時解引用和成員訪問,例如'newNode-> getValue()'。 – 2011-12-30 18:12:00