欢迎来到天天文库
浏览记录
ID:19506931
大小:141.50 KB
页数:16页
时间:2018-10-02
《数据结构与算法分析》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、[首页,上一页,下一页; 目录]第二章单链表链表是最常用、最简单和最基本的数据结构之一。我们先来看看单链表的实现。2.1 代码实现单链表的实现如下:///////////////////////////////////////////////////////////////////////////////////FileName:slist.h//Version:0.10//Author:LuoCong//Date:2004-12-299:58:38//Comment://////////////////////////////////////////////
2、///////////////////////////////////#ifndef__SINGLE_LIST_H__#define__SINGLE_LIST_H__#include#include#ifdef_DEBUG#defineDEBUG_NEWnew(_NORMAL_BLOCK,THIS_FILE,__LINE__)#endif#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif#ifdef_DEBUG
3、#ifndefASSERT#defineASSERTassert#endif#else//not_DEBUG#ifndefASSERT#defineASSERT#endif#endif//_DEBUGtemplateclassCNode{public:Tdata;CNode*next;CNode():data(T()),next(NULL){}CNode(constT&initdata):data(initdata),next(NULL){}CNode(constT&initdata,CNode*p):data(initdata)
4、,next(p){}};templateclassCSList{protected:intm_nCount;CNode*m_pNodeHead;public:CSList();CSList(constT&initdata);~CSList();public:intIsEmpty()const;intGetCount()const;intInsertBefore(constintpos,constTdata);intInsertAfter(constintpos,constTdata);intAddHead(constTdata);int
5、AddTail(constTdata);voidRemoveAt(constintpos);voidRemoveHead();voidRemoveTail();voidRemoveAll();T&GetTail();TGetTail()const;T&GetHead();TGetHead()const;T&GetAt(constintpos);TGetAt(constintpos)const;voidSetAt(constintpos,Tdata);intFind(constTdata)const;};templateinlineCSList
6、::CSList():m_nCount(0),m_pNodeHead(NULL){}templateinlineCSList::CSList(constT&initdata):m_nCount(0),m_pNodeHead(NULL){AddHead(initdata);}templateinlineCSList::~CSList(){RemoveAll();}templateinlineintCSList::IsEmpty()const{return0==m_nCount;
7、}templateinlineintCSList::AddHead(constTdata){CNode*pNewNode;pNewNode=newCNode;if(NULL==pNewNode)return0;pNewNode->data=data;pNewNode->next=m_pNodeHead;m_pNodeHead=pNewNode;++m_nCount;return1;}templateinlineintCSList::AddTail(constTdata){returnInsertA
8、fter(
此文档下载收益归作者所有