#include<iostream>
using namespace std;
//有附加头节点的循环链表
template <class T>
struct CircLinkNode {
T data;
CircLinkNode<T>* link;
CircLinkNode(CircLinkNode* next = NULL) { data = 0; link = next; }
CircLinkNode(T d, CircLinkNode* next = NULL) { data = d, link = next; }
};
template<class T>
class CircList {
private:
CircLinkNode<T>* first, * last;
public:
CircList(const T& x);
CircList(CircList<T>& L);
~CircList();
int Length()const;
bool IsEmpty() { return first->link == first ? true : false; }
CircLinkNode<T>* getHead()const { return first; }
CircLinkNode<T>* Search(T x);
void setHead(CircLinkNode<T>* p) { p->link = first->link; first = p; last->link = first; }
CircLinkNode<T>* Locate(int i);
T getData(int i);
bool Insert(int i, T x);
bool Remove(int i, T& x);
void output();
};
template<class T>
CircList<T>::CircList(const T& x) {
first = new CircLinkNode<T>;
CircLinkNode<T>* newNode = new CircLinkNode<T>(x);
first->link = newNode;
last = newNode;
newNode->link = first;
}
template<class T>
CircList<T>::CircList(CircList<T>& L) {
if (L.getHead()->link == L.getHead())return;
CircLinkNode<T>* scrptr = L.getHead()->link;
CircLinkNode<T>* desptr = first=new CircLinkNode<T>;
while (scrptr != L.getHead()) {
desptr->link =new CircLinkNode<T>(scrptr->data);
desptr = desptr->link;
scrptr = scrptr->link;
}
last = desptr;
desptr->link = first;
}
template<class T>
CircList<T>::~CircList() {
while (first->link != first) {
CircLinkNode<T>* del = first->link;
first->link = first->link->link;
delete del;
}
}
template<class T>
int CircList<T>::Length()const {
CircLinkNode<T>* currt = first->link; int len = 0;
while (currt!= first) {
len++;
currt = currt->link;
}
return len;
}
template<class T>
CircLinkNode<T>* CircList<T>::Search(T x) {
CircLinkNode<T>* currt = first->link;
while (currt != first) {
if (currt->data == x)return currt;
currt = currt->link;
}
return NULL;
}
template<class T>
CircLinkNode<T>* CircList<T>::Locate(int i) {
if (i > Length()) return NULL;
CircLinkNode<T>* currt = first->link; int k = 1;
while (k < i) { currt = currt->link; k++; }
return currt;
}
template<class T>
T CircList<T>::getData(int i) {
if (i > Length()) return NULL;
return Locate(i)->data;
}
template<class T>
bool CircList<T>::Insert(int i, T x) {
if (i < 0 || i > Length()) return false;
CircLinkNode<T>* currt = first; int k = 0;
while (k < i) { currt = currt->link; k++; }
CircLinkNode<T>* newNode = new CircLinkNode<T>(x);
newNode->link = currt->link;
currt->link = newNode;
return true;
}
template<class T>
bool CircList<T>::Remove(int i, T& x) {
if (i <= 0 || i > Length()) return false;
CircLinkNode<T>* ptr = first; int k = 1;
while (k < i) { ptr = ptr->link; k++; }
CircLinkNode<T>* del = ptr->link;
ptr->link = ptr->link->link;
x = del->data;
delete del;
return true;
}
template<class T>
void CircList<T>::output() {
CircLinkNode<T>* currt = first->link;
while (currt != first) {
cout << currt->data << " ";
currt = currt->link;
}
cout << endl;
}
//测试函数
//void test01() {
// CircList<int> a(1); int x;
// CircList<int> b(a);
// if (a.Insert(1, 3))cout << "插入成功"<<" "; else cout << "插入失败"<<endl;
// if (a.Insert(1, 5))cout << "插入成功" << " "; else cout << "插入失败" << endl;
// if (a.Insert(2, 7))cout << "插入成功" << endl; else cout << "插入失败" << endl;
// if (b.Remove(1, x))cout << "已成功删除:" << x << endl;
// a.output();
// cout << "第三个元素为:" << a.getData(3) << endl;
// cout << "链表长度为:" << a.Length() << endl;
//}
//有附加头节点的双向链表
template<class T>
struct DblNode {
T data;
DblNode<T>* prev, * next;
DblNode(DblNode<T>* p = NULL, DblNode* n = NULL) { data = 0; prev = p; next = n; }
DblNode(T d,DblNode<T>* p = NULL, DblNode* n = NULL) { data = d; prev = p; next = n; }
};
template<class T>
class DblList {
private:DblNode<T>* first;
public:
DblList(T uniqueVal);
~DblList();
int Length()const;
bool IsEmpty() { return first->next == NULL ? true : false; }
DblNode<T>* getHead()const { return first; }
void setHead(DblNode<T>* ptr) { first = ptr; }
DblNode<T>* Locate(int i, int d);
bool Insert(int i,const T& x, int d);
bool Remove(int i, T& x, int d);
void output();
};
template<class T>
DblList<T>::DblList(T uniqueVal) {
first = new DblNode<T>(uniqueVal);
first->prev = first->next = first;
}
template<class T>
int DblList<T>::Length()const {
DblNode<T>* currt = first->next; int num = 0;
while (currt != first) {
num++;
currt = currt->next;
}
return num;
}
template<class T>
DblList<T>::~DblList() {
while (first->next != first) {
DblNode<T>* del = first->next;
first->next = del->next;
delete del;
}
}
template<class T>
DblNode<T>* DblList<T>::Locate(int i, int d) {
if (i == 0 || first->next == first) return first;
DblNode<T>* currt; int k = 1;
if (d == 0) {
currt = first->next;
while (k < i && currt != first) {
currt = currt->next;
k++;
}
}
else {
currt = first->prev; k = 1;
while (k < i && currt != first) {
currt = currt->prev;
k++;
}
}
return currt == first ? NULL : currt;
}
template<class T>
bool DblList<T>::Insert(int i, const T& x, int d) {
if (i<0 || i>Length())return false;
DblNode<T>* currt = first; int k = 0;
while (k < i) {
k++;
if (d == 0)currt = currt->next;
else currt = currt->prev;
}
if (d == 0) {
DblNode<T>* newNode = new DblNode<T>(x);
newNode->next = currt->next;
currt->next = newNode;
newNode->next->prev = newNode;
newNode->prev = currt;
}
else {
DblNode<T>* newNode = new DblNode<T>(x);
newNode->prev = currt->prev;
currt->prev = newNode;
newNode->prev->next = newNode;
newNode->next = currt;
}
return true;
}
template<class T>
bool DblList<T>::Remove(int i, T& x, int d) {
DblNode<T>* currt = Locate(i, d);
if (currt == NULL)return false;
currt->prev->next = currt->next;
currt->next->prev = currt->prev;
x = currt->data;
delete currt;
return true;
}
template<class T>
void DblList<T>::output() {
DblNode<T>* currt = first->next;
while (currt != first) {
cout << currt->data << " ";
currt = currt->next;
}
cout << endl;
}
//void test02() {
// DblList<int> a(3);
// DblList<int> b(a); int x;
// if (b.Insert(0, 5, 0)&& b.Insert(1, 2, 0)&& b.Insert(1, 8, 1)&& b.Insert(0, 10, 1))
// cout << "插入成功" << endl; else cout << "插入失败" << endl;
// b.output();
// if (b.Remove(1, x, 0))cout << "已成功删除:" << x << endl; else cout << "删除失败" << endl;
// if (b.Remove(1, x, 1))cout << "已成功删除:" << x << endl; else cout << "删除失败" << endl;
// b.output();
//}
//带附加头节点的静态链表
const int maxSize = 50;
template<class T>
struct SLinkNode {
T data;
int link;
};
template<class T>
class StaticList {
public:
SLinkNode<T> elem[maxSize];
int avil; //当前可分配空间首地址
StaticList() { InitList(); }
void InitList(); //将链表空间初始化
int Length();
int Search(T x);
int Locate(int i);
bool Insert(int i, T x);
bool Append(T x);
bool Remove(int i);
bool IsEmpty() { return elem[0].link == -1 ? true : false; }
void SOrderOutput(); //存储顺序输出(地址 键值 下一个结点地址)
void LOrderOutput(); //逻辑顺序输出(地址 键值 下一个结点地址)
};
template<class T>
void StaticList<T>::InitList() {
elem[0].link = -1; avil = 1;
for (int i = 1; i < maxSize - 1; i++)
elem[i].link = i + 1;
elem[maxSize - 1].link = -1;
}
template<class T>
int StaticList<T>::Length() {
int currt = elem[0].link; int len = 0;
while (currt != -1) {
currt = elem[currt].link;
len++;
}
return len;
}
template<class T>
int StaticList<T>::Search(T x) {
int currt = elem[0].link;
while (currt != -1) {
if (elem[currt].data == x) return currt;
currt = elem[currt].link;
}
return -1;
}
template<class T>
int StaticList<T>::Locate(int i) {
if (i < 0 )return -1;
if (i == 0) return 0;
int currt = elem[0].link; int k = 1;
while (k < i && currt != -1) {
currt = elem[currt].link;
k++;
}
return currt;
}
template<class T>
bool StaticList<T>::Insert(int i, T x) {
int currt = Locate(i);
if (currt == -1)return false;
int newNode = avil; avil = elem[avil].link;
elem[newNode].data = x;
elem[newNode].link = elem[currt].link;
elem[currt].link = newNode;
return true;
}
template<class T>
bool StaticList<T>::Append(T x) {
if (avil == -1)return false;
int currt = 0;
while (elem[currt].link != -1) {
currt = elem[currt].link;
}
int p = avil; avil = elem[avil].link;
elem[currt].link = p;
elem[p].data = x;
elem[p].link = -1;
return true;
}
template<class T>
bool StaticList<T>::Remove(int i) {
int prev = Locate(i - 1);
if (prev == -1) return false;
int del = elem[prev].link;
elem[prev].link = elem[del].link;
elem[del].link = avil;
avil = del;
return true;
}
template<class T>
void StaticList<T>::SOrderOutput() {
cout << 0 << " " << elem[0].data << " " << elem[0].link << endl;
for (int i = 1; elem[i].data!=elem[0].data; i++) {
cout << i << " " << elem[i].data << " " << elem[i].link << endl;
}
}
template<class T>
void StaticList<T>::LOrderOutput() {
int currt = 0;
while (currt != -1) {
cout << currt << " " << elem[currt].data << " " << elem[currt].link << endl;
currt = elem[currt].link;
}
}
//void test03() {
// StaticList<int> a;
// if (a.Append(10) && a.Append(4))cout << "追加成功" << endl; else cout << "追加失败" << endl;
// if (a.Insert(0, 3) && a.Insert(2, 5)&&a.Insert(4,13))cout << "插入成功" << endl; else cout << "插入失败" << endl;
// a.SOrderOutput(); cout << endl;
// a.LOrderOutput();
// cout << "13是第" << a.Search(13) << "个结点" << endl;
// if (a.Remove(2) && a.Remove(1))cout << "删除成功" << endl; else cout << "删除失败" << endl;
// if (a.Append(86) && a.Append(68)&&a.Append(1))cout << "追加成功" << endl; else cout << "追加失败" << endl;
// a.SOrderOutput(); cout << endl;
// a.LOrderOutput();
//}
【c++数据结构】链表类实现(循环 双向 静态)
最新推荐文章于 2022-11-20 16:26:41 发布