I am using templates in c++ to create a linked list. I have coded a few functions , but the deletion of node ( either from head/tail ) isnt working properly. It displays an error and terminates. When debugging , the 'this' shown in watch windows contains a head which is null, which i have no idea why. Here is my code:
我在c++中使用模板创建一个链表。我编写了一些函数,但是删除节点(头/尾)不能正常工作。它显示一个错误并终止。当调试时,watch windows中显示的“this”包含一个空头,我不知道这是为什么。这是我的代码:
SLlistSc.h
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
and the cpp file : SLlistSc.cpp
和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);
}
Another thing i would like to ask is , when we use templates on a class (eg. class A) , we are required to replace the A with A everywhere. Is it because compiler generates a new class ie.
我想问的另一个问题是,当我们在一个类上使用模板时(如。A类),我们需要用A来代替A。是因为编译器生成了一个新类ie。
A<T> isnt = A.
< T >不是=。
And while using friend class SLlist inside snode class , why do i have to declare as follows :
在snode类中使用friend类SLlist时,为什么要声明如下:
template<class T> class SLlist;
before the snode class? why cant i declare it directly in the snode class as :
在综述类?为什么我不能直接在snode类中声明它:
friend class SLlist<T>;
Also if there is a chance of improvemet in my code , please go hard on me. Thanks.
如果我的代码有改进的机会,请对我严厉一点。谢谢。
1 个解决方案
#1
2
You are attempting to use a pointer that is invalid because the block containing that pointer has been freed.
您正在尝试使用一个无效的指针,因为包含该指针的块已被释放。
The Microsoft C++ debug runtime library overwrites freed memory with the pattern 0xFEEEFEEE which makes this problem easier to identify.
Microsoft c++调试运行时库覆盖了释放内存和模式0xFEEEFEEE,这使得这个问题更容易识别。
#1
2
You are attempting to use a pointer that is invalid because the block containing that pointer has been freed.
您正在尝试使用一个无效的指针,因为包含该指针的块已被释放。
The Microsoft C++ debug runtime library overwrites freed memory with the pattern 0xFEEEFEEE which makes this problem easier to identify.
Microsoft c++调试运行时库覆盖了释放内存和模式0xFEEEFEEE,这使得这个问题更容易识别。