线性表的ADT
List.h
//线性表的C++抽象类声明
template<class Elem>
class List
{
public:
virtual void clear() = 0;
virtual bool insert(const Elem&) = 0;
virtual bool append(const Elem&) = 0;
virtual bool remove(Elem&) = 0;
virtual void setStart() = 0;
virtual void setEnd() = 0;
virtual void prev() = 0;
virtual void next() = 0;
virtual int leftLength() const = 0;
virtual int rightLength() const = 0;
virtual bool setPos(int pos) = 0;
virtual bool getValue(Elem&) const = 0;
virtual void print() const = 0;
};
单链表节点的定义
link.h
// 单链表节点类的定义
template <class Elem>
class Link
{
public:
//数据成员声明为公有的,从技术上讲,违反了封装原则,应该为私有的。
//这里,为了用于栈和队列的实现,声明为公有的。
Elem element; // Value for this node
Link *next; // Pointer to next node in list
Link(const Elem& elemval, Link* nextval = NULL)
{
element = elemval;
next = nextval;
}
Link(Link* nextval = NULL)
{
next = nextval;
}
};
链表的实现声明 & 成员函数的是实现
// 链表的实现声明
#include "stdafx.h"
#include "List.h"
#include "link.h"
template <class Elem>
class LList: public List<Elem>
{
private:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
int leftcnt;
int rightcnt;
void init()
{
fence = tail = head = new Link<Elem>;
leftcnt = rightcnt = 0;
}
void removeall()
{
while(head != NULL)
{
fence = head;
head = head->next;
delete fence;
}
}
public:
LList()
{
init();
}
~LList()
{
removeall();
}
void clear()
{
removeall();
init();
}
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart()
{
fence = head;
rightcnt += leftcnt;
leftcnt = 0;
}
void setEnd()
{
fence = tail;
leftcnt += rightcnt;
rightcnt = 0;
}
void prev();
void next()
{
if(fence != tail)
{
fence = fence->next;
rightcnt--;
leftcnt++;
}
}
int leftLength() const
{
return leftcnt;
}
int rightLength() const
{
return rightcnt;
}
bool setPos(int pos);
bool getValue(Elem& it) const
{
if(rightLength() == 0)
{
return false;
}
it = fence->next->element;
return true;
}
void print() const;
};
template<class Elem>
bool LList<Elem>::insert(const Elem& item)
{
fence->next = new Link<Elem>(item, fence->next);
if(tail == fence)
{
tail = fence->next;
}
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::append(const Elem& item)
{
tail = tail->next = new Link<Elem>(item, NULL);
rightcnt++;
return true;
}
template<class Elem>
bool LList<Elem>::remove(Elem& it)
{
if(fence->next == NULL)
{
return false;
}
it = fence->next->element;
Link<Elem>* ltemp = fence->next;
fence->next = ltemp->next;
if(tail == ltemp)
{
tail = fence;
}
delete ltemp;
rightcnt--;
return true;
}
template<class Elem>
void LList<Elem>::prev()
{
Link<Elem>* temp = head;
if(fence == head) return;
while(temp->next != fence)
{
temp = temp->next;
}
fence = temp;
leftcnt--;
rightcnt++;
}
template<class Elem>
bool LList<Elem>::setPos(int pos)
{
if((pos < 0) || (pos > rightcnt + leftcnt))
{
return false;
}
fence = head;
for(int i = 0; i < pos; i++)
{
fence = fence->next;
}
return true;
}
template<class Elem>
void LList<Elem>::print() const
{
Link<Elem>* temp = head;
cout << "< ";
while(temp != fence)
{
cout << temp->next->element << " ";
temp = temp->next;
}
cout << "| ";
while(temp->next != NULL)
{
cout << temp->next->element << " ";
temp = temp->next;
}
cout << ">\n ";
}
测试:
#include "stdafx.h"
#include <iostream>
#include "LList.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int max = 0;
cout << "链表容量:\n";
cin >> max;
LList<int> myList;
for(int i = 0; i < max; i++)
{
myList.append(i);
}
myList.print();
cout << endl;
return 0;
}