单链表内存泄露问题
#include <iostream>#include <assert.h>#include <stdlib.h>#include <crtdbg.h>using namespace std;template<typename T>class CList;template<typename T>class CNode{public: CNode(){m_pNext = NULL;} //默认构造函数 CNode(const T &data){m_pNext = NULL; m_data = data; }//赋值 ~CNode(){} friend class CList;private: void AddAfter(T *node);//在该节点后面插入一个节点 void DelAfter(T *node);//删除该节点后面的一个节点 T m_data; CNode<T> * m_pNext;public: T GetData(){return m_data;} void SetData(T data){m_data = data;}};//CList-----------------------------template<typename T>class CList{private: CNode<T> *head;public: CList(); CList(CNode<T> &node); ~CList(); void AddNode(CNode<T> *node);//在最后面添加一个新的节点. void AddNodeAt(CNode<T> *node, int n);//把node插入到索引为n的节点 CNode<T>* DelNodeAt(int n);//删除第n个节点,并返回 void DisplayAllNode();//显示所有的节点数据};void main(){ CList<int> myList; int i; for (i = 0; i < 10; i++) { myList.AddNode(new CNode<int>(i)); } myList.DisplayAllNode(); myList.AddNodeAt(new CNode<int>(100),6); myList.DisplayAllNode();// CNode<int>* abc = new CNode<int>(3333); // delete abc; _CrtDumpMemoryLeaks();}//默认构造函数template<typename T>CList<T>::CList(){ head = new CNode<T>();}//析构函数template<typename T>CList<T>::~CList(){ if(head != NULL) { CNode<T>* p = head; CNode<T>* q = NULL; //delete head; while (p != NULL) { q = p; p = p->m_pNext; delete q; q = NULL; } }}//通过节点来初始化一个链表template<typename T>CList<T>::CList(CNode<T> &node){ head = new CNode<T>(); head->m_pNext = &node;}//在末尾插入template<typename T>void CList<T>::AddNode(CNode<T> *node){ //assert(node != NULL); CNode<T> *temp = head; while (temp->m_pNext != NULL) { temp = temp->m_pNext; } temp->m_pNext = node;}//在指定节点插入template<typename T>void CList<T>::AddNodeAt(CNode<T> *node, int n){ //assert(node != NULL || n > 0); CNode<T> *temp = head; CNode<T> *nextNext; int i = 0; while (i != n && temp != NULL) { temp = temp->m_pNext; i++; }//next指向的是n的前一个节点,head的索引可以认为是-1. if (i == n) { nextNext = temp->m_pNext; temp->m_pNext = node; node->m_pNext=nextNext; }}//输出所有的节点内容template<typename T>void CList<T>::DisplayAllNode(){ //输出内容, 头节点要跳过, 因为头节点没有内容 CNode<T> *temp = head->m_pNext; cout<<"输入所有的数据"<<endl; while (temp != NULL) { cout<<"temp->GetData(): "<<temp->GetData()<<endl; temp = temp->m_pNext; }}//删除第n个节点, 并返回template<typename T>CNode<T>* CList<T>::DelNodeAt(int n){ //assert( n > 0); CNode<T> *temp = head; CNode<T> *nextNext; int i = 0; while (i != n && temp != NULL) { temp = temp->m_pNext; i++; }//next指向的是n的前一个节点,head的索引可以认为是-1. if (i == n) { //temp->m_pNext } return NULL;}
Data: < .8 > 07 00 00 00 E0 2E 38 00
{62} normal block at 0x00381470, 8 bytes long.
Data: < .8 > 06 00 00 00 98 2E 38 00
{61} normal block at 0x00381428, 8 bytes long.
Data: < x18 > 05 00 00 00 78 31 38 00
{60} normal block at 0x003813E0, 8 bytes long.
Data: < ( 8 > 04 00 00 00 28 14 38 00
{59} normal block at 0x00381398, 8 bytes long.
Data: < 8 > 03 00 00 00 E0 13 38 00
{58} normal block at 0x00381350, 8 bytes long.
Data: < 8 > 02 00 00 00 98 13 38 00
{57} normal block at 0x00381308, 8 bytes long.
Data: < P 8 > 01 00 00 00 50 13 38 00
{56} normal block at 0x003812C0, 8 bytes long.
Data: < 8 > 00 00 00 00 08 13 38 00
{55} normal block at 0x00381278, 8 bytes long.
Data: < 8 > CD CD CD CD C0 12 38 00
Object dump complete.
程序“[3360] templateeee.exe: 本机”已退出,返回值为 0 (0x0)。
[解决办法]
我vs2010跑没问题啊?