文件:LinkNode.h
#ifndef LINK_NODE_H_
#define LINK_NODE_H_
#include <iostream>
#include <string>
#include <strstream>
using namespace std;
template <class T>
struct LinkNode //链表结点类的定义
{
T data; //数据域
LinkNode<T> *link; //指针域——后继指针
//仅初始化指针成员的构造函数
LinkNode(LinkNode<T>* ptr = NULL){ link = ptr; }
//初始化数据与指针成员的构造函数
LinkNode(const T& value, LinkNode<T>* ptr = NULL){ data = value; link = ptr; }
};
#endif /* LINK_NODE_H_ */
文件:CircularLinkedList.h
#ifndef CIRCULAR_LINKED_LIST_H_
#define CIRCULAR_LINKED_LIST_H_
#include "LinkNode.h"
template <class T>
class CircularLinkedList//带附加头结点
{
public:
CircularLinkedList(); //构造函数
CircularLinkedList(const CircularLinkedList<T>& L); //拷贝构造函数
~CircularLinkedList(); //析构函数
public:
int Length()const; //计算链表的结点总数(除附加头结点)
LinkNode<T>* Search(const T& x)const; //搜索数据值为x的结点并返回
LinkNode<T>* Locate(int i)const; //获取第i个结点并返回
bool GetData(int i, T& x)const; //获取第i个结点的数据值保存至x,并返回获取成功与否
bool SetData(int i, const T& x); //修改第i个结点的数据值为x
bool Insert(int i, const T& x); //在第i个结点后插入数据值为x的新结点
bool Remove(int i, T& x); //删除第i个结点,并将被删结点的数据值保存至x
bool IsEmpty()const; //判断表是否为空
bool IsFull()const; //判断表是否为满
void Sort(); //表排序——冒泡排序
void InputFront(const T& endTag); //前插法建立链表
void InputRear(const T& endTag); //后插法建立链表
void Output()const; //输出所有结点的数据值
void Reverse(); //链表逆置
void MakeEmpty(); //清空链表(保留附加头结点)
CircularLinkedList<T>& operator=(const CircularLinkedList<T>& L); //链表间赋值操作——重载等号运算符
public:
LinkNode<T>* GetHead()const; //返回附加头结点的地址
T get_value(string& s_value); //返回输入的结点数据值
public:
static bool IsNumber(const string& s_num); //判断输入的字符串每个字符是否都是数值0~9
static T StrToTtype(const string& s_num); //类型转换——将string型转为模板类型T
private:
LinkNode<T> *first; //链表的头结点
};
//构造函数
template<class T>
CircularLinkedList<T>::CircularLinkedList()
: first(new LinkNode<T>)
{
first->link = first;
cout << "$ 执行构造函数" << endl;
}
//拷贝构造函数
template<class T>
CircularLinkedList<T>::CircularLinkedList(const CircularLinkedList<T>& L)
{
cout << "$ 执行拷贝构造函数" << endl;
LinkNode<T> *srcptr = L.GetHead();
LinkNode<T> *destptr = first = new LinkNode<T>;
while (L.GetHead() != srcptr->link)
{
srcptr = srcptr->link;
destptr->link = new LinkNode<T>(srcptr->data);
destptr = destptr->link;
}
destptr->link = first;
}
//析构函数
template<class T>
CircularLinkedList<T>::~CircularLinkedList()
{
cout << "$ 执行析构函数" << endl;
MakeEmpty();
}
//计算链表的结点总数(除附加头结点)
template<class T>
int CircularLinkedList<T>::Length()const
{
int count = 0;
LinkNode<T> *curNode = first->link;
while (first != curNode)
{
curNode = curNode->link;
count++;
}
return count;
}
//搜索数据值为x的结点并返回
template<class T>
LinkNode<T>* CircularLinkedList<T>::Search(const T& x)const
{
LinkNode<T> *curNode = first->link;
while ((first != curNode) && (x != curNode->data))
{
curNode = curNode->link;
}
if (first == curNode)
{
curNode = NULL;
}
return curNode;
}
//获取第i个结点并返回
template<class T>
LinkNode<T>* CircularLinkedList<T>::Locate(int i)const
{
if (i < 0)
{
return NULL;
}
if ((first == first->link) || (0 == i))
{
return first;
}
int location = 1;
LinkNode<T> *curNode = first->link;
while ((location < i) && (first != curNode))
{
curNode = curNode->link;
location++;
}
if (first == curNode)
{
curNode = NULL;
}
return curNode;
}
//获取第i个结点的数据值保存至x,并返回获取成功与否
template<class T>
bool CircularLinkedList<T>::GetData(int i, T& x)const
{
LinkNode<T> *curNode = Locate(i);
if ((NULL == curNode) || (first == curNode))
{
return false;
}
x = curNode->data;
return true;
}
//修改第i个结点的数据值为x
template<class T>
bool CircularLinkedList<T>::SetData(int i, const T& x)
{
LinkNode<T> *curNode = Locate(i);
if ((NULL == curNode) || (first == curNode))
{
return false;
}
curNode->data = x;
return true;
}
//在第i个结点后插入数据值为x的新结点
template<class T>
bool CircularLinkedList<T>::Insert(int i, const T& x)
{
LinkNode<T> *curNode = Locate(i);
if (NULL == curNode)
{
return false;
}
LinkNode<T> *newNode = new LinkNode<T>(x);
if (NULL == newNode)
{
cerr << "* 存储分配错误" << endl;
exit(1);
}
newNode->link = curNode->link;
curNode->link = newNode;
return true;
}
//删除第i个结点,并将被删结点的数据值保存至x
template<class T>
bool CircularLinkedList<T>::Remove(int i, T& x)
{
LinkNode<T> *preNode = Locate(i - 1);
if (NULL == preNode)
{
return false;
}
LinkNode<T> *delNode = preNode->link;
if ((NULL == delNode) || (first == delNode))
{
return false;
}
preNode->link = delNode->link;
x = delNode->data;
delete delNode;
return true;
}
//判断表是否为空
template<class T>
bool CircularLinkedList<T>::IsEmpty()const
{
return (first == first->link) ? true : false;
}
//判断表是否为满
template<class T>
bool CircularLinkedList<T>::IsFull()const
{
return false;
}
//表排序——冒泡排序
template<class T>
void CircularLinkedList<T>::Sort()
{
LinkNode<T> *curNode = first->link;
while (first != curNode)
{
LinkNode<T> *nextNode = curNode->link;
while (first != nextNode)
{
if (curNode->data < nextNode->data)
{
T temp = curNode->data;
curNode->data = nextNode->data;
nextNode->data = temp;
}
nextNode = nextNode->link;
}
curNode = curNode->link;
}
}
//前插法建立链表
//endTag是约定的输入序列结束的标志。如果输入序列是正整数,endTag可以是0或负数;
//如果输入序列是字符,endTag可以是字符集中不会出现的字符,如“\0”。
template<class T>
void CircularLinkedList<T>::InputFront(const T& endTag)
{
MakeEmpty();
LinkNode<T> *lastNode = NULL;
string s_value;
T value = get_value(s_value);
while (endTag != value)
{
LinkNode<T> *newNode = new LinkNode<T>(value); //创建新结点
if (NULL == newNode)
{
cerr << "* 存储分配错误" << endl;
exit(1);
}
if (first->link == first)
{
lastNode = newNode;
}
newNode->link = first->link; //新结点中指针域的指针指向链表的第一个结点
first->link = newNode; //附加头结点中指针域的指针指向新结点
value = get_value(s_value);
}
lastNode->link = first;
}
//后插法建立链表
//endTag是约定的输入序列结束的标志。如果输入序列是正整数,endTag可以是0或负数;
//如果输入序列是字符,endTag可以是字符集中不会出现的字符,如“\0”。
template<class T>
void CircularLinkedList<T>::InputRear(const T& endTag)
{
MakeEmpty();
LinkNode<T> *lastNode = first; //设置链表的最后一个结点为附加头结点
string s_value;
T value = get_value(s_value);
while (endTag != value)
{
LinkNode<T> *newNode = new LinkNode<T>(value); //创建新结点
if (NULL == newNode)
{
cerr << "* 存储分配错误" << endl;
exit(1);
}
lastNode->link = newNode; //最后一个结点中指针域的指针指向新结点
lastNode = newNode; //设置最后一个结点为新结点
value = get_value(s_value);
}
lastNode->link = first;
}
//输出所有结点的数据值
template<class T>
void CircularLinkedList<T>::Output()const
{
LinkNode<T> *curNode = first->link;
while (first != curNode)
{
cout << "curNode->data = " << curNode->data << endl;
curNode = curNode->link;
}
}
//链表逆置
template<class T>
void CircularLinkedList<T>::Reverse()
{
LinkNode<T> *prevNode = first;
LinkNode<T> *curNode = first->link;
LinkNode<T> *nextNode = curNode->link;
while (first != curNode)
{
curNode->link = prevNode;
prevNode = curNode;
curNode = nextNode;
nextNode = curNode->link;
}
first->link = prevNode;
}
//清空链表(保留附加头结点)
template<class T>
void CircularLinkedList<T>::MakeEmpty()
{
LinkNode<T> *curNode = NULL;
while (first != first->link) //当链表不为空时,删去链表中所有结点
{
curNode = first->link; //保存被删结点
first->link = curNode->link; //将链表附加头结点中指针域的指针指向被删结点的下一个结点
delete curNode; //从链表上摘下被删结点
}
}
//链表间赋值操作——重载等号运算符
template<class T>
CircularLinkedList<T>& CircularLinkedList<T>::operator=(const CircularLinkedList<T>& L)
{
cout << "$ 执行赋值操作函数" << endl;
LinkNode<T> *srcptr = L.GetHead();
LinkNode<T> *destptr = first = new LinkNode<T>;
while (L.GetHead() != srcptr->link)
{
srcptr = srcptr->link;
destptr->link = new LinkNode<T>(srcptr->data);
destptr = destptr->link;
}
destptr->link = first;
return *this;
}
//返回附加头结点的地址
template<class T>
LinkNode<T>* CircularLinkedList<T>::GetHead()const
{
return first;
}
//返回输入的结点数据值
template <class T>
T CircularLinkedList<T>::get_value(string& s_value)
{
cout << "newNode->data = ";
cin >> s_value;
return StrToTtype(s_value);
}
//判断输入的字符串每个字符是否都是数值0~9
template <class T>
bool CircularLinkedList<T>::IsNumber(const string& s_num)
{
for (size_t i = 0; i < s_num.size(); i++)
{
if ((s_num[i] < '0') || (s_num[i] > '9'))
{
return false;
}
}
return true;
}
//类型转换——将string型转为模板类型T
template <class T>
T CircularLinkedList<T>::StrToTtype(const string& s_num)
{
T n_num;
strstream ss_num;
ss_num << s_num;
ss_num >> n_num;
return n_num;
}
#endif /* CIRCULAR_LINKED_LIST_H_ */
文件:main.cpp
#include "CircularLinkedList.h" //带附加头结点
#define EXIT 0 //退出
#define CONSTRUCT_COPY 1 //拷贝构造链表
#define LENGTH 2 //计算链表的结点总数
#define SEARCH 3 //搜索数据值为x的结点并返回
#define LOCATE 4 //获取第i个结点并返回
#define GETDATA 5 //获取第i个结点的数据值保存至x,并返回获取成功与否
#define SETDATA 6 //修改第i个结点的数据值为x
#define INSERT 7 //在第i个结点后插入数据值为x的新结点
#define REMOVE 8 //删除第i个结点,并将被删结点的数据值保存至x
#define ISEMPTY 9 //判断表是否为空
#define ISFULL 10 //判断表是否为满
#define SORT 11 //表排序——冒泡排序
#define INPUTFRONT 12 //前插法建立链表
#define INPUTREAR 13 //后插法建立链表
#define OUTPUT 14 //输出所有结点的数据值
#define REVERSE 15 //链表逆置
#define MAKEEMPTY 16 //清空链表
#define OPERATOR_COPY 17 //链表间赋值操作——重载等号运算符
#define GETHEAD 18 //返回头结点的地址
void print_description()
{
cout << "------------------------------>循环链表<------------------------------" << endl;
cout << "功能选项说明:" << endl;
cout << "#0: 退出" << endl;
cout << "#1: 拷贝构造链表" << endl;
cout << "#2: 计算链表的结点总数" << endl;
cout << "#3: 搜索数据值为x的结点并返回" << endl;
cout << "#4: 获取第i个结点并返回" << endl;
cout << "#5: 获取第i个结点的数据值保存至x,并返回获取成功与否" << endl;
cout << "#6: 修改第i个结点的数据值为x" << endl;
cout << "#7: 在第i个结点后插入数据值为x的新结点" << endl;
cout << "#8: 删除第i个结点,并将被删结点的数据值保存至x" << endl;
cout << "#9: 判断表是否为空" << endl;
cout << "#10:判断表是否为满" << endl;
cout << "#11:表排序——冒泡排序" << endl;
cout << "#12:前插法建立链表" << endl;
cout << "#13:后插法建立链表" << endl;
cout << "#14:输出所有结点的数据值" << endl;
cout << "#15:链表逆置" << endl;
cout << "#16:清空链表" << endl;
cout << "#17:链表间赋值操作——重载等号运算符" << endl;
cout << "#18:返回头结点的地址" << endl;
cout << "--------------------------------------------------------------------" << endl;
}
//输入结点编号
template <class T>
int get_item()
{
cout << "> 请输入结点编号,item = ";
string s_item;
cin >> s_item;
while (false == CircularLinkedList<T>::IsNumber(s_item))
{
cout << "* 输入有误,请重新输入:";
cin >> s_item;
}
return atoi(s_item.c_str());
}
//输入数据值
template <class T>
T get_data()
{
cout << "> 请输入数据值,data = ";
string s_data;
cin >> s_data;
return CircularLinkedList<T>::StrToTtype(s_data);
}
//输入结束标志位
template <class T>
T get_endtag()
{
cout << "请输入结束标志值,endTag = ";
string s_endTag;
cin >> s_endTag;
return CircularLinkedList<T>::StrToTtype(s_endTag);
}
//构造循环链表
template <class T>
CircularLinkedList<T>* construct_linkedlist()
{
cout << "\n==> 创建循环链表" << endl;
CircularLinkedList<T> *linkedList = new CircularLinkedList<T>;
return linkedList;
}
//析构循环链表
template <class T>
void destory_linkedlist(CircularLinkedList<T>* linkedList)
{
cout << "\n==> 释放循环链表在堆中申请的空间,并将指向该空间的指针变量置为空" << endl;
delete linkedList;
linkedList = NULL;
}
//拷贝构造链表
template <class T>
void construct_copy(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行拷贝构造链表函数" << endl;
CircularLinkedList<T> cpy_linkedlist = *linkedList;
cpy_linkedlist.Output();
}
//计算链表的结点总数
template <class T>
void length(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行计算链表的结点总数函数,Length = " << linkedList->Length() << endl;
}
//搜索数据值为x的结点并返回
template <class T>
void search(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行搜索数据值为x的结点并返回函数" << endl;
T data = get_data<T>();
LinkNode<T> *node = linkedList->Search(data);
if (NULL == node)
{
cout << "* 搜索失败" << endl;
return;
}
cout << "* 搜索成功,data = " << node->data << endl;
}
//获取第i个结点并返回
template <class T>
void locate(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行获取第i个结点并返回函数" << endl;
int n_item = get_item<T>();
LinkNode<T> *node = linkedList->Locate(n_item);
if ((NULL == node) || (linkedList->GetHead() == node))
{
cout << "* 获取失败" << endl;
return;
}
cout << "* 获取成功,item = " << n_item << ",data = " << node->data << endl;
}
//获取第i个结点的数据值保存至x,并返回获取成功与否
template <class T>
void getdata(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行获取第i个结点的数据值保存至x并返回获取成功与否函数" << endl;
T data;
int n_item = get_item<T>();
if (false == linkedList->GetData(n_item, data))
{
cout << "* 获取失败" << endl;
return;
}
cout << "* 获取成功,item = " << n_item << ",data = " << data << endl;
}
//修改第i个结点的数据值为x
template <class T>
void setdata(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行修改第i个结点的数据值为x函数" << endl;
int n_item = get_item<T>();
T data = get_data<T>();
if (false == linkedList->SetData(n_item, data))
{
cout << "* 修改失败" << endl;
return;
}
cout << "* 修改成功,item = " << n_item << ",data = " << data << endl;
}
//在第i个结点后插入数据值为x的新结点
template <class T>
void insert(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行在第i个结点后插入数据值为x的新结点函数" << endl;
int n_item = get_item<T>();
T data = get_data<T>();
if (false == linkedList->Insert(n_item, data))
{
cout << "* 插入失败" << endl;
return;
}
cout << "* 插入成功,item+1 = " << n_item+1 << ",data = " << data << endl;
}
//删除第i个结点,并将被删结点的数据值保存至x
template <class T>
void remove(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行删除第i个结点并将被删结点的数据值保存至x函数" << endl;
T data;
int n_item = get_item<T>();
if (false == linkedList->Remove(n_item, data))
{
cout << "* 删除失败" << endl;
return;
}
cout << "* 删除成功,item = " << n_item << ",data = " << data << endl;
}
//判断表是否为空
template <class T>
void isempty(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行判断表是否为空函数,IsEmpty = " << linkedList->IsEmpty() << endl;
}
//判断表是否为满
template <class T>
void isfull(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行判断表是否为满函数,IsFull = " << linkedList->IsFull() << endl;
}
//表排序——冒泡排序
template <class T>
void sort(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行表排序——冒泡排序函数" << endl;
linkedList->Sort();
}
//前插法建立链表
template <class T>
void inputfront(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行前插法建立链表函数" << endl;
T endTag = get_endtag<T>();
linkedList->InputFront(endTag);
}
//后插法建立链表
template <class T>
void inputrear(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行后插法建立链表函数" << endl;
T endTag = get_endtag<T>();
linkedList->InputRear(endTag);
}
//输出所有结点的数据值
template <class T>
void output(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行输出所有结点的数据值函数" << endl;
linkedList->Output();
}
//链表逆置
template <class T>
void reverse(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行链表逆置函数" << endl;
linkedList->Reverse();
}
//清空链表
template <class T>
void make_empty(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行清空链表函数" << endl;
linkedList->MakeEmpty();
}
//链表间赋值操作——重载等号运算符
template <class T>
void operator_copy(CircularLinkedList<T>* linkedList)
{
cout << "$ 执行链表间赋值操作——重载等号运算符函数" << endl;
CircularLinkedList<T> cpy_linkedlist;
cpy_linkedlist = *linkedList;//或cpy_linkedlist.operator=(*linkedList);
cpy_linkedlist.Output();
}
//返回头结点的地址
template <class T>
void gethead(CircularLinkedList<T>* linkedList)
{
LinkNode<T> *head = linkedList->GetHead();
cout << "$ 执行返回头结点的地址函数,&first = " << &head << endl;
}
//单链表操作选择
template <class T>
void select_operation(CircularLinkedList<T>* linkedList)
{
if (NULL == linkedList)
{
cout << "* 没有构造循环链表,请先构造循环链表。" << endl;
return;
}
string s_operation;
while (s_operation != "0")
{
cout << "\n==> 请输入功能选项编号(按\"0\"退出程序):";
cin >> s_operation;
while (false == CircularLinkedList<T>::IsNumber(s_operation))
{
cout << "* 输入有误,请重新输入:";
cin >> s_operation;
}
int n_operation = atoi(s_operation.c_str());
switch (n_operation)
{
case EXIT://退出
{
cout << "$ 退出程序" << endl;
break;
}
case CONSTRUCT_COPY://拷贝构造链表
{
construct_copy(linkedList);
break;
}
case LENGTH://计算链表的结点总数
{
length(linkedList);
break;
}
case SEARCH://搜索数据值为x的结点并返回
{
search(linkedList);
break;
}
case LOCATE://获取第i个结点并返回
{
locate(linkedList);
break;
}
case GETDATA://获取第i个结点的数据值保存至x,并返回获取成功与否
{
getdata(linkedList);
break;
}
case SETDATA://修改第i个结点的数据值为x
{
setdata(linkedList);
break;
}
case INSERT://在第i个结点后插入数据值为x的新结点
{
insert(linkedList);
break;
}
case REMOVE://删除第i个结点,并将被删结点的数据值保存至x
{
remove(linkedList);
break;
}
case ISEMPTY://判断表是否为空
{
isempty(linkedList);
break;
}
case ISFULL://判断表是否为满
{
isfull(linkedList);
break;
}
case SORT://表排序——冒泡排序
{
sort(linkedList);
break;
}
case INPUTFRONT://前插法建立链表
{
inputfront(linkedList);
break;
}
case INPUTREAR://后插法建立链表
{
inputrear(linkedList);
break;
}
case OUTPUT://输出所有结点的数据值
{
output(linkedList);
break;
}
case REVERSE://链表逆置
{
reverse(linkedList);
break;
}
case MAKEEMPTY://清空链表
{
make_empty(linkedList);
break;
}
case OPERATOR_COPY://链表间赋值操作——重载等号运算符
{
operator_copy(linkedList);
break;
}
case GETHEAD://返回头结点的地址
{
gethead(linkedList);
break;
}
default:
{
cout << "* 请输入正确的功能选项编号" << endl;
break;
}
}
}
}
int main(int argc, char* argv[])
{
print_description();
CircularLinkedList<int> *linkedList = construct_linkedlist<int>();
select_operation(linkedList);
destory_linkedlist(linkedList);
system("pause");
return 0;
}
参考文献:
[1]《数据结构(用面向对象方法与C++语言描述)(第2版)》殷人昆——第二章
[2]《C/C++常用算法手册》秦姣华、向旭宇——第二章
[3] 百度搜索关键字:循环链表的优缺点