谁帮我看下这段代码为什么出现内存问题....谢谢了(约瑟夫环问题)
// 单链表的模版实现.cpp : 定义控制台应用程序的入口点。
//
// 模版技术实现单向链表.cpp : Defines the entry point for the console application.
//
//不带头结点
//
能准确输出,但关闭后提示内存泄露,谁帮忙看看
#include "iostream"
#include "cstdlib"
#include <iomanip>
#include <stdexcept>
using namespace std;
template<typename T>class LinkList;
template<typename T>
class Node
{
friend class LinkList<T>;
private:
Node<T> *next;
T value;
public:
Node():next(NULL){NULL;}
explicit Node(T &val):value(val),next(NULL){NULL;}
//~Node();
T &getValue(){return value;}
Node *getNext(){return next;}
};
template<typename T>
class LinkList
{
private:
Node<T> *pHead;
public:
LinkList();
~LinkList();
void clear(void);
void add(T &val);
void visit() const;
void Create();
void Josephus();
};
template<typename T>
void LinkList<T>::Create()
{
int num;
cout<<"请输入要建立的结点个数:";
cin>>num;
Node<T> *pMove=pHead;
for(int i=0;i<num;++i)
{
Node<T> *pNew=new Node<T>();
cin>>pNew->value;
pMove->next=pNew;
pMove=pNew;
}
pHead=pHead->next;
pMove->next=pHead;
}
template<typename T>
ostream &operator <<(ostream &os,const LinkList<T> L)
{
L.visit();
return os;
}
template<typename T>
void LinkList<T>::visit() const
{
Node<T> *pNow=pHead;
while(pNow->next!=pHead)
{
cout<<pNow->value<<" ";
pNow=pNow->next;
}
}
template<typename T>
void LinkList<T>::add(T &val)
{
Node<T> *pNew=new Node<T>(val);
Node<T> *p=pHead;
pNew->next=pHead->next;
pHead->next=pNew;
}
template<typename T>
void LinkList<T>::clear()
{
for(Node<T> *pDel=pHead->next;pDel!=NULL;pDel=pHead->next)
{
pHead->next=pDel->next;
#if !defined(NDEBUG)
std::cout << "删除值:" << pDel->value << std::endl;
#endif
delete [] pDel;
}
}
template<typename T>
LinkList<T>::LinkList()
{
pHead=new Node<T>();
}
template <typename T>
LinkList<T>::~LinkList(void)
{
clear();
delete pHead;
}
template<typename T>
void LinkList<T>::Josephus()
{
//Node<T> *pPrev;
Node<T> *pPrev;
Node<T> *pDel=pHead;
int n;
cout<<"请输入要删除数到的个数:";
cin>>n;
while(pDel->next!=pDel)
{
for(int i=1;i<n;++i)
{
pPrev=pDel;
pDel=pDel->next;
}
pPrev->next=pDel->next;
cout<<"要删除的元素为:"<<pDel->value<<endl;
delete [] pDel;
pDel=pPrev->next;
}
cout<<"最后一个元素为:"<<pDel->value;
delete [] pDel;
}
int main()
{
LinkList<int> L;
L.Create();
//L.visit();
L.Josephus();
system("pause");
return 0;
}
[解决办法]
delete [] pDel;
不解???