所實現的循環雙鏈表的結構如下圖所示:
循環雙鏈表的實現,和第一篇文章單鏈表的實現思想大致相同點擊打開鏈接,具體思想參考該文章。本篇文章在構建節點的同時,初始化構建節點的前驅和後繼,具體細節參考下列代碼
頭文件:DCList.h
#ifndef DCLIST_H
#define DCLIST_H
#include<iostream>
#include<cstdlib>
#include<cassert>
using namespace std;
typedef enum{FALSE,TRUE}Status;
template<class Type>
class List;
template<class Type>
class ListNode
{
template<class Type>
friend class List;
private:
Type data;
ListNode<Type> *prio;
ListNode<Type> *next;
public:
ListNode() :data(Type()),prio(NULL), next(NULL){}
ListNode(const Type x, ListNode<Type> *p = NULL, ListNode<Type> *n = NULL)
:data(x), prio(p), next(n){}
Type getData()const { return data; }
};
template<class Type>
class List
{
private:
ListNode<Type> *first;
ListNode<Type> *last;
size_t size;
public:
ListNode<Type>* _BuyNode(const Type x, ListNode<Type> *p = NULL, ListNode<Type> *n = NULL)
{
ListNode<Type> *s = new ListNode<Type>(x, p, n);
assert(s != NULL);
return s;
}
List()
{
ListNode<Type> *s = _BuyNode(0, NULL, NULL);
first = last = s;
last->next = first;
size = 0;
}
~List()
{
destory();
}
Status push_back(const Type &x)
{
ListNode<Type> *s = _BuyNode(x, last, first);
last->next = s;
last = s;
size++;
return TRUE;
}
void show_list()
{
ListNode<Type> *s = first->next;
while (s != first)
{
cout << s->data << "->";
s = s->next;
}
cout << "Nul." << endl;
}
Status push_front(const Type &x)
{
ListNode<Type> *s = _BuyNode(x, first, first->next);
if (size == 0)
{
first->next = s;
last = s;
last->next = first;
}
else
{
first->next->prio = s;
first->next = s;
}
size++;
return TRUE;
}
Status pop_back()
{
if (size == 0)
{
cout << "循環雙鏈表已空,無法尾刪" << endl;
return FALSE;
}
ListNode<Type> *s = last->prio;
delete last;
last = s;
last->next = first;
size--;
return TRUE;
}
Status pop_front()
{
if (size == 0)
{
cout << "循環雙鏈表已空,無法頭刪" << endl;
return FALSE;
}
ListNode<Type> *s = first->next;
if (size == 1)
{
last = first;
last->next = first;
}
else
{
first->next = s->next;
s->next->prio = first;
}
delete s;
size--;
return TRUE;
}
Status insert_val(const Type &x)
{
ListNode<Type> *s = first;
while (s->next != first && s->next->data < x)
{
s = s->next;
}
if (s->next == first)//直接尾插即可
{
ListNode<Type> *p = _BuyNode(x, last, first);
last->next = p;
last = p;
}
else//找到了前驅,在其之後插入即可
{
ListNode<Type> *p = _BuyNode(x, s, s->next);
s->next->prio = p;
s->next = p;
}
size++;
return TRUE;
}
size_t lenth()
{
return size;
}
ListNode<Type>* find(const Type &x)
{
ListNode<Type> *s = first->next;
while (s != first && s->data != x)
{
s = s->next;
}
if (s == first)
return NULL;
else
return s;
}
Status delete_val(const Type &x)
{
ListNode<Type> *s = find(x);
if (s == NULL)
{
cout << "該元素不存在,無法刪除" << endl;
return FALSE;
}
if (s == last)
{
ListNode<Type> *p = last->prio;
delete last;
last = p;
last->next = first;
}
else
{
ListNode<Type> *q = s->next;
s->data = q->data;
s->next = q->next;
q->next->prio = s;
delete q;
}
size--;
return TRUE;
}
void sort()
{
if (size == 0 || size == 1)
return;
ListNode<Type> *s = first->next;
ListNode<Type> *p = s->next;
last = s;
last->next = first;
while (p != first)
{
s = p;
p = p->next;
ListNode<Type> *q = first;
while (q->next != first && q->next->data < s->data)
{
q = q->next;
}
if (q->next == first)
{
last->next = s;
s->prio = last;
last = s;
last->next = first;
}
else
{
s->next = q->next;
q->next->prio = s;
q->next = s;
s->prio = q;
}
}
}
void reverse()
{
if (size == 0 || size == 1)
return;
ListNode<Type> *s = first->next;
ListNode<Type> *p = s->next;
last = s;
last->next = first;
while (p != first)
{
s = p;
p = p->next;
s->next = first->next;
first->next->prio = s;
first->next = s;
s->prio = first;
}
}
void clear()
{
if (size == 0)
return;
ListNode<Type> *s = first->next;
while (s != first)
{
if (size == 1)
{
last = first;
last->next = first;
}
else
{
first->next = s->next;
s->next->prio = first;
}
delete s;
size--;
s = first->next;//爲下一次刪除做準備
}
}
void destory()
{
clear();
delete[] first;
first = last = NULL;
}
ListNode<Type>* next(ListNode<Type> *s)
{
if (s == last)
return NULL;
else
return s->next;
}
ListNode<Type>* prio(ListNode<Type> *s)
{
if (s == first->next) //-------------->第一個節點沒有前驅,返回NULL
return NULL;
else
return s->prio;
}
};
#endif
測試文件main.cpp
#include"DCList.h"
int main()
{
List<int> mylist;
int item;
int n;
int select = 1;
//ListNode<int> *p;
while (select)
{
cout << "*************************************** *" << endl;
cout << "*[1] push_back [2] push_front *" << endl;
cout << "*[3] show_list [4] pop_back *" << endl;
cout << "*[5] pop_front [6] insert_val *" << endl;
cout << "*[7] lenth [8] find *" << endl;
cout << "*[9] merge [10] delete_val*" << endl;
cout << "*[11] sort [12] reverse *" << endl;
cout << "*[13] next [14] clear *" << endl;
cout << "*[15] prio [0] quit_system*" << endl;
cout << "請選擇:>";
cin >> select;
switch (select)
{
case 1:
cout << "請輸入要插入的元素(-1結束):>";
while (cin >> item, item != -1)
{
mylist.push_back(item);
}
break;
case 2:
cout << "請輸入要插入的元素(-1結束):>";
while (cin >> item, item != -1)
{
mylist.push_front(item);
}
break;
case 3:
mylist.show_list();
break;
case 4:
mylist.pop_back();
break;
case 5:
mylist.pop_front();
break;
case 6:
cout << "請輸入要插入的元素:";
cin >> item;
mylist.insert_val(item);
break;
case 7:
cout << "長度爲:" << mylist.lenth() << endl;
break;
case 8:
cout << "請輸入要查找的元素:";
cin >> item;
if (mylist.find(item))
cout << "it's found" << endl;
else
cout << "it's not exist" << endl;
break;
case 9:
cout << "請輸入要刪除的位置:";
cin >> n;
//mylist.delete_pos(n,item);
break;
case 10:
cout << "請輸入要刪除的元素:";
cin >> item;
mylist.delete_val(item);
break;
case 11:
mylist.sort();
break;
case 12:
mylist.reverse();
break;
case 14:
mylist.clear();
break;
default:
break;
}
}
system("pause");
return 0;
}