#include<iostream>
using namespace std;
//C++实现无头节点链表
class Node
{
public:
Node(int v):m_value(v),m_next(NULL){}
Node():m_next(NULL){}
int m_value;
Node *m_next;
};
class List
{
public:
List():m_pHead(NULL),m_pTail(NULL){}
~List(){Clear();}
void InsertHead(int v);//头插
void InsertTail(int v);//尾插
void InsertIvalue(int i,int v);//按位置插入
void DeleteHead();//头删
void DeleteTail();//尾删
void DeleteIvalue(int i);//按值删除
Node *GetIP(int i);//获取链表中某个节点中的值是链表中的第几个
int GetLength();//求长度
void Print();//打印函数
bool IsEmpty(){return m_pHead == NULL ? true : false;}//判空
void Sort();//排序
void Reverse();//逆置
void Clear();//清除
private:
Node *m_pHead;
Node *m_pTail;
};
void List::Clear()
{
//方法1 /*Node *p=m_pHead;
while(p!=NULL)
{
p=p->m_next;
DeleteHead();
}
delete p;
p=NULL;*/
//方法2
Node *p=m_pHead;
Node *q=p;
while(p!=NULL)
{
p=p->m_next;
delete q;
q=p;
}
m_pHead=NULL;
}
void List::Delete
数据结构之C++实现无头节点链表(List)(无主函数)
最新推荐文章于 2022-05-23 11:46:39 发布
