循环链表(测试代码,Qt5.1 for windows)

循环链表的原理很简单,这里就不在赘述。下面是循环链表的测试代码:
主函数所在文件:
circularlistwithheader.cpp

/*
 * 主函数
*/
#include<iostream>
#include "circularlistwithheader.h"

using namespace std;

int main(int argc, char *argv[])
{
    //测试构造函数
    circularListWithHeader<int>y,z;
    cout<<"Initial size of y and z = "
       <<y.size()<<", "
      <<z.size() <<endl;

    //测试insert()函数
    y.insert(0,2);
    y.insert(1,6);
    cout<<"after insert 2 and 6:";y.output(cout);cout<<endl;

    y.insert(0,1);
    y.insert(2,4);
    cout<<"after insert 1 and 4:";y.output(cout);cout<<endl;

    y.insert(3,5);
    y.insert(2,3);
    cout<<"after insert 5 and 3:";y.output(cout);cout<<endl;

     //测试size()函数
    cout<<"size of y = "<<y.size()<<endl;

    //测试indexOf()函数
    int index = y.indexOf(5);
    if(index < 0)
    {
        cout << "4 not fouond" <<endl;
    }
    {
        cout << "The index of 4 is" <<index <<endl;
    }

    index = y.indexOf(7);
    if(index < 0)
    {
        cout << "7 not found" <<endl;
    }
    else
    {
        cout << "The index of 7 is " <<index <<endl;
    }
    return 0;
}

chainNode.h

/*
 *  chainNode.h
 *  链表节点定义
 *
*/

#ifndef CHAINNODE_H
#define CHAINNODE_H

template<class T>
struct chainNode
{
    //数据成员
    T element;
    chainNode<T> *next;

    //方法
    chainNode(){}
    chainNode(const T& element)
    {
        this->element = element;
    }
    chainNode(const T& element,chainNode<T>* next)
    {
        this->element = element;
        this->next = next;
    }
};
#endif // CHAINNODE_H

circularListWithHeader.h

/*
 * circularListWithHeader.h
 * 循环链表实体类定义
*/

#ifndef CIRCULARLISTWITHHEADER_H
#define CIRCULARLISTWITHHEADER_H

#include<iostream>
#include<sstream>
#include<string>
#include "chainnode.h"
#include "myexceptions.h"

using namespace std;

template<class T>
class circularListWithHeader
{
public:
    //构造函数
    circularListWithHeader();

    /*
     * 一些其他函数
    */
    //返回链表中元素个数
    int size() const {return listSize;}
    int indexOf(const T& theElement) const;
    void insert(int theIndex,const T& theElement);
    void output(ostream& out) const;
protected:
    //检查索引是否合法
    void checkIndex(int theIndex) const;

    chainNode<T>* headerNode;//指向头节点
    int listSize;//元素个数
};

/*
 * 类中各个函数的具体实现
 *
*/
template<class T>
circularListWithHeader<T>::circularListWithHeader()
{
    headerNode = new chainNode<T>();
    headerNode->next = headerNode;
    listSize = 0;
}

template<class T>
void circularListWithHeader<T>::checkIndex(int theIndex) const
{
    if(theIndex < 0 || theIndex >= listSize)
    {
        ostringstream s;
        s <<"index = "<<theIndex<<" size = "<<listSize;
        throw illegalIndex(s.str());
    }
}

template<class T>
int circularListWithHeader<T>::indexOf(const T &theElement) const
{
    headerNode->element = theElement;

    chainNode<T>* currentNode = headerNode->next;
    int index = 0;
    while(currentNode->element != theElement)
    {
        currentNode = currentNode->next;
        index++;
    }
    if(currentNode == headerNode)
        return -1;
    else
        return index;
}

template<class T>
void circularListWithHeader<T>::insert(int theIndex, const T &theElement)
{
    if(theIndex < 0 || theIndex > listSize)
    {
        ostringstream s;
        s<<"index = "<<theIndex<<" size = "<<listSize;
        throw illegalIndex(s.str());
    }

    chainNode<T>* p = headerNode;
    for(int i = 0;i< theIndex;i++)
    {
        p = p->next;
    }
    p->next = new chainNode<T>(theElement,p->next);
    listSize++;
}

template<class T>
void circularListWithHeader<T>::output(ostream &out) const
{
    for(chainNode<T>* currentNode = headerNode->next;
                        currentNode != headerNode;
                        currentNode = currentNode->next)
        out<<currentNode->element<<" ";
}

//<<的重载
template<class T>
ostream& operator <<(ostream& out,const circularListWithHeader<T>& x)
{
    x.output(out);
    return out;
}

#endif // CIRCULARLISTWITHHEADER_H

myExceptions.h

/*
 * myExceptions.h
 * 这个类包含类对各种异常的处理类
*/
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H

#include<string>
using namespace std;

//不合法的参数值
class illegalParameterValue
{
public:
    illegalParameterValue(string theMessage = "Illegal patameter value")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout<<message<<endl;
    }
 private:
    string message;
};

//输入数据不合法
class illegalInputData
{
public:
    illegalInputData(string theMessage = "Illegal data input")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout <<message <<endl;
    }
private:
    string message;
};

//索引不合法
class illegalIndex
{
public:
    illegalIndex(string theMessage = "Illegal index")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout<<message<<endl;
    }
private:
    string message;
};

#endif // MYEXCEPTIONS_H

猜你喜欢

转载自my.oschina.net/u/1771419/blog/1794560