gpt4 book ai didi

c++ - 无法弄清楚为什么我的双向链表分崩离析,我可以使用一些指导

转载 作者:行者123 更新时间:2023-11-28 05:18:03 25 4
gpt4 key购买 nike

<分区>

完全披露:这是我类(class)的一个项目,但我不是来要求你为我做作业的。我正在寻找一些关于我似乎遗漏的方向。

分配:来自格式化数据文件(见下文)。你要创建 3 个双向链表。 (因此一组数据具有三个“链”,按数字对数据进行排序)如果您遇到与前一条数据具有相同时间戳的一段数据,则该数据被认为是不可靠的,需要从中删除双向链表。

问题:我有四个链表头,timeHead、tempHead 和 windHead 用于从文件中读取的数据。最后一个是 duplHead (duplicateHead) 用于重复列表。我的问题是,每当我尝试从链表中删除特定节点时。我似乎无法正确地做到这一点。我要么使程序崩溃,要么双向链表崩溃。

这是我的代码:我觉得我的主要问题是没有正确创建列表或没有正确删除问题节点。

int main 函数只调用addData 和print report 两个函数。我已经包含了我认为相关的内容。

//ADD TO LINKED LIST
void linkedlist::addToLinkedList(weatherdata *newNode){

int doubleMarker = 0;
weatherdata *newNodeCopy;
weatherdata *currentNode;
weatherdata *nextNode;

newNodeCopy = new weatherdata; // <-- NEW
newNodeCopy = newNode;

/*_____ lINKED lIST FOR TIMESTAMP _____*/

//checks the duplicate list so as not to add a deleted triple
if (isItInDuplicateList(newNode) == 1){
doubleMarker = 1;
}

//if something exists in the list do this: traverse the list, check for duplicates.
if ((timeHead != nullptr) && (doubleMarker != 1)){

currentNode = timeHead;

while (currentNode != nullptr){

//if its the same as another item DELETE
if (newNode->time == currentNode->time) {
addToDuplicateList(newNode);
deleteNodeFromList(newNode);
doubleMarker = 1; // <-- this double marker will ensure that the function doesnt add a duplicate item
break;
}

currentNode = currentNode->timeN;
}
}
//if the incoming number is not a duplicate of something we already have on our list we add it
if (doubleMarker != 1){
//very first item on list
if (timeHead == nullptr){
timeHead = newNodeCopy;
}
//first position on list
else if (newNode->time < timeHead->time){
nextNode = timeHead;
timeHead = newNodeCopy;
newNodeCopy->timeN = nextNode;
nextNode->timeP = newNodeCopy;
}
//either between 2 entries or at the end of the list
else {
//traverse the list and find the appropriate placement for the newNode
currentNode = timeHead;
nextNode = timeHead->timeN;

//while "not yet at the end of the list"
while (nextNode != nullptr){

//newNode belongs somewhere in between two other entries
if ((currentNode->time < newNode->time) && (newNode->time < nextNode->time)){
currentNode->timeN = newNodeCopy;
newNodeCopy->timeP = currentNode;
newNodeCopy->timeN = nextNode;
nextNode->timeP = newNodeCopy;
break;
}
//otherwise increment currentNode and nextNode and compare again
else {
currentNode = nextNode;
nextNode = nextNode->timeN;
}
}
//newNode goes at the end of the linked List
if (nextNode == nullptr){
currentNode->timeN = newNodeCopy;
newNodeCopy->timeP = currentNode;
}
}
}

/*_____ lINKED lIST FOR TEMPERATURE _____*/

//if the incoming number is not a duplicate of something we already have on our list we add it
if (doubleMarker != 1){
//very first item on list
if (tempHead == nullptr){
tempHead = newNodeCopy;
}
//first position on list
else if (newNode->temp < tempHead->temp){
nextNode = tempHead;
tempHead = newNodeCopy;
newNodeCopy->tempN = nextNode;
nextNode->tempP = newNodeCopy;
}
//either between 2 entries or at the end of the list
else {
//traverse the list and find the appropriate placement for the newNode
currentNode = tempHead;
nextNode = tempHead->tempN;

//while "not yet at the end of the list"
while (nextNode != nullptr){

//newNode belongs somewhere in between two other entries
if ((currentNode->temp <= newNode->temp) && (newNode->temp <= nextNode->temp)){
currentNode->tempN = newNodeCopy;
newNodeCopy->tempN = nextNode;
nextNode->tempP = newNodeCopy;
break;
}
//otherwise increment currentNode and nextNode and compare again
else {
currentNode = nextNode;
nextNode = nextNode->tempN;
}
}
//newNode goes at the end of the linked List
if (nextNode == nullptr){
currentNode->tempN = newNodeCopy;
newNodeCopy->tempP = currentNode;
}
}
}

/*_____ lINKED lIST FOR WINDSPEED _____*/

//if the incoming number is not a duplicate of something we already have on our list we add it
if (doubleMarker != 1){
//very first item on list
if (windHead == nullptr){
windHead = newNodeCopy;
}
//first position on list
else if (newNode->wind < windHead->wind){
nextNode = windHead;
windHead = newNodeCopy;
newNodeCopy->windN = nextNode;
nextNode->windP = newNodeCopy;
}
//either between 2 entries or at the end of the list
else {
//traverse the list and find the appropriate placement for the newNode
currentNode = windHead;
nextNode = windHead->windN;

//while "not yet at the end of the list"
while (nextNode != nullptr){

//newNode belongs somewhere in between two other entries
if ((currentNode->wind <= newNode->wind) && (newNode->wind <= nextNode->wind)){
currentNode->windN = newNodeCopy;
newNodeCopy->windN = nextNode;
nextNode->windP = newNodeCopy;
break;
}
//otherwise increment currentNode and nextNode and compare again
else {
currentNode = nextNode;
nextNode = nextNode->windN;
}
}
//newNode goes at the end of the linked List
if (nextNode == nullptr){
currentNode->windN = newNodeCopy;
newNodeCopy->windP = currentNode;
}
}
}
}

