文档介绍:[首页,上一页,下一页;目录]
第二章
精品快线之计算机
链表是最常用、最简单和最基本的数据结构之一。我们先来看看单链表的实现。
单链表的实现如下:
///////////////////////////////////////////////////////////////////////////////
//
//
FileName
//
Version :
//
Author
Luo Cong
//
Date
2004-12-29 9:58:38
//
Comment
//
///////////////////////////////////////////////////////////////////////////////
#ifndef __SI NGLE_LI ST_H__
define __SI NGLE_LIST_H__
include < >
include < >
#ifdef _DEBUG
#define DEBUG_NEW new (_NORMAL_BLOCK, THI8_FILE, __LINE__)
endif
#ifdef _DEBUG
define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FI LE[] = __FI LE # endif
#ifdef _DEBUG
#ifndef ASSERT
define ASSERT assert
endif
else // not _DEBUG
#ifndef ASSERT
define ASSERT
endif
endif // _DEBUG
template<typename T>
class CNode
(
public:
T data;
CNode< T> * next;
CNode() : data(T()), next(NULL) (}
CNode(const T &initdata) : data(initdata), next(NULL) ()
CNode(const T &initdata, CNode<T> * p) : data(initdata), next(p) () );
template<typename T>
class CSList
(
protected:
int m_nCount;
CNode< T> * m_pNodeHead;
public:
CSList();
CSList(const T &initdata);
~CSList();
public:
int lsEmpty() const;
int GetCount() const;
int I nsertBefore(const int pos, const T data);
int I nsertAfter(const int pos, const T data);
int AddHead(const T data);
int AddTail(const T data);
void RemoveAt(const int pos);
void RemoveHead();
void RemoveTail();
void RemoveAII();
T& GetTail();
T GetTail() const;
T& GetHead();
T GetHead() const;
T& GetAt(const int pos);
T Get At (con st int pos) const;
void Set At (con st int pos, T data);
int Find(const T data) const;
};
template<typename T>
inline C8List<T> ::CSList() : m_nCount(0), m_pNodeHead(NULL)
template<typename T>
inline CSList<T> ::CSList(const T &initdata) : m_nCount(0)3 m_pNodeHead(NULL)
(
AddHead(initdata);
)
template<typename T> inline CSListv T> :: ~ CSList() (
RemoveAII();
}
template<typename T>
inline