gpt4 book ai didi

c++ - 链表和动态分配挫折c++

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:49:46 27 4
gpt4 key购买 nike

引用代码如下:

#include <iostream>
using namespace std;

class linkedList {

struct listNode{ //a node of a list
int value;
struct listNode *next;
};

listNode *head;

public:
linkedList(){
cout << "hello1\n";
head = NULL;
};

linkedList(listNode* a){
cout << "hello2\n";
head = a;
};

~linkedList();
listNode* getHead() {return head;}

void appendNode(int);

//inline Search function due to unable to function outside of class definition
listNode* rangeSearch(int a, int b){
//listNode to search
listNode *search = head;
//listNode* toReturn = new listNode;
//listNode to return list of values that are found within range
linkedList *found = new linkedList;

while(search){
//if the current value is within range, then add to list
if(search->value >= a && search->value <= b){
//append searched value onto found
found->appendNode(search->value);
//after appending, go to next value
}
search = search->next;
}

return found->getHead();
}

void display();
};



int main()
{
cout << "Programmer : n\n";
cout << "Description : \n";
linkedList* list = new linkedList;
int x = 12;
//values to search
int s1 = 10, s2 = 14;

// adds 2 to each number on list for 5 times
for(int i = 0; i < 5; i++){
list->appendNode(x);
x += 2;
}

//create something to hold pointer of found to be deleted when done using

//print list
cout << "Original set of numbers in linked list: ";
list->display();
cout << "\nThe following are the values withing ranges: " << s1 << " and " << s2 << ":\n";

//EDITED:
//list->rangeSearch(s1,s2);
linkedList foundList(list->rangeSearch(s1,s2));
foundList.display();
//End of edit 6:40PM 7/18/13

cout << "\nHere are the original set of numbers in linked list (again): ";
list->display();
delete list;
return 0;
}


void linkedList::appendNode(int newValue)
{
listNode *newNode = new listNode(); // To point to a new node
listNode *nodePtr; // To move through the list

// Allocate a new node and store newValue there.
newNode->value = newValue;
newNode->next = 0;

// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;

// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;

// Insert newNode as the last node.
nodePtr->next = newNode;
}
}

void linkedList::display() {
for(listNode* p = head; p != NULL; p = p->next)
cout << p->value << ' ';
}

linkedList::~linkedList()
{
cout << "\ndestructor called";
listNode *nodePtr; // To traverse the list
listNode *nextNode; // To point to the next node

// Position nodePtr at the head of the list.
nodePtr = head;

// While nodePtr is not at the end of the list...
while (nodePtr != NULL)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;

// Delete the current node.
delete nodePtr;

// Position nodePtr at the next node.
nodePtr = nextNode;
}
}

这里有几个问题。首先,为什么当我尝试将 rangeSearch 成员函数放在类定义之外时,编译器会报错说 listNode* type is not recognized?

其次,这与析构函数有关。在这个程序中,创建了 2 个实例(列表和找到的列表),但只调用了 1 个析构函数。有人可以解释为什么吗?我的直觉告诉我,动态分配的指向 linkedList 对象的指针没有被破坏。但是,我不知道为什么。我必须使用动态分配内存的原因主要是因为我想将指针传回主函数。如果我不这样做,当 rangeSearch 退出时,指针将被传回 main 但指针所具有的任何列表都会在之后被解构 返回指针; (假设 ptr 是指向 rangeSearch 中声明的 linkedList 的指针)这将导致我的程序崩溃,因为现在地址中什么都没有,我正在尝试调用......什么都没有。

好吧,像往常一样,我会很感激那里的伟大撒玛利亚人,如果他愿意对我进行更多这方面的教育。

最佳答案

首先,您在范围界定方面遇到了问题。在 C++ 中,大括号定义了一个新范围,因此您在类 linkedlist 中定义了 listNode。如果你想访问它,你必须使用作用域运算符 linkedlist::listNode

我不完全理解你的第二个问题。我只看到一个 delete 调用,那么你认为为什么会调用两个析构函数?析构函数仅在您调用 delete 时调用,因此除非您指定要销毁它,否则它仍会存在。

虽然我不完全理解你的问题,但我看到你在 rangeSearch 中返回了一个指向头部的指针,但你没有将它分配给任何东西。这意味着您将发生内存泄漏;您为找到的对象分配了内存,但随后不对其进行任何操作。实际上,由于您只返回头部,如果您确实向它分配了一些内容,您仍然无法删除它,因为您无法访问链表本身。

关于c++ - 链表和动态分配挫折c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17736465/

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