單鏈表(
節點類
#ifndef Node_H
#define Node_H
template <class Type> class Node //單鏈節點類
{
public:
Type data;
Node<Type> *link;
Node() : data(Type())
Node(const Type &item) : data(item)
Node(const Type &item
};
#endif
【說明】因為數據結構裡用到這個結構的地方太多了
【重要修改】原書的缺省構造函數是這樣的Node() : data(NULL)
【閒話】請不要對int *p = new int(
單鏈表類定義與實現
#ifndef List_H
#define List_H
#ifndef TURE
#define TURE
#endif
#ifndef FALSE
#define FALSE
#endif
typedef int BOOL;
#include
template <class Type> class List //單鏈表定義
{
//基本上無參數的成員函數操作的都是當前節點
//認為表中
public:
List() { first = current = last = new Node<Type>; prior = NULL; }
~List() { MakeEmpty(); delete first; }
void MakeEmpty() //置空表
{
Node<Type> *q;
while (first
{
q = first
first
delete q;
}
Initialize();
}
BOOL IsEmpty()
{
if (first
{
Initialize();
return TURE;
}
else return FALSE;
}
int Length() const //計算帶表頭節點的單鏈表長度
{
Node<Type> *p = first
int count =
while (p != NULL)
{
p = p
count++;
}
return count;
}
Type *Get()//返回當前節點的數據域的地址
{
if (current != NULL) return ¤t
else return NULL;
}
BOOL Put(Type const &value)//改變當前節點的data
{
if (current != NULL)
{
current
return TURE;
}
else return FALSE;
}
Type *GetNext()//返回當前節點的下一個節點的數據域的地址
{
if (current
else return NULL;
}
Type *Next()//移動current到下一個節點
{
if (current != NULL && current
{
prior = current;
current = current
return ¤t
}
else
{
return NULL;
}
}
void Insert(const Type &value)//在當前節點的後面插入節點
{
Node<Type> *p = new Node<Type>(value
current
}
BOOL InsertBefore(const Type &value)//在當前節點的前面插入一節點
{
Node<Type> *p = new Node<Type>(value);
if (prior != NULL)
{
p
prior
prior = p;
return TURE;
}
else return FALSE;
}
BOOL Locate(int i)//移動current到第i個節點
{
if (i <=
current = first
for (int j =
prior = current;
if (current != NULL) return TURE;
else return FALSE;
}
void First()//移動current到表頭
{
current = first;
prior = NULL;
}
void End()//移動current到表尾
{
if (last
{
for ( ;current
prior = current;
last = current;
}
current = last;
}
BOOL Find(const Type &value)//移動current到數據等於value的節點
{
if (IsEmpty()) return FALSE;
for (current = first
current = current
prior = current;
if (current != NULL) return TURE;
else return FALSE;
}
BOOL Remove()//刪除當前節點
{
if (current != NULL && prior != NULL)
{
Node<Type> *p = current;
prior
current = p
delete p;
return TURE;
}
else return FALSE;
}
BOOL RemoveAfter()//刪除當前節點的下一個節點
{
if (current
{
Node<Type> *p = current
current
delete p;
return TURE;
}
else return FALSE;
}
friend ostream & operator << (ostream & strm
{
l
while (l
strm << endl;
l
return strm;
}
protected:
/*主要是為了高效的入隊算法所添加的
{
Node<Type> *p = new Node<Type>(value
last
last = p;
}
void Initialize()//當表為空表時使指針復位
{
current = last = first;
prior = NULL;
}
//這部分函數返回類型為Node<Type>指針
Node<Type> *pGet()
{
return current;
}
Node<Type> *pNext()
{
prior = current;
current = current
return current;
}
Node<Type> *pGetNext()
{
return current
}
Node<Type> *pGetFirst()
{
return first;
}
Node<Type> *pGetLast()
{
return last;
}
Node<Type> *pGetPrior()
{
return prior;
}
void PutLast(Node<Type> *p)
{
last = p;
}
//這部分插入刪除函數不建立或刪除節點
void Insert(Node<Type> *p)
{
p
current
}
void InsertBefore(Node<Type> *p)
{
p
prior
prior = p;
}
void LastInsert(Node<Type> *p)
{<
From:http://tw.wingwit.com/Article/program/sjjg/201311/23010.html