数据结构-带头结点的循环链表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
File chainNode.h
#ifndef chainNode_
#define chainNode_
template
struct chainNode
{
// data members
T element;
chainNode
// methods
chainNode() {next=NULL;}
chainNode(const T& element)
{
this->element = element;
this->next=NULL;
}
chainNode(const T& element, chainNode
this->next = next;}
};
#endif
File linnerList.h
// abstract class linearList
// abstract data type specification for linear list data structure
// all methods are pure virtual functions
#ifndef linearList_
#define linearList_
#include
using namespace std;
template
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
// return true iff list is empty
virtual int size() const = 0;
// return number of elements in list
virtual T& get(int theIndex) const = 0;
// return element whose index is theIndex virtual int indexOf(const T& theElement) const = 0;
// return index of first occurence of theElement virtual void erase(int theIndex) = 0;
// remove the element whose index is theIndex virtual void insert(int theIndex, const T& theElement) = 0;
// insert theElement so that its index is theIndex virtual void output(ostream& out) const = 0;
// insert list into stream out
};
#endif
File circularListWithHeader.h
// circularList list with header node and iterator
#ifndef circularListWithHeader_
#define circularListWithHeader_
#include
#include
#include
#include"chainNode.h"
#include"myExceptions.h"
#include"linearList.h"
usingnamespace std;
template
class circularListWithHeader
{
public:
// 构造函数
circularListWithHeader();
// some methods
bool empty(){return listSize = 0;}
int size() const {return listSize;}
T&get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out) const;
void reverse();
// iterators to start and end of list
class iterator;
iterator begin() {return iterator(headerNode->next);}
iterator end() {return iterator(headerNode);}
// iterator for chain
class iterator
{
public:
// typedefs required by C++ for a forward iterator
typedef forward_iterator_tagiterator_category;
typedef T value_type;
typedef ptrdiff_tdifference_type;
typedef T* pointer;
typedef T&reference;
// 构造函数
iterator(chainNode
{node = theNode;}
// 解引用操作符
T&operator*() const {return node->element;}
T* operator->() const {return&node->element;}
// 迭代器加法操作
iterator&operator++() // preincrement
{node = node->next; return *this;} iterator operator++(int) // postincrement
{iterator old = *this;