gpt4 book ai didi

c++ - 删除链表中的节点不起作用?

转载 作者:行者123 更新时间:2023-11-30 04:05:56 24 4
gpt4 key购买 nike

我正在使用 C++ 中的模板来创建链表。我已经编写了一些函数,但是删除节点(从头/尾)无法正常工作。它显示错误并终止。调试时,监 window 口中显示的“this”包含一个空头,我不知道为什么。这是我的代码:

SLlistSc.h

#ifndef SLlistSc_H
#define SLlistSc_H

#include<iostream>
using namespace std;

template<class T> class SLlist;

template <class T> class snode{
friend class SLlist<T>;
private:
T info;
snode<T> *next;
public:
inline T getter() const { return info; }
inline void setter(T setValue){ info = setValue; }
snode(){ info = 0; next = 0; }
snode(T inf , snode *nex = 0){ info = inf; next = nex; }
~snode(){ delete next; }

};
template <class T> class SLlist{
private:
snode<T> *head, *tail;
static int total;
public:
SLlist(){ head = tail = 0; }

~SLlist(){
snode<T> *p = head;
while (head != 0){
head = head->next;
delete p;
p = head;
}
}
inline void incrcnt(){ total++; }
inline void decrcnt(){ total--; }
void displayList();
void addToHead(T inf);
void addToTail(T inf);
T deleteFromHead();
T deleteFromTail();
int returnIndex();
void deleteInfo();
void deleteAll();
};

template <class T> void SLlist<T>::displayList(){
snode<T> *p = head;
while (p != 0){
cout << "\n->" << p->info;
p = p->next;
}
return;
}

template <class T> void SLlist<T>::addToHead(T inf){
snode<T> *temp = new snode<T>(inf, 0);
if (head == 0){ head = tail = temp; temp = 0; delete temp; incrcnt(); return; }
else { temp->next = head; head = temp; temp = 0; delete temp; incrcnt(); return; }
}

template <class T> void SLlist<T>::addToTail(T inf){
snode<T> *temp = new snode<T>(inf, 0);
if (head == 0){ head = tail = temp; temp = 0; delete temp; incrcnt(); return; }
else{ tail->next = temp; tail = temp; temp = 0; delete temp; return; }
}

template <class T> T SLlist<T>::deleteFromHead(){
if (head == 0){ cout << "\nList is already empty"; return (T)0; }
if (head == tail){ delete head; head = tail = 0; T info = head->info; return info; }
else {
T info = head->info;
snode<T> *temp = head;
head = head->next;
temp = 0;
delete temp;
return info;
}
}

template <class T> T SLlist<T>::deleteFromTail(){
if (head == 0){ cout << "\nList is already empty"; return (T)0; }
if (head == tail){ delete head; head = tail = 0; T info = head->info; return info; }
else {
T info = tail->info;
snode<T> *temp = head;
while (temp != 0)
{
temp = tail;
}
delete tail;
tail = temp;
temp = 0; delete temp;
return (T)info;
}
}

#endif

和cpp文件:SLlistSc.cpp

// SLlistSc.cpp : Defines the entry point for the console application.
//

#include"SLlistSc.h"
#include<iostream>
using namespace std;

template <class T> int SLlist<T>::total = 0;

void main()
{
cout << "\t\t\t Singly Linked List Super Class Implementation";
SLlist<int> List1;
int userchoice=0, inf=0;
do{
cout << "\n\n\t\t Menu"
<< "\n\t1. Display List"
<< "\n\t2. Add to Head"
<< "\n\t3. Add to tail"
<< "\n\t4. Delete from Head"
<< "\n\t5. Delete from Tail"
<< "\n\t6. Exit";
cin >> userchoice;

switch (userchoice){
case 1: List1.displayList(); break;
case 2: cout << "\nEnter info to be added:";
cin >> inf;
List1.addToHead(inf); break;
case 3: cout << "\nEnter info to be added:";
cin >> inf;
List1.addToTail(inf); break;
case 4: inf = List1.deleteFromHead();
cout << "\n Value" << inf << "deleted from list"; break;
case 5: inf = List1.deleteFromTail();
cout << "\n Value" << inf << "deleted from list"; break;
case 6: cout << "\n\t\t\t\tExiting";
exit(0);
default: continue;
}
} while (userchoice < 4);
}

我想问的另一件事是,当我们在类(例如类 A)上使用模板时,我们需要在所有地方将 A 替换为 A。是因为编译器生成了一个新类即。

A<T> isnt = A.

而在 snode 类中使用 friend 类 SLlist 时,为什么我必须声明如下:

template<class T> class SLlist;

在 snode 类之前?为什么我不能直接在 snode 类中将其声明为:

friend class SLlist<T>;

此外,如果我的代码有改进的机会,请加倍努力。谢谢。

最佳答案

您正试图使用​​一个无效的指针,因为包含该指针的 block 已被释放。

Microsoft C++ 调试运行时库使用模式 0xFEEEFEEE 覆盖释放的内存,这使得此问题更容易识别。

关于c++ - 删除链表中的节点不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23073652/

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