//线性表的顺序存储结构
template
class Linearlist
{
public:
Linearlist(int MaxListSize == 10);
~Linearlist()
{
delete []element;
}
bool IsEmpty() const
{
return length == 0;
}
bool IsFull() const
{
return length == MaxSize;
}
int Length() const
{
return length;
}
bool Find(int k, T &item) const;
int search(const T &item) const;
void Delete(int k, T &item);
void Insert(int k, const T &item);
private:
int MaxSize, length, T *element;
};
template
Linearlist
::Linearlist(int MaxListSize == 10) { MaxSize = MaxListSize; element = new T[MaxSize]; //申请规模为MaxSize的数组空间 length = 0; // 初始时没有真正的表结点,故表长度为0 } //存取:将下标为k的结点的字段值赋给item并返回true,若不存在则返回false; template
bool Linearlist
::Find(int k, T &item) const { if (k < 0 || length == 0 || k >length-1) { cout << " unreasonable position or empty list!" << endl; return false; } else { item = element[k]; return true; } } //查找:在表中查找字段值为item的结点并返回其下标;若表中没有item,则返回-1; template
int Linearlist
::search(const T &item) const { for (int k = 0; k <= length-1; k++) { if(element[k] == item) return k; } return -1; } //删除表中下表为k的结点并将其值赋给item template
void Linearlist
::Delete(int k, T &item) { if (find(k, item)) //若找到下标为k的结点,则将其后面所有结点均向前移动一个位置 { for (int i=k; i
void Linearlist
::Insert(int k, const T &item) { if(IsFull()) //若表已满,无法插入新结点 { cout << "The list is full!" << endl; exit(1); } else if (k<0 || k>length-1) { cout << "The node does not exist!" << endl; exit(1); } else { for (i=length-1; i>=k+1; i--) { element[i+1] = element[i]; } } element[k+1] = item; length++; } //单链表结点结构SLNode template
struct SLNode { T data; //数据域 SLNode
*next; //指针域 SLNode(SLNode *nextNode = NULL) { next = nextNode; } SLNode(const T &item, SLNode *nextNode = NULL) { data = item; next = nextNode; } }; // SLNode 的定义参考博文数据结构-栈的一些基础操作c++代码 //单链表的链式存储结构 template
class SLList { public: SLList(){ head = new SLNode
()}; //构造函数,构造一个只有哨位结点的空表 SLList(T &item); //构造函数 ~SLList(); bool IsEmpty() const {return head->next == NULL;}; int length() const; // bool Find(int k, T &item) const; int Search(const T &item) const; void Delete(int k, T &item); void Insert(int k, const T &item); private: SLNode
*head; SLNode
*tail; SLNode
*currptr; }; //单链表的构造函数,生成一个只有哨位结点的空表 template
SLList
::SLList() { head = tail = currptr = new SLNode
(); size = 0; } //单链表的构造函数,生成含有哨位结点和一个表结点的表 template
SLList
::SLList(T &item) { tail = currptr = new SLNode
(item); //生成一个表结点 head = new SLNode
(currptr); //生成哨位结点 size = 1; } //单链表的析构函数 template
SLList
::~SLList() { while (!IsEmpty()) { currptr = head->next; head->next = currptr->next; delete currptr; } delete head; } //算法Insert //在当前结点后插入一个data域值为item的结点 template
void SLList
::Insert(const T &item) { currptr->next = new SLNode
(item, currptr->next); if (tail == currptr) tail = currptr->next; size++; } //在表尾插入一个data域值为item的结点 template
void SLList
::InsertFromTail(const T &item) { tail->next = new SLNode
(item, NULL); tail = tail->next; size++; } //在哨位结点后插入 template
void SLList
::InsertFromHead(const T &item) { if(IsEmpty()) { head->next = new SLNode
(item, head->next); tail = head->next; } else { head->next = new SLNode
(item, head->next); } size++; } //算法Delete //删除当前结点的后继结点并将其data值返回给变量de_item template
bool SLList
::Delete(T &de_item) { if(currptr == tail || IsEmpty()) { cout << "No next node or empty list!"; return false; } SLNode
*temp = currptr->next; currptr->next = temp->next; size--; de_item = temp->data; if(temp == tail) tail = currptr; delete temp; return true; } // 删除哨位结点后的第一个真正表结点并将其data值返回给变量de_item template
bool SLList
::DeleteFromHead(T &de_item) { if (IsEmpty()) { cout << "Empty list!"; return false; } SLNode
*temp = head->next; head->next = temp->next; size--; de_item = temp->data; if (temp == tail) { tail = head; } delete temp; return true; } //删除表尾结点并将其data值返回给变量de_item template
bool SLList
::DeleteFromTail(T &de_item) {