C++类中单链表的实现(头插、尾插、头删、尾删、指定位置插入、指定位置删除、链表长度、清空链表、链表排序)
来源:互联网 发布:淘宝种子暗语 编辑:程序博客网 时间:2024/06/06 07:51
#include<iostream>
using namespace std;
class Node
{
public:
Node():next(NULL){}
Node(int n,Node *p = NULL):value(n),next(p){}
int value;
Node *next;
};
class List
{
public:
static int len; //对链表的长度进行累积
List():m_pHead(NULL),m_pTail(NULL){};
~List(){Clear();};
void InsertHead(int v) //头部插入
{
Node *p = new Node(v);
if(m_pHead == NULL)
{
m_pHead=p;
m_pTail=p;
}
else
{
p->next = m_pHead;
m_pHead = p;
}
++len;
}
void InsertTail(int v) //尾部插入
{
Node *p = new Node(v);
if(m_pHead==NULL)
{
m_pTail=m_pTail=p;
}
else
{
m_pTail->next=p;
m_pTail=p;
}
++len;
}
void InsertPosValue(int p,int v) //在p位置插入v值
{
Node *s = new Node(v);
Node *x=m_pHead;
int n=1;
Node *q=GetIp(p);
if(q==NULL)
return;
else
{
if(x==q) //输入的位置为1
InsertHead(v);
else
{
while(x->next!=q)
x=x->next;
x->next=s;
s->next=q;
++len;
}
}
}
void DeleteHead() //头删
{
if(m_pHead==NULL)
{
cout<<"链表为空,无法删除头!"<<endl;
return;
}
else
{
Node *p=m_pHead;
m_pHead=p->next;
delete p;
p=NULL;
}
--len;
}
void DeleteTail() //尾删
{
if(m_pHead==NULL)
{
cout<<"链表为空,无法进行尾删操作!"<<endl;
return;
}
else
{
Node *p=m_pHead;
while(p->next!=m_pTail)
{
p=p->next;
}
delete m_pTail;
m_pTail=p;
m_pTail->next=NULL;
}
--len;
}
void DeletePosValue(int p) //指定位置删除
{
Node *s=m_pHead;
Node *q=GetIp(p);
if(q==NULL)
return;
else
{
if(s==q) //删除的是头
DeleteHead();
else
{
while(s->next!=q)
s=s->next;
s->next=q->next;
delete q;
q=NULL;
}
}
--len;
}
Node *GetIp(int i)
{
Node *q=m_pHead;
if(i<=0)
{
cout<<"输入位置不合法!"<<endl;
return NULL;
}
else if(i>Length())
{
cout<<"输入位置大于链表长度!"<<endl;
return NULL;
}
else
{
int n=1;
while( n!=i)
{
q=q->next;
n++;
}
return q;
}
}
Node *GetHead() //获得头指针
{
if(m_pHead==NULL)
{
cout<<"链表为空,无法获得头地址!"<<endl;
return NULL;
}
else
return m_pHead;
}
void Clear() //清空链表
{
Node *p=m_pHead;
while(m_pHead!=NULL)
{
m_pHead=p->next;
delete p;
p=m_pHead;
}
len=0;
}
int Length() //求长度
{
return len;
}
void Sort() //从小到大排序
{
Node *q;
int n;
if(m_pHead==NULL || m_pHead->next==NULL)
return;
else
{
for(int i=0;i<Length()-1;++i)
{
for(int j=0;j<Length()-1-i;++j)
{
Node *p=m_pHead;
while(p->next!=NULL)
{
if(p->value>p->next->value)
{
n=p->value;
p->value=p->next->value;
p->next->value=n;
}
p=p->next;
}
}
}
}
}
void show()
{
if (m_pHead==NULL)
{
cout<<"链表为空!"<<endl;
return;
}
else
{
Node *p=m_pHead;
while(p!=NULL)
{
cout<<p->value<<"-->";
p=p->next;
}
cout<<"end"<<endl;
}
}
private:
Node *m_pHead;
Node *m_pTail;
};
int List::len=0;
void main()
{
List li;
li.InsertHead(9);
li.InsertHead(0);
li.InsertHead(3);
li.InsertHead(8);
li.InsertHead(2);
li.InsertHead(4);
li.InsertHead(5);
li.InsertTail(7);
li.InsertPosValue(1,6);
li.InsertPosValue(34,99);
li.InsertPosValue(9,34);
li.show();
li.DeleteHead();
li.DeleteTail();
li.show();
li.DeletePosValue(1);
cout<<li.GetHead()<<endl;
cout<<li.Length()<<endl;
li.Sort();
li.show();
li.Clear();
cout<<li.Length()<<endl;
li.show();
using namespace std;
class Node
{
public:
Node():next(NULL){}
Node(int n,Node *p = NULL):value(n),next(p){}
int value;
Node *next;
};
class List
{
public:
static int len; //对链表的长度进行累积
List():m_pHead(NULL),m_pTail(NULL){};
~List(){Clear();};
void InsertHead(int v) //头部插入
{
Node *p = new Node(v);
if(m_pHead == NULL)
{
m_pHead=p;
m_pTail=p;
}
else
{
p->next = m_pHead;
m_pHead = p;
}
++len;
}
void InsertTail(int v) //尾部插入
{
Node *p = new Node(v);
if(m_pHead==NULL)
{
m_pTail=m_pTail=p;
}
else
{
m_pTail->next=p;
m_pTail=p;
}
++len;
}
void InsertPosValue(int p,int v) //在p位置插入v值
{
Node *s = new Node(v);
Node *x=m_pHead;
int n=1;
Node *q=GetIp(p);
if(q==NULL)
return;
else
{
if(x==q) //输入的位置为1
InsertHead(v);
else
{
while(x->next!=q)
x=x->next;
x->next=s;
s->next=q;
++len;
}
}
}
void DeleteHead() //头删
{
if(m_pHead==NULL)
{
cout<<"链表为空,无法删除头!"<<endl;
return;
}
else
{
Node *p=m_pHead;
m_pHead=p->next;
delete p;
p=NULL;
}
--len;
}
void DeleteTail() //尾删
{
if(m_pHead==NULL)
{
cout<<"链表为空,无法进行尾删操作!"<<endl;
return;
}
else
{
Node *p=m_pHead;
while(p->next!=m_pTail)
{
p=p->next;
}
delete m_pTail;
m_pTail=p;
m_pTail->next=NULL;
}
--len;
}
void DeletePosValue(int p) //指定位置删除
{
Node *s=m_pHead;
Node *q=GetIp(p);
if(q==NULL)
return;
else
{
if(s==q) //删除的是头
DeleteHead();
else
{
while(s->next!=q)
s=s->next;
s->next=q->next;
delete q;
q=NULL;
}
}
--len;
}
Node *GetIp(int i)
{
Node *q=m_pHead;
if(i<=0)
{
cout<<"输入位置不合法!"<<endl;
return NULL;
}
else if(i>Length())
{
cout<<"输入位置大于链表长度!"<<endl;
return NULL;
}
else
{
int n=1;
while( n!=i)
{
q=q->next;
n++;
}
return q;
}
}
Node *GetHead() //获得头指针
{
if(m_pHead==NULL)
{
cout<<"链表为空,无法获得头地址!"<<endl;
return NULL;
}
else
return m_pHead;
}
void Clear() //清空链表
{
Node *p=m_pHead;
while(m_pHead!=NULL)
{
m_pHead=p->next;
delete p;
p=m_pHead;
}
len=0;
}
int Length() //求长度
{
return len;
}
void Sort() //从小到大排序
{
Node *q;
int n;
if(m_pHead==NULL || m_pHead->next==NULL)
return;
else
{
for(int i=0;i<Length()-1;++i)
{
for(int j=0;j<Length()-1-i;++j)
{
Node *p=m_pHead;
while(p->next!=NULL)
{
if(p->value>p->next->value)
{
n=p->value;
p->value=p->next->value;
p->next->value=n;
}
p=p->next;
}
}
}
}
}
void show()
{
if (m_pHead==NULL)
{
cout<<"链表为空!"<<endl;
return;
}
else
{
Node *p=m_pHead;
while(p!=NULL)
{
cout<<p->value<<"-->";
p=p->next;
}
cout<<"end"<<endl;
}
}
private:
Node *m_pHead;
Node *m_pTail;
};
int List::len=0;
void main()
{
List li;
li.InsertHead(9);
li.InsertHead(0);
li.InsertHead(3);
li.InsertHead(8);
li.InsertHead(2);
li.InsertHead(4);
li.InsertHead(5);
li.InsertTail(7);
li.InsertPosValue(1,6);
li.InsertPosValue(34,99);
li.InsertPosValue(9,34);
li.show();
li.DeleteHead();
li.DeleteTail();
li.show();
li.DeletePosValue(1);
cout<<li.GetHead()<<endl;
cout<<li.Length()<<endl;
li.Sort();
li.show();
li.Clear();
cout<<li.Length()<<endl;
li.show();
}
运行结果
阅读全文
0 0
- C++类中单链表的实现(头插、尾插、头删、尾删、指定位置插入、指定位置删除、链表长度、清空链表、链表排序)
- C语言:单链表的实现(包括:尾插、尾删、头插、头删、指定位置插入、长度、打印)
- 【C语言】静态顺序表的实现(包括头插、头删、尾插、尾删、查找、删除指定位置)
- 单链表的基本操作(尾插,尾删,头插,头删,查找,指定位置插入、删除,指定元素删除、全部删除等)
- 数据结构中静态顺序表的初始化、尾插、尾删、头插、头删、任意位置的插入及删除
- C语言实现链表的头插,尾插,插入,修改,删除和遍历
- 数组的链表实现:创建(头插/尾插)、打印、删除、插入
- 数据结构 单链表 头插 尾插 指定位置前插后插 以及 删除指定元素 C++ 面向对象方法实现
- 链表的创建(头插,尾插),插入,删除
- 链表的插入(头插,尾插,中间插)
- 双向循环链表的头插、中插、尾插、删除、逆序顺序显示(C++实现)
- 单向循环链表的头插,尾插,中间插以及删除节点
- 单链表的头插、中插、尾插、删除、逆序、显示(C语言实现)
- 单向循环链表,头插,尾插,中间插入,删除等功能
- 线性表 之 链表的头插,尾插建立,及链表的长度
- 实现链表逆序(头插)
- 实现链表逆序(头插)
- c++链表的初始化,头插,尾插,遍历
- 数据结构之图(2)
- Hadoop-No.3之序列化存储格式
- 联想笔记本安装ubuntu,无线网卡被禁用的问题及解决方法
- 亲测解决Vmware12中Ubuntu14.04占用空间大的问题
- goroutine背后的系统知识
- C++类中单链表的实现(头插、尾插、头删、尾删、指定位置插入、指定位置删除、链表长度、清空链表、链表排序)
- CSDN无法写博客的问题
- HDU6023Automatic Judge(水)
- HDU
- AC自动机以及KMP模板
- JAVA中的注释
- Python面试题
- 群居影响力
- LVS-NAT