C++類中單鏈表的實現(頭插、尾插、頭刪、尾刪、指定位置插入、指定位置刪除、連結串列長度、清空連結串列、連結串列排序)
阿新 • • 發佈:2019-01-06
#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();
}
執行結果