chain.cpp
//循环链表和头节点
//1 ) 把线性表描述成一个单向
//循环链表 ( singly linked circular list ) ( 简称循环链表 ),而不是单向链表 ;
//2 ) 在链表的前面增加一个节点,称为头节点(header node )。只要将单向链表的尾节点与头节点链接起来,单向
//链表就成为循环链表,
#include <iostream>
#include <sstream>
#include <iterator>
#include <cassert>
using namespace std;
//数据结构算法与应用-C++语言描述 chain 单链表 带头节点
//一个线性表的抽象类
template <class T>
class linearList
{
public:
virtual ~linearList(){}
//返回true,当且仅当线性表为空
virtual bool empty() const = 0;
//返回线性表的元素个数
virtual int size() const = 0;
//返回索引为 theIndex 的元素
virtual T& get(int theIndex) const = 0;
//返回元素 theElement 第一次出现时的索引
virtual int indexOf(const T& theElement) const = 0;
//删除索引为 theIndex 的元素
virtual void erase(int theIndex) = 0;
//把 theElement 插入线性表中索引为 theIndex 的位置上
virtual void insert(int theIndex, const T& theElement) = 0;
//把线性表插入输出流 out
virtual void output(ostream& out) const = 0;
};
/*
结构 chainNode
为了用链表摘述线性表,我们要定义一个结构 chainNode 和一个类 chain。
数据成员 element 是节点的数据域,存储表元素; 数据成员 next 是节点的链域,存储下一个节点的指针。
*/
//链表节点的结构定义
template <class T>
struct chainNode
{
public:
//方法
chainNode(){}
chainNode(const T& element)
{
this->element = element;
}
chainNode(const T& element,chainNode<T>* next)
{
this->element = element;
this->next = next;
}
//数据成员
T element;
chainNode<T> *next;
};
template<class T>
class circularList : public linearList<T>
{
public:
circularList(int initialCapacity = 10);
~circularList();
bool empty() const{ return listSize == 0;}
int size() const { return listSize; }
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void insert(int theIndex, const T& theElement);
void erase(int theIndex);
void output(ostream& out) const;
void clear();
void push_back(const T& theElement);
chainNode<T>* getNode(int theIndex) const;
//ex37.令x指向循环链表的任意一个节点。
//1 ) 编写一个方法,删除节点 x。提示 : 因为不知道节点 x 的前驱,所以删除节点 x 有困
//难。然而,可以用后继y 的数据域覆盖 x 的数据域,然后删除节点 y。当删除最后一
//个节点之后,首节点成为最后的节点。
//2 ) 计算方法的时间复杂度。
//3 ) 使用自己的测试数据检验方法的正确性。
void removeNode(chainNode<T>* x);
circularList<T>& reverse();
circularList<T>& merge(circularList<T>& a,circularList<T>& b);
circularList<T>& split(circularList<T>& a,circularList<T>& b);
protected:
void checkIndex(int theIndex) const;
chainNode<T>* lastNode;
chainNode<T>* headerNode;
int listSize;
};
//默认构造一个头节点尾指针指向头节点
template<class T>
circularList<T>::circularList(int initialCapacity)
{
//构造函数
if(initialCapacity < 1)
{
ostringstream s;
s << "Initial capacity = "<<initialCapacity<<"must be > 0";
throw logic_error(s.str());
}
headerNode = new chainNode<T>();
headerNode->next = headerNode;
lastNode = headerNode;
listSize = 0;
}
template<class T>
int circularList<T>::indexOf(const T& theElement) const
{
//返回元素 theBlement 首次出现的索引
// 若该元素不存在,则返回 -1
//将元素 theElement 放入头节点
headerNode->element = theElement;
// 在链表中搜索元素 theElement
chainNode<T>* currentNode = headerNode->next;
// 当前节点的索引
int index = 0;
while(currentNode->element != theElement)
{
//移动到下一个节点
currentNode = currentNode->next;
index++;
}
//确定是否找到元素 theElement
if(currentNode == headerNode)
return -1;
else
return index;
}
template <class T>
circularList<T>::~circularList()
{
clear();
delete headerNode;
headerNode = lastNode = nullptr;
}
template<class T>
void circularList<T>::clear()
{
if(!headerNode) return;
chainNode<T>* currentNode = headerNode->next;
while(currentNode != headerNode)
{
chainNode<T>* next = currentNode->next;
delete currentNode;
currentNode = next;
}
cout<<endl;
lastNode = headerNode;
headerNode->next = headerNode;
listSize = 0;
}
template<class T>
void circularList<T>::checkIndex(int theIndex) const
{
if(theIndex < 0 || theIndex >= listSize)
{
ostringstream s;
s<<"index = "<<theIndex<<" size = "<<listSize;
throw logic_error(s.str());
}
}
template<class T>
T& circularList<T>::get(int theIndex) const
{
checkIndex(theIndex);
chainNode<T>* currentNode = headerNode;
for(int i = 0;i <= theIndex;i++)
{
currentNode = currentNode->next;
}
return currentNode->element;
}
template<class T>
void circularList<T>::insert(int theIndex, const T& theElement)
{
if(theIndex < 0 || theIndex > listSize + 1)
throw logic_error("theIndex < 0 || theIndex > listSize + 1");
chainNode<T>* newNode = new chainNode<T>;
newNode->element = theElement;
if(theIndex == 0)
{
if(listSize == 0)
{
headerNode->next = newNode;
newNode->next = headerNode;
lastNode = newNode;
}
else
{
newNode->next = headerNode->next;
headerNode->next = newNode;
}
}
else
{
chainNode<T>* currentNode = headerNode;
for(int i = 1;i < theIndex; ++i)
currentNode = currentNode->next;
chainNode<T>* nextNode = currentNode->next;
currentNode->next = newNode;
newNode->next = nextNode;
if(theIndex == listSize)
lastNode = newNode;
}
listSize++;
}
template<class T>
void circularList<T>::erase(int theIndex)
{
checkIndex(theIndex);
chainNode<T> * erasePreNode = headerNode;
chainNode<T> * eraseNode = headerNode->next;
for(int i = 0; i < theIndex;++i)
{
erasePreNode = erasePreNode->next;
eraseNode = erasePreNode->next;
}
if(theIndex == 0 && listSize == 1)
{
erasePreNode->next = lastNode = headerNode;
delete eraseNode;
}
else if(theIndex == 0)
{
erasePreNode->next = eraseNode->next;
delete eraseNode;
}
else if(theIndex == listSize - 1)
{
lastNode = erasePreNode;
erasePreNode->next = headerNode;
delete eraseNode;
}
else
{
erasePreNode->next = eraseNode->next;
delete eraseNode;
}
listSize--;
}
template<class T>
void circularList<T>::push_back(const T& theElement)
{
chainNode<T>* newNode = new chainNode<T>(theElement,nullptr);
if(headerNode->next == headerNode)
{
headerNode->next = newNode;
newNode->next = headerNode;
lastNode = newNode;
}
else
{
lastNode->next = newNode;
lastNode = newNode;
newNode->next = headerNode;
}
listSize++;
}
template<class T>
ostream& operator<<(ostream &out,circularList<T>& list)
{
list.output(out);
return out;
}
template<class T>
void circularList<T>::output(ostream &out) const
{
chainNode<T>* currentNode = headerNode->next;
for(int i = 0;i < listSize; i++)
{
out<<currentNode->element<<" ";
currentNode = currentNode->next;
}
assert(lastNode->next == headerNode);
assert(currentNode == headerNode);
}
template<class T>
circularList<T>& circularList<T>::circularList::reverse()
{
if(headerNode == nullptr || headerNode->next == lastNode)
return *this;
chainNode<T>*currentNode = headerNode->next;
chainNode<T>*currentNextNode = currentNode->next;
lastNode = headerNode->next;
while(currentNextNode->next != headerNode)
{
currentNode = currentNextNode;
currentNextNode = currentNode->next;
currentNode->next = headerNode->next;
headerNode->next = currentNode;
}
currentNextNode->next = currentNode;
headerNode->next = currentNextNode;
lastNode->next = headerNode;
return *this;
}
template<class T>
circularList<T>& circularList<T>::merge(circularList<T>& a,circularList<T>& b)
{
chainNode<T>* headerNodeA = a.headerNode;
chainNode<T>* headerNodeB = b.headerNode;
chainNode<T>* currentNodeA = a.headerNode->next;
chainNode<T>* currentNodeB = b.headerNode->next;
chainNode<T>* currentNextNodeA = currentNodeA->next;
chainNode<T>* currentNextNodeB = currentNodeB->next;
chainNode<T>* currentNode = headerNode;
int i = 0,j = 0;
while(i < a.listSize && j < b.listSize)
{
if(currentNodeA->element <= currentNodeB->element)
{
currentNode->next = currentNodeA;
currentNodeA->next = headerNode;
currentNodeA = currentNextNodeA;
currentNextNodeA = currentNextNodeA->next;
headerNodeA->next = currentNodeA;
currentNode = currentNode->next;
i++;
}
else
{
currentNode->next = currentNodeB;
currentNodeB->next = headerNode;
currentNodeB = currentNextNodeB;
currentNextNodeB = currentNextNodeB->next;
headerNodeB->next = currentNodeB;
currentNode = currentNode->next;
j++;
}
}
for(;i < a.listSize;i++)
{
currentNode->next = currentNodeA;
currentNodeA->next = headerNode;
currentNodeA = currentNextNodeA;
currentNextNodeA = currentNextNodeA->next;
headerNodeA->next = currentNodeA;
currentNode = currentNode->next;
}
for(;j < b.listSize;j++)
{
currentNode->next = currentNodeB;
currentNodeB->next = headerNode;
currentNodeB = currentNextNodeB;
currentNextNodeB = currentNextNodeB->next;
headerNodeB->next = currentNodeB;
currentNode = currentNode->next;
}
lastNode = currentNode;
if(currentNode)
currentNode->next = headerNode;
listSize = a.listSize + b.listSize;
a.listSize = b.listSize = 0;
a.lastNode = a.headerNode;
b.lastNode = b.headerNode;
return *this;
}
template<class T>
circularList<T>& circularList<T>::split(circularList<T>& a,circularList<T>& b)
{
if(listSize == 0)
return *this;
chainNode<T>* currentNodeA = a.headerNode;
chainNode<T>* currentNodeB = b.headerNode;
chainNode<T>* currentNode = headerNode->next;
for(int i = 0;i < listSize && currentNode != headerNode;i++)
{
if(i % 2 == 0)
{
if(currentNodeA->next == currentNodeA)
currentNodeA->next = currentNode;
currentNodeA->next = currentNode;
currentNodeA = currentNode;
a.listSize++;
}
else
{
if(currentNodeB->next == currentNodeB)
currentNodeB->next = currentNode;
currentNodeB->next = currentNode;
currentNodeB = currentNode;
b.listSize++;
}
currentNode = currentNode->next;
}
cout<<endl;
currentNodeA->next = a.headerNode;
currentNodeB->next = b.headerNode;
a.lastNode = currentNodeA;
b.lastNode = currentNodeB;
listSize = 0;
lastNode = currentNode;
currentNode->next = headerNode;
return *this;
}
template<class T>
void circularList<T>::removeNode(chainNode<T>* x)
{
if(listSize == 1)
{
delete headerNode;
headerNode = lastNode;
lastNode->next->headerNode;
listSize = 0;
return;
}
if(x == lastNode)
{
x->element = 0;
x->next = headerNode->next;
delete headerNode;
headerNode = x;
listSize--;
if(listSize == 1)
{
lastNode = headerNode->next;
}
else
{
for(lastNode = headerNode->next;
lastNode->next != headerNode;
lastNode = lastNode->next)
;
}
}
else
{
chainNode<T>* nextNode = x->next;
x->element = x->next->element;
x->next = x->next->next;
if(next == lastNode)
lastNode = x;
delete next;
listSize--;
}
}
int main()
{
cout<<"----------------------insert start--------------------"<<endl;
circularList<double> list;
for(int i = 0;i < 10;i++)
{
list.insert(0,i);
}
cout<<list<<endl;
cout<<"----------------------insert end--------------------"<<endl;
cout<<"----------------------get start--------------------"<<endl;
for(int i = 0; i < list.size();i++)
{
cout<<list.get(i)<<" ";
}
cout<<endl;
cout<<"----------------------get end--------------------"<<endl;
cout<<"----------------------indexOf start--------------------"<<endl;
for(int i = 0; i < 10;i++)
{
cout<<list.indexOf(i)<<" ";
}
cout<<endl;
cout<<"----------------------indexOf end--------------------"<<endl;
cout<<"----------------------erase start--------------------"<<endl;
list.erase(0);
list.erase(8);
for(int i = 0;i<8;i++)
{
list.erase(0);
cout<<list<<endl;
}
cout<<"----------------------erase end--------------------"<<endl;
cout<<"----------------------clear start--------------------"<<endl;
for(int i = 0;i < 10;i++)
{
list.insert(0,i);
}
cout<<list<<endl;
cout<<list.size()<<endl;
list.clear();
cout<<list<<endl;
cout<<list.size()<<endl;
cout<<"----------------------clear end--------------------"<<endl;
cout<<"----------------------push_back start--------------------"<<endl;
for(int i = 0;i < 10;i++)
{
list.push_back(i);
cout<<list<<endl;
}
cout<<"----------------------push_back end--------------------"<<endl;
cout<<"----------------------reverse start--------------------"<<endl;
list.reverse();
cout<<list<<endl;
cout<<"----------------------reverse end--------------------"<<endl;
cout<<"----------------------merge start--------------------"<<endl;
list.reverse();
cout<<list<<endl;
circularList<double> list1;
for(int i = 0;i < 10;i++)
{
list1.push_back(2*i);
}
circularList<double> list2;
for(int i = 0;i < 10;i++)
{
list2.push_back(2*i+1);
}
circularList<double> list3;
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list3<<endl;
list3.merge(list1,list2);
//list3.insert(0,12);
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list3<<endl;
cout<<"----------------------merge end--------------------"<<endl;
cout<<"----------------------split start--------------------"<<endl;
list1.clear();
list2.clear();
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list3<<endl;
list3.split(list1,list2);
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list3<<endl;
cout<<"----------------------split end--------------------"<<endl;
return 0;
}
测试输出
xz@xiaqiu:~/study/algorithm/c++/1/build$ ./test
----------------------insert start--------------------
9 8 7 6 5 4 3 2 1 0
----------------------insert end--------------------
----------------------get start--------------------
9 8 7 6 5 4 3 2 1 0
----------------------get end--------------------
----------------------indexOf start--------------------
9 8 7 6 5 4 3 2 1 0
----------------------indexOf end--------------------
----------------------erase start--------------------
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
----------------------erase end--------------------
----------------------clear start--------------------
9 8 7 6 5 4 3 2 1 0
10
0
----------------------clear end--------------------
----------------------push_back start--------------------
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
----------------------push_back end--------------------
----------------------reverse start--------------------
9 8 7 6 5 4 3 2 1 0
----------------------reverse end--------------------
----------------------merge start--------------------
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
1 3 5 7 9 11 13 15 17 19
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
----------------------merge end--------------------
----------------------split start--------------------
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 2 4 6 8 10 12 14 16 18
1 3 5 7 9 11 13 15 17 19
----------------------split end--------------------
xz@xiaqiu:~/study/algorithm/c++/1/build$