【資料結構】實現迴圈連結串列(c++)
標頭檔案:
#pragma once
#include <iostream>
using namespace std;
template<class Type>
class List;
// 結點類
template<class Type>
class NodeList
{
friend class List<Type>;
public:
NodeList();
NodeList(Type d, NodeList<Type> *n = NULL);
private:
Type data;
NodeList<Type>* next;
};
// 連結串列類
template<class Type>
class List
{
public:
List();
~List();
public:
void show_list();
void tail_insert(Type const &x);
void head_insert(Type const &x);
void sort();
void head_delete();
void tail_delete();
void val_insert(Type const &x);
NodeList<Type>* find(Type const &x);
void val_delete(Type const &x);
Type length();
void reverse();
void clear();
void destroy();
void quit_system(Type &x);
protected:
NodeList<Type>* BuyNode(Type x = Type())
{
NodeList<Type>* p = new NodeList<Type>(x);
return p;
}
private:
NodeList<Type>* first;
NodeList<Type>* last;
};
// 結點類的建構函式
template<class Type>
NodeList<Type>::NodeList()
{
data = Type();
next = NULL;
}
template<class Type>
NodeList<Type>::NodeList(Type d, NodeList<Type> *n = NULL)
{
data = d;
next = n;
}
// 連結串列類的建構函式
template<class Type>
List<Type>::List()
{
first = last = BuyNode();
last->next = first;
}
// 連結串列類的解構函式
template<class Type>
List<Type>::~List()
{
destroy();
}
// 顯示
template<class Type>
void List<Type>::show_list()
{
NodeList<Type> *p = first->next;
while (p != first)
{
cout << p->data << "->";
p = p->next;
}
cout << "NULL" << endl;
}
// 尾插
template<class Type>
void List<Type>::tail_insert(Type const& x)
{
NodeList<Type> *p = BuyNode(x);
last->next = p;
p->next = first;
last = p;
first->data++;
}
// 頭插
template<class Type>
void List<Type>::head_insert(Type const &x)
{
NodeList<Type> *p = BuyNode(x);
p->next = first->next;
first->next = p;
last = p;
for (int i = 0; i < first->data; ++i)
{
last = last->next;
}
first->data++;
}
// 排序
template<class Type>
void List<Type>::sort()
{
if (first->data == 0)
{
return;
}
if (first->data == 1)
{
show_list();
}
NodeList<Type> *t = first->next;
NodeList<Type> *p = t->next;
NodeList<Type> *q = NULL;
t->next = first;
last = t;
first->data = 1;
while (p != first)
{
val_insert(p->data);
q = p;
p = p->next;
delete q;
}
}
// 頭刪
template<class Type>
void List<Type>::head_delete()
{
if (first->data == 0)
{
cout << "the list is empty,cannot delete!" << endl;
return;
}
NodeList<Type> *p = first->next;
first->next = p->next;
if (first->data == 1)
last = first;
delete p;
first->data--;
}
// 尾刪
template<class Type>
void List<Type>::tail_delete()
{
if (first->data == 0)
{
cout << "the list is empty,cannot delete!" << endl;
return;
}
NodeList<Type> *s = first;
NodeList<Type> *p = s->next;
while (p->next != first)
{
s = s->next;
p = p->next;
}
if (p->next == first)
{
s->next = first;
last = s;
delete p;
first->data--;
}
}
// 按值插入
template<class Type>
void List<Type>::val_insert(Type const &x)
{
if (first->data == 0)
{
tail_insert(x);
return;
}
NodeList<Type> *p = first;
NodeList<Type> *s = BuyNode(x);
while (p->next->data < x && p->next->next != first)
{
p = p->next;
}
if (p->next->data>x)
{
s->next = p->next;
p->next = s;
first->data++;
}
else
{
tail_insert(x);
}
}
// 查詢
template<class Type>
NodeList<Type>* List<Type>::find(Type const &x)
{
NodeList<Type> *s = first->next;
while (s != first)
{
if (s->data == x)
{
return s;
}
s = s->next;
}
return NULL;
}
// 按值刪除
template<class Type>
void List<Type>::val_delete(Type const &x)
{
if (first->data == 0)
{
cout << "the list is empty,cannot delete!" << endl;
return;
}
NodeList<Type> *s = find(x);
if (s == NULL)
{
cout << "the number is not exist,can not delete!" << endl;
return;
}
NodeList<Type> *p = first;
while (p->next != s)
{
p = p->next;
}
if (s->next != first)
{
p->next = s->next;
first->data--;
delete s;
}
else
{
tail_delete();
}
}
// 求長度
template<class Type>
Type List<Type>::length()
{
return first->data;
}
// 反轉
template<class Type>
void List<Type>::reverse()
{
if (first->data == 0)
{
return;
}
if (first->data == 1)
{
show_list();
}
NodeList<Type> *t = first->next;
NodeList<Type> *p = t->next;
NodeList<Type> *q = NULL;
t->next = first;
last = t;
first->data = 1;
while (p != first)
{
head_insert(p->data);
q = p;
p = p->next;
delete q;
}
}
// 清空
template<class Type>
void List<Type>::clear()
{
if (first->data == 0)
{
return;
}
while (first->next != first)
{
tail_delete();
}
}
// 摧毀
template<class Type>
void List<Type>::destroy()
{
clear();
delete first;
}
// 退出系統
template<class Type>
void List<Type>::quit_system(Type &x)
{
x = 0;
}
// 用c++實現單連結串列
#include "CList.h"
int main()
{
List<int> mylist;
int input = 1;
int insert;
while (input)
{
cout << "*********************************************************************" << endl;
cout << "* [1] show_list [2] tail_insert *" << endl;
cout << "* [3] head_insert [4] sort *" << endl;
cout << "* [5] head_delete [6] tail_delete *" << endl;
cout << "* [7] val_insert [8] find *" << endl;
cout << "* [9] val_delete [10] length *" << endl;
cout << "* [11] reverse [12] clear *" << endl;
cout << "* [13] destroy [14] quit_system *" << endl;
cout << "*********************************************************************" << endl;
cout << "please choose:";
cin >> input;
switch (input)
{
case 1:
mylist.show_list();
break;
case 2:
cout << "please enter the number want to insert(-1 end):";
while (cin >> insert, insert != -1)
{
mylist.tail_insert(insert);
}
break;
case 3:
cout << "please enter the number want to insert(-1 end):";
while (cin >> insert, insert != -1)
{
mylist.head_insert(insert);
}
break;
case 4:
mylist.sort();
break;
case 5:
mylist.head_delete();
break;
case 6:
mylist.tail_delete();
break;
case 7:
cout << "please enter the number want to insert:";
cin >> insert;
mylist.val_insert(insert);
break;
case 8:
cout << "please enter the number want to find:";
cin >> insert;
if (mylist.find(insert) == NULL)
{
cout << "the number is not exist!" << endl;
}
else
{
cout << "the number at the " << mylist.find(insert) << endl;
}
break;
case 9:
cout << "please enter the number want to delete:";
cin >> insert;
mylist.val_delete(insert);
break;
case 10:
cout << "the length is" << " " << mylist.length() << endl;
break;
case 11:
mylist.reverse();
break;
case 12:
mylist.clear();
break;
case 13:
mylist.destroy();
break;
case 14:
mylist.quit_system(input);
break;
default:
break;
}
}
return 0;
}
清空:
查詢:
頭刪:
頭插:
退出系統:
反轉:
排序:
尾刪:
尾插:
按值刪除:
按值插入:
相關文章
- 【資料結構】雙迴圈連結串列(c++)資料結構C++
- 資料結構之迴圈連結串列資料結構
- 資料結構學習(C++)——迴圈連結串列 (轉)資料結構C++
- 【資料結構】遞迴實現連結串列逆序資料結構遞迴
- 資料結構 - 單連結串列 C++ 實現資料結構C++
- 【資料結構】實現單連結串列(c++)資料結構C++
- 連結串列-迴圈連結串列
- 資料結構與演算法--迴圈連結串列資料結構演算法
- 資料結構——單連結串列的C++實現資料結構C++
- 連結串列4: 迴圈連結串列
- 迴圈單連結串列建構函式、解構函式C++實現函式C++
- 實戰資料結構(5)_雙向迴圈連結串列的基本操作資料結構
- 單向迴圈連結串列的實現
- 【資料結構】迴圈佇列的實現(c++)資料結構佇列C++
- 資料結構初階--雙向迴圈連結串列(講解+類别範本實現)資料結構
- 資料結構:單迴圈連結串列的建立插入與刪除資料結構
- js實現資料結構--單連結串列JS資料結構
- 【資料結構】連結串列(單連結串列實現+詳解+原碼)資料結構
- 實戰資料結構(4)_迴圈單連結串列解決約瑟夫問題資料結構
- 【資料結構與演算法學習】線性表(順序表、單連結串列、雙向連結串列、迴圈連結串列)資料結構演算法
- php實現基本資料結構之連結串列PHP資料結構
- Python資料結構——連結串列的實現Python資料結構
- 資料結構(雙向連結串列的實現)資料結構
- 資料結構_連結串列_單向迴圈連結串列 & 雙向連結串列的初始化、插入、刪除、修改、查詢列印(基於C語言實現)資料結構C語言
- 6-C/C++實現資料結構連結串列相關操作C++資料結構
- 單向迴圈連結串列
- JS資料結構第三篇---雙向連結串列和迴圈連結串列之約瑟夫問題JS資料結構
- C語言資料結構:單向迴圈連結串列的增刪操作C語言資料結構
- C語言資料結構:雙向迴圈連結串列的增刪操作C語言資料結構
- C++資料結構連結串列的基本操作C++資料結構
- 資料結構_連結串列_單向迴圈連結串列的初始化、插入、刪除、修改、查詢列印(基於C語言實現)資料結構C語言
- 資料結構_連結串列_雙向迴圈連結串列的初始化、插入、刪除、修改、查詢列印(基於C語言實現)資料結構C語言
- 資料結構-連結串列資料結構
- 資料結構 - 連結串列資料結構
- 連結串列-資料結構資料結構
- 資料結構--連結串列資料結構
- 資料結構—連結串列資料結構
- 資料結構學習(C++)——單連結串列(定義與實現) (轉)資料結構C++