数据结构_线性链表
线性链表的定义如图
算法2.1
两个线性链表LA, LB ,现在求一个新的链表LA = LA ∩ LB
eg.LA={0,1,2,3,4,5,6,7,8,9} LB={0,1,2,3,4,5} LA=LA∩LB={0,1,2,3,4,5∩}
//linearlist.h
#ifndef LINEARLIST_H
#define LINEARLIST_H
#include <iostream>
using namespace std;
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; //线性表的长度
};
#endif
//singlelisti.h
#ifndef SINGLELIST_H
#define SINGLELIST_H
#include "linearlist.h"
template <class T> class SingleList; //超前声明SingleList
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>
int SingleList<T>::Length() const
{
return n;
}
template <class T>
bool SingleList<T>::IsEmpty() const
{
return n==0;
}
template<class T>
bool SingleList<T>::Find(int i,T& x)const
{ //在(a0,a1,...,an-1)中找下标为i的元素ai
if (i<0 || i>n-1){ //对i进行越界检查
cout<< "Out Of Bounds"; return false;
}
Node<T> *p=first;
for (int 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;
int j;
for ( 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"; return false;
}
Node<T> * q=new Node<T>; q->element=x; //生成新结点q
Node<T> *p=first; //从头结点开始找ai元素所在的结点p
for (int j=0; j<i; j++) p=p->link;
if(i>-1) {
q->link=p->link; //新结点q插在p之后
p->link=q;
}
else {
q->link=first; //新结点q插在头结点之前,成为新的头结点
first=q;
}
n++; return true;
}
template<class T>
bool SingleList<T>::Delete(int i)
{
if (!n) { //若n=0,则再删除就会下溢出
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; //q指向要删除的结点的前一结点
if (i==0) first=first->link; //若i=0,则删除的是头结点
else { //否则删除的是非头结点
p=q->link; //p指向要删除的结点,q是p的前驱
q->link=p->link; //从单链表中删除p结点
}
delete p; //释放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;
}
#endif
//singlelisti.h
#ifndef SINGLELISTI_H
#define SINGLELISTI_H
#include "singlelist.h"
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++;
}
}
#endif
//Intersection.cpp
#include "singlelisti.h"
int main()
{
SingleList<int> LA;
SingleList<int> LB;
for(int i=0;i<10;i++)
LA.Insert(i-1,i);
LA.Output(cout);
for(int i=0;i<6;i++)
LB.Insert(i-1,i);
LB.Output(cout);
Intersection(LA,LB);
LA.Output(cout);
return 0;
}