我有一个链表的工作示例,他在0处插入一个节点,然后输出其值。但是,我的print()函数不适用于较大的数据,因为NULL检查无法产生所需的结果。在调试期间,我的指针成员current
指向一种称为nodeT
的类型。在像这样nodeT
打印info
项目的std::cout << *(current->info) << " ";
成员后,current的值不再是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;
}
请注意,此代码不会产生错误或异常。但是,我处于开发的中间阶段,我担心无法继续正确地查看要写入链表实现中的数据。请让我知道您是否可以在调试模式下运行它并重新创建此垃圾值/失败的空检查。为什么会这样呢?
最佳答案
&nodeT<elemType>(insertItem)
和&nodeT<elemType>(insertItem, &rightNode)
接收临时对象的地址,这些地址在评估方法后将消失。
您必须使用new
运算符来分配新对象,例如
current = new nodeT<elemType>(insertItem);
关于c++ - 指针值更改而无需显式分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36050944/