此类的大小变量在构造函数中设置为零。仅当您将项目添加到数组或删除一个项目时,它才会增加或减少,在这种情况下切割成两半,大小会减少到 1/2 容量。
但是,我的 size 变量似乎跑掉了,随机变成了随机数,就像 516846 一样,不在它设置的大小范围内。我检查并遵循了我的程序,找不到任何改变尺寸的东西,我知道尺寸和容量是在施工时设置的。
#pragma once
#include <string>
#include <iostream>
using namespace std;
template <typename ItemType>
class Node
{
private:
ItemType* items;
int size;
int capacity;
public:
Node* nextNode;
Node* prevNode;
Node(Node* pNode, Node* nNode, int limit)
{
prevNode = pNode;
nextNode = nNode;
capacity = limit;
size = 0;
if(capacity != 0)
items = new ItemType[capacity];
cout << "Node() capcity = " << capacity << " size = " << size << endl;
};
~Node(void){};
int getSize()
{
return size;
};
void addItem(int index, ItemType item)
{
cout << "node->addItem" << endl;
for(int i = (getSize() - 1); i >= index; i--)
{
items[i + 1] = items[i];
}
items[index] = item;
size ++;
};
void addItem(ItemType item)
{
cout << "node->addItem" << endl;
items[getSize()] = item;
size ++;
};
void deleteItem(int index)
{
cout << "node->deleteItem index = " << index << endl;
for(int i = index; i < (getSize() - 1); i++)
{
items[i] = items[i+1];
}
size --;
};
void cleaveInHalf()
{
cout << "node->cleaveInHalf" << endl;
size = capacity/2;
};
bool isFull()
{
return ((getSize() >= capacity) ? true : false);
};
};
当调用 isFull() 函数时,我收到错误“访问冲突读取位置 0x00000004”。大小是一些奇怪的数字,例如 51515615。
void insert(int index, const ItemType& item)
{
cout << "lal->insert" << endl;
if (index > size)
return;
//if we have no nodes to hold data make a new one between head and tail
if (head->nextNode == tail)
{
linkNewNode(head, tail);
}
// lets find the node to put it in and the spot in the array of the node
int indexIntoArray = 0;
Node<ItemType>* temp = getNodeContainingIndex(index, indexIntoArray);
if(temp->isFull())
{
// if we are full then we ant to split, and then call this function again to find the new location to go in.
splitNode(temp);
insert(index, item);
return;
}
// and now insert it in
cout << "lal->----attempting to add item at " << indexIntoArray << endl;
temp->addItem(indexIntoArray, item);
size ++;
};
Node<ItemType>* getNodeContainingIndex(int index, int& indexIntoArray)
{
cout << "lal->getNodeContainingIndex" << endl;
Node<ItemType>* temp;
if (index == size)
{
temp = tail->prevNode;
indexIntoArray = temp->getSize();
}
else if (index <= (size/2)) /* coming from 0*/
{
cout << "lal->----coming from 0" << endl;
int position = 0;
temp = head->nextNode;
position = position + temp->getSize();
while (position < index)
{
temp = temp->nextNode;
position = position + temp->getSize();
}
indexIntoArray = index - (position - temp->getSize());
return temp;
}
else /*coming from size*/
{
cout << "lal->----coming from size = " << size << endl;
int position = size;
temp = tail;
while (position > index)
{
temp = temp->prevNode;
position = position - temp->getSize();
}
indexIntoArray = abs(position - index);
return temp;
}
}
Node<ItemType>* linkNewNode(Node<ItemType>* prev, Node<ItemType>* next)
{
cout << "lal->linkNewNode" << endl;
Node<ItemType>* temp = new Node<ItemType>(prev, next, arrayCapacity);
prev->nextNode = temp; next->prevNode = temp;
numOfNodes ++;
return temp;
}
它在 getNodeContainingIndex 函数中中断的地方,就在这里
int indexIntoArray = 0;
Node<ItemType>* temp = getNodeContainingIndex(index, indexIntoArray);
if(temp->isFull())
{
在 temp->isFull() 行。