【发布时间】:2020-08-30 06:34:50
【问题描述】:
之前的问题已解决!
新问题:代码本身。我一直在研究从链接列表中删除选定项目的函数的代码,它可能出现多少次。但是,当我运行该部分程序以尝试删除节点时,它只会结束程序。我知道肯定会发生一些事情,但是即使查看有关此的教程,我也不太了解如何实现此指令。任何帮助将不胜感激。 [已解决]
功能
void LinkedList::deleteItem(int _newItem)
{
if(isEmptyList())
cout << "\t<ERROR> List is empty.\n";
else
{
bool itemDelete = false;
nodeType *q = first;
while(q != NULL)
{
if(first->info == _newItem)
{
nodeType *p = first->link;
first->link = p->link;
delete p;
--count;
itemDelete = true;
}
if(q->link->info == _newItem)
{
nodeType *r = q;
nodeType *p = q;
r = r->link;
p->link = r->link;
delete r;
--count;
itemDelete = true;
}
q = q->link;
}
if(itemDelete == true)
cout << "Item was deleted.";
else
cout << "Item was not found.";
}
}
类和结构
struct nodeType
{
int info;
nodeType *link;
};
class LinkedList
{
public:
void initializeList();
bool isEmptyList();
void printList();
int findLength();
void destroyList();
int infoFirst();
int infoLast();
bool searchItem(int);
void insertFront(int);
void insertBack(int);
void deleteItem(int);
int calcTotal();
int calcAvg();
LinkedList();
private:
nodeType *first, *last, *newNode;
int count; //adds or remove one whenever a node is added or removed
};
【问题讨论】:
-
正是以下消息:“无法打开输出文件(文件夹名称).exe 访问被拒绝”、“Id 返回 1 退出状态”和“目标(文件夹名称).exe 的配方失败”。
-
这个错误不是由你的代码引起的(不是说你的代码很好)。这意味着链接器无法写入可执行文件,这可能是由于在早期版本运行时尝试构建代码而导致的。确保在重建程序之前终止所有正在运行的程序实例。
-
所以总结一下:不,您删除节点的代码不正确,但这不会导致您的链接错误。您只是没有写入输出文件的权限。上次编译成功是什么时候?可能正如 Bessie 建议的那样,当前正在运行的程序。
-
嗯,确实如此。没有注意到早期版本仍在运行。当我打开太多东西时,就会发生这种情况。谢谢,贝西!
-
约翰,这有什么不对?
标签: c++ class struct makefile linked-list