c++ - C++ 链表断点错误与删除
问题描述
嘿,我正在尝试制作一个链表,计算从头到尾添加元素所需的时间。然后从头到尾删除then。我有添加和删除的代码,但是当我从列表中删除最后一个元素时,我遇到了一个涉及内存分配的断点错误。我猜我需要在列表为空时添加故障保护,但我不知道该怎么做。
一些建议将不胜感激,因为我对此很陌生。
谢谢!
我的列表类代码:
class list
{
public:
struct node {
int data;
struct node* next;
} *head, *tail;
list() :head(NULL), tail(NULL) {} // constructor
~list() {
node* current, * temp;
current = head;
temp = head;
while (current != NULL) {
current = current->next;
delete temp;
temp = current;
}
}
// adding to the end of list
void addToEnd(int n) {
node* newNode = new node;
newNode->data = n;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
}
node* cur = head;
while (cur) {
if (cur->next == NULL) {
cur->next = newNode;
return;
}
cur = cur->next;
}
}
//Add to beginning
void addToBeginning(int n) {
node* newNode = new node;
newNode->data = n;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
}
else {
newNode->next = head;
head = newNode;
}
}
void deleteFromBeginning() {
if (head != NULL) {
node* temp = head->next;
delete head;
head = temp;
}
if (head == NULL) {
cout << "Nothing to delete" << endl;
return;
}
}
void deleteFromEnd() {
node* removeLastNode = head;
if (head == NULL)
return;
if (head->next == NULL) {
delete head;
return;
}
// Find the second last node
node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
// Delete last node
delete (second_last->next);
// Change next of second last
second_last->next = NULL;
}
//Display List
void displayList() {
if (head != NULL) {
// This was also triggering an access violation error when I tried to run this after the delete loop
for (node* temp = head; temp != NULL; temp = temp->next) {
cout << temp->data << " ";
}
cout << endl;
}
}
};
我想将它保存在一个单独的类中,因为它将成为更大程序的一部分,并且我想最小化主源文件上的代码。
这是主要的代码:
int main() {
int data;
list myList;
for (int i = 0; i < 10; i++){
myList.addToBeginning(i);
}
myList.displayList();
for (int i = 0; i < 5; i++) {
myList.deleteFromBeginning();
}
myList.displayList();
for (int i = 0; i < 10; i++) {
myList.addToEnd(i);
}
myList.displayList();
for (int i = 0; i < 15; i++) {
myList.deleteFromEnd(); // Error comes here
}
}
解决方案
必须检查列表是否有多个项目,然后启动 while 循环。当我只剩下 1 个节点时,它导致了一个问题。
void deleteFromEnd() {
node* temp;
if (head == NULL)
{
cout << " There is no item to delete!" << endl;
return;
}
node* start = head;
if (start->next != NULL)
{
while ((start->next)->next != NULL)
{
start = start->next;
}
temp = start->next;
start->next = NULL;
}
else
{
temp = start;
head = NULL;
}
delete temp;
}
推荐阅读
- c# - 如何使相机仅在角色移动中单独使用鼠标转动玩家?
- asp.net-mvc - 客户端应用程序如何使用从访问令牌中提取的范围和资源来限制 API 的访问 - identityserver4
- python - 使用 NLTK 标记 CSV 文件时出错,我将如何解决这个问题?
- javascript - 获取相对于父容器的focusOffset
- firefox-addon-webextensions - Web 扩展 - 可选 content_scipt 权限?
- python-3.x - 使用 Python 3 和 re.sub 将图像转换为 Base64 字符串,然后用循环中的编码字符串替换每个匹配的图像路径
- java - Maven 部署到 Github 包 UNAUTHORIZED 401
- python - python中的煎饼排序,并为空max()错误消息
- react-native-sqlite-storage - 我们如何将 RNSqlite db 存储在外部存储中,例如谷歌驱动器而不是内部存储?
- node.js - 如何从猫鼬的对象数组中删除一个对象?