2016-03-17 31 views
0

我有一个链接列表的工作示例,它在0处插入一个节点,然后打印出其值。但是,我的print()函数不适用于较大的数据,因为NULL检查无法产生所需的结果。在调试过程中,我的指针成员current指向一个称为nodeT的类型。在打印nodeT项目的info成员为std::cout << *(current->info) << " ";之后,当前的值不再是nodeT,而是垃圾值0xcccccccc。 current已初始化。为什么我的指针值在调试时被改变了?以下是代码:指针值的变化没有明确的赋值

#include "nodeT.h" 

using namespace std; 

template <class elemType> 
class linkedListSort 
{ 
public: 
    void insertAt(int location, elemType& insertItem); 
    void print(); 

private: 
    //consider making const 
    nodeT<elemType> *beginningNode; // handle to the beginning of the list 
    nodeT<elemType> *current; // pointer to current node 
    int currentIndex; //int representing which node in the list current is pointing to 
    int length;  //to store the length of the list 
    int maxSize;  //to store the maximum size of the list 

}; 

插入项目。第一个实际的项目插入代码使用(如果列表为空插入项):

template <class elemType> 
void linkedListSort<elemType>::insertAt 
(int location, elemType& insertItem) 
{ 
    if (location < 0 || location >= maxSize) 
    cerr << "The position of the item to be inserted " 
    << "is out of range" << endl; 
    else 
    if (length >= maxSize) //list is full 
    cerr << "Cannot insert in a full list" << endl; 
    else 
    { 
    if (currentIndex == -1 && current == NULL) { // if the list is empty 
     currentIndex++; //increment index to 0 (first item key) 
     current = &nodeT<elemType>(insertItem); //point current to new item (link defaulted to NULL--last item in list) 
     beginningNode = current; 
     length++; 
    } 
    else 
    if (currentIndex != -1 && current != NULL) { // if the list is non-empty 
     if (currentIndex == location - 1) { // if current already points to desired location 
     nodeT<elemType> rightNode = *(current->link); 
     current->link = &nodeT<elemType>(insertItem, &rightNode); // insert item after current 
     length++; 
     } 
     else { //traverse list until desired index 
     if (currentIndex < location - 1) { // if current points to item before desired location 
      for (int i = currentIndex; i < location; i++) { // increment until desired location 
      if (i == location - 1) { 
       nodeT<elemType> rightNode = *(current->link); 
       current->link = &nodeT<elemType>(insertItem, &rightNode); // insert item after current 
       length++; 
       break; //item inserted break out of traversal 
      } 
      current = current->link; 
      currentIndex++; 
      } 
     } 
     else { // start from the beginning 
      *(current) = *beginningNode; 
      currentIndex = 0; 
      for (int k = 0; k < location; k++) { 
      if (k == location - 1) { 
       nodeT<elemType> rightNode = *(current->link); 
       current->link = &nodeT<elemType>(insertItem, &rightNode); // insert item after current 
       length++; 
       break; //item inserted break out of traversal 
      } 
      current = current->link; 
      currentIndex++; 
      } 
     } 
     } 
    } 
    } 
} //end insertAt 

这是用来存储在链表项节点类:

template <class elemType> 
class nodeT { 
public: 
    nodeT(elemType& infoParam, nodeT<elemType> *linkParam); //standard 
    nodeT(elemType& infoParam); //if unlinked node (ex. last item) 
    nodeT(); 
    elemType *info; 
    nodeT *link; 
}; 

template<class elemType> 
nodeT<elemType>::nodeT(elemType& infoParam, nodeT<elemType> *linkParam) { 
    info = &infoParam; 
    link = linkParam; 
} 

//when link is null (last item and uncircular) 
template<class elemType> 
nodeT<elemType>::nodeT(elemType& infoParam) { 
    info = &infoParam; 
    link = NULL; 
} 

//in case node is needed before info or link is known (default) 
template<class elemType> 
nodeT<elemType>::nodeT() { 
    info = NULL; 
    link = NULL; 
} 

这是打印功能在指针似乎行动起来:

template <class elemType> 
void linkedListSort<elemType>::print() 
{ 
    //start from the begginning of the list 
    *(current) = *beginningNode; 
    for (int i = length; i > 0; i--) { 
    std::cout << *(current->info) << " "; //current seems to change value after this line executes 
    if (current->link != NULL) //link is not null because pointer somehow changed from nodeT to 0xcccccccc 
     current = current->link; 
    else 
     break; 
    } 
    //reset index and current 
    *(current) = *beginningNode; 
    currentIndex = 0; 

    std::cout << endl; 
} 

这是主要的:

#include <iostream> 
#include "linkedListSort.h" 
int main() { 

    linkedListSort<int> list1 = linkedListSort<int>(100); 
    int num = 0; 
    list1.insertAt(0, num); 
    list1.print(); 

    char stop; 
    std::cin >> stop; 
    return 0; 
} 

请注意,此代码不会产生错误或异常。但是,我正处于开发阶段,我不敢正确地看到我写入链接列表实现的数据。请让我知道,如果你可以在调试模式下运行这个并重新创建这个垃圾值/失败的空检查。为什么会发生?

回答

1

&nodeT<elemType>(insertItem)&nodeT<elemType>(insertItem, &rightNode)正在接收临时对象的地址,在评估该方法后将会消失。

你必须使用new运营商分配新的物体,像

current = new nodeT<elemType>(insertItem); 
+0

和'删除current'就足够了,即使目前已分配多少“'new'项目”? – gordlonious