我在c ++中使用模板来创建链表。我编写了一些函数,但删除节点(从头部/尾部)无法正常工作。它显示错误并终止。在调试时,这个'在监视窗口中显示包含一个null的头,我不明白为什么。这是我的代码:
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&LT; T&GT; isnt = A.
在snode类中使用友元类SLlist时,为什么我必须声明如下:
template<class T> class SLlist;
在snode课程之前?为什么我不能直接在snode类中声明它:
friend class SLlist<T>;
如果我的代码中有可能改进,请继续努力。感谢。
答案 0 :(得分:2)
您正在尝试使用无效的指针,因为已释放包含该指针的块。
Microsoft C ++调试运行时库使用模式0xFEEEFEEE覆盖释放的内存,这使得此问题更容易识别。