单链表以及两个集合“交”的程序:
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 20;
template <class T> class SingleLst;
template <class T>
class LinearList
{
public:
virtual bool IsEmpty() const = 0;
virtual int Length() const = 0;
virtual bool Find(int i, T& x) const = 0; virtual int Search(T x) const = 0;
virtual bool Insert(int i, T x) = 0;
virtual bool Delete(int i) = 0;
virtual bool Update(int i, T x) = 0;
virtual void Output(ostream& out) const = 0;
protected:
int n;
};
template <class T>
class Node
{
private:
T element;
Node<T>* link;
friend class SingleList<T>;
};
template <class T>
class SingleList :public LinearList<T>
{
public:
SingleList(){ first = NULL; n = 0; }
~SingleList();
bool IsEmpty() const;
int Length() const;
bool Find(int i, T& x) const;
int Search(T x) const;
bool Insert(int i, T x);
bool Delete(int i);
bool Update(int i, T x);
void Clear();
void Output(ostream& out)const;
private:
Node<T>* first;
};
template <class T>
SingleList<T>::~SingleList()
{
Node<T>* p;
while (first)
{
p = first->link;
delete first;
first = p;
}
}
template <class T>
bool SingleList<T>::IsEmpty() const
{
return n == 0;
}
template <class T>
int SingleList<T>::Length() const
{
return n;
}
template<class T>
bool SingleList<T>::Find(int i, T& x) const
{
if (i<0 || i>n - 1)
{
cout << "out of Bounds" << endl;
return false;
}
Node<T>* p = first;
for (iny j = 0; j < i; j++)
p = p - > link;
x = p->element;
return true;
}
template<class T>
int SingleList<T>::Search(T x) const
{
Node<T>* p = first;
for (iny j = 0; p&&p->element!=x; j++)
p = p - > link;
if (p)
return j;
return -1;
}
template<class T>
bool SingleList<T>::Insert(int i, T x)
{
if (i<-1 || i>n - 1)
{
cout << "out Of Bounds" << endl; return false;
}
Node<T>* q = new Node<T>;
q->element = x;
Node<T>* p=first;
for (iny j = 0; j < i; j++)
p = p - > link;
if (i > -1)
{
q->link = p->link;
p->link = q;
}
else
{
q->link = first;
first = q;
}
n++;
return true;
}
template <class T>
bool SingleList<T>::Delete(int i)
{
//下溢出检查
if (!n)
{
cout << "UnderFlow" << endl; return false;
}
if (i<0 || i>n - 1)
{
cout << "out Of Bounds" << endl; return false;
}
Node<T>* p = first, * q = first;
for (int j = 0; j < i - 1; j++)
q = q->link;
if (i == 0)
first = first->link;
else
{
p = q->link;
q->link = p->link;
}
delete p;
n--;
return true;
}
template <class T>
bool SingleList<T>::Update(int i, T x)
{
if (i<0 || i>n - 1)
{
cout << "out of Bounds" << endl;
return false;
}
Node<T>* p = first;
for (int j = 0; j < i; j++)
p = p->link;
p->element = x;
return true;
}
template <class T>
void SingleList<T>::Output(ostream& out)const
{
Node<T>* p = first;
while (p)
{
out << p->element << " ";
p = p->link;
}
out << endl;
}
template <class T>
void Intersection(SingleList<T>& LA, SingleList<T>& LB)
{
T x;
int i = 0;
while (i < LA.Length())
{
LA.Find(i, x);
if (LB.Search(x) == -1)
LA.Delete(i);
else
i++;
}
}
void main()
{
SingleList<int> LA;//定义顺序表对象LA,表示集合A
SingleList<int> LB;//定义顺序表对象LB,表示集合B
for (int i = 0; i < 5; i++)
LA.Insert(i - 1, i); //插人元素0~4,构造集合LA={0,1,2,3,4}
for (int i = 5; i < 10; i++)
LB.Insert(i - 6,i);//插人5~9,构造集合B={5,6,7,8,9}
LB.Insert(-1,0);//LB中插入0,LB={0,5,6,7,8,9}
LB.Insert(3,2);//LB中插入2,LB={0,5,6,7,2,8,9}
LB.Insert(LB.Length()-1,4);//LB中插人4, LB={0,5,6,7,2,8,9,4}
Intersection(LA,LB);//两集合并,结果放人LA中
LA.Output(cout);//输出最终结果LA={0,1,2,3,4,5,6,7,8,9}
//system("pause");
}
带表头结点单链表的构造、插入和删除函数:
template <class T>
HeaderList<T>::HeaderList()
{
Node<T>* first = new Node<T>;
first->link = NULL;
n = 0;
}
template <class T>
HeaderList<T>::Insert(int i,T x)
{
if (i<-1 || i>n - 1)
{
cout << "out Of Bounds" << endl;
return false;
}
Node<T>* p = first;
for (int j = 0; j <= i; j++)
p = p->link;
Node<T>* q = new Node<T>;
q->element = x;
q->link = p->link;
p->link = q;
n++;
return true;
}
template <class T>
HeaderList<T>::Delete(int i)
{
if (!n)
{
cout << "UnderFlow" << endl;
return false;
}
if (i<0 || i>n - 1)
{
cout << "out Of Bounds" << endl;
return false;
}
Node<T>* q = first,*p;
for (int j = 0; j < i; j++)
q = q->link;
p - q->link;
q->link = p->link;
delete p;
n--;
return true;
}
双向链表:
template<T> class DoubleList;
template<T>
class DNode
{
private:
T element;
DNode<T>* lLink, * rLink;
friend DoubleList<T>;
};
插入操作:(在p结点前插入)
DNode<T> *q=new DNode<T>;
q->element=x;
q->lLink=p->lLink;
q->rLink=p;
p->lLink->rLink=q;
p->lLink=q;
删除操作:
p->lLink->rLink=p->rLink;
p->rLink->lLink=p->lLink;
delete p;