//ADD TO DUPLICATE LIST
void linkedlist::addToDuplicateList(weatherdata *duplicateNode){

weatherdata *currentNode;
weatherdata *nextNode;
weatherdata *addDuplicateNode;

addDuplicateNode = new weatherdata; // <-- NEW

//make a complete copy for the duplicate list (since were going to delete that node)
addDuplicateNode->time = duplicateNode->time;
addDuplicateNode->temp = duplicateNode->temp;
addDuplicateNode->wind = duplicateNode->wind;
addDuplicateNode->timeN = duplicateNode->timeN;
addDuplicateNode->timeP = duplicateNode->timeP;
addDuplicateNode->tempN = duplicateNode->tempN;
addDuplicateNode->tempP = duplicateNode->tempP;
addDuplicateNode->windN = duplicateNode->windN;
addDuplicateNode->windP = duplicateNode->windP;
addDuplicateNode->duplN = duplicateNode->duplN;

if (duplHead == nullptr){
duplHead = addDuplicateNode;
}
else {
currentNode = duplHead;
nextNode = duplHead->duplN;

while (nextNode != nullptr){
currentNode = nextNode;
nextNode = nextNode->duplN;
}

currentNode->duplN = addDuplicateNode;
}
}


/DELETE FROM LINKEDLIST
void linkedlist::deleteNodeFromList(weatherdata *toBeDeletedNode){

weatherdata *currentNode;
weatherdata *nextNode;

currentNode = timeHead;
nextNode = timeHead->timeN;

while (nextNode != nullptr){

if (nextNode->time == toBeDeletedNode->time){

currentNode->timeN = nextNode->timeN;
//currentNode->tempN = nextNode->tempN;
//cout << ".";
delete toBeDeletedNode;
toBeDeletedNode = nullptr;
break;
}

currentNode = nextNode;
nextNode = nextNode->timeN;
}
}

//DUPLICATE LIST CHECK
bool linkedlist::isItInDuplicateList(weatherdata *checkThisNode){

bool found = false;

weatherdata *currentNode;
currentNode = duplHead;

if (duplHead == nullptr){
found = false;
}
else {

do {

if (currentNode->time == checkThisNode->time) {
found = true;
break;
}
currentNode = currentNode->duplN;
} while (currentNode != nullptr);
}

return found;
}

所以要么第一个(长)函数linkedlist addToLinkedList();或最后一个(短)函数 linkedlist deleteNodeFromList();

如果您需要我再发布代码,请告诉我,我会这样做的。同样,我觉得我要么没有正确制作双向链表,要么没有正确删除它。

再次感谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com