我已经从10000个文件的双向链接列表(从最高到最低)实施插入排序,并以相反的顺序输出到文件。双重链接列表插入排序错误
据我所知,我已经实现了这样一个程序,但我注意到在输出文件中,单个数字是不合适的。每个其他号码的顺序都是正确的。
不合格的数字是一个重复的数字,但这个数字的其他重复是正确的顺序。奇怪的是这个数字是不正确的。未分类号码也只有6个地方不同步。
我已经浏览了我的程序几天,不知道问题出在哪里,所以我转向你寻求帮助。
下面是有问题的代码,
(边注:?可我的问题由我自己被删除,而我的大学不thieve我的代码,如果没有它如何被删除)
void DLLIntStorage::insertBefore(int inValue, node *nodeB)
{
node *newNode;
newNode = new node();
newNode->prev = nodeB->prev;
newNode->next = nodeB;
newNode->value = inValue;
if(nodeB->prev==NULL)
{
this->front = newNode;
}
else
{
nodeB->prev->next = newNode;
}
nodeB->prev = newNode;
}
void DLLIntStorage::insertAfter(int inValue, node *nodeB)
{
node *newNode;
newNode = new node();
newNode->next = nodeB->next;
newNode->prev = nodeB;
newNode->value = inValue;
if(nodeB->next == NULL)
{
this->back = newNode;
}
else
{
nodeB->next->prev = newNode;
}
nodeB->next = newNode;
}
void DLLIntStorage::insertFront(int inValue)
{
node *newNode;
if(this->front == NULL)
{
newNode = new node();
this->front = newNode;
this->back = newNode;
newNode->prev = NULL;
newNode->next = NULL;
newNode->value = inValue;
}
else
{
insertBefore(inValue, this->front);
}
}
void DLLIntStorage::insertBack(int inValue)
{
if(this->back == NULL)
{
insertFront(inValue);
}
else
{
insertAfter(inValue, this->back);
}
}
ifstream& operator>> (ifstream &in, DLLIntStorage &obj)
{
int readInt, counter = 0;
while(!in.eof())
{
if(counter==dataLength) //stops at 10,000
{
break;
}
in >> readInt;
if(obj.front != NULL)
{
obj.insertion(readInt);
}
else
{
obj.insertBack(readInt);
}
counter++;
}
return in;
}
void DLLIntStorage::insertion(int inValue)
{
node* temp;
temp = this->front;
if(temp->value >= inValue)
{
insertFront(inValue);
return;
}
else
{
while(temp->next!=NULL && temp!=this->back)
{
if(temp->value >= inValue)
{
insertBefore(inValue, temp);
return;
}
temp = temp->next;
}
}
if(temp == this->back)
{
insertBack(inValue);
}
}
谢谢你的时间。
您不能删除您的问题。完全通常的数据结构并不经常被盗用。 – Potatoswatter 2010-04-28 00:47:48
好的,谢谢,我想我的代码很防守。 – House 2010-04-28 00:56:28
为什么不使用标准模板库,这是非常好的。 – martsbradley 2010-04-28 19:32:19