Chapter2 线性表
何以谓之线性表?
所谓线性表是指数据元素之间是一个对一个的关系。
算法2.1
两个线性链表LA, LB ,现在求一个新的链表LA = LA ∪ LB
eg.LA={0,1,2,3,4} LB={0,5,6,7,2,8,9,4} LA=LA∪LB={0,1,2,3,4,5,6,7,8,9}
//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
//seqlist.h
#ifndef SEQLIST_H
#define SEQLIST_H
#include "linearlist.h"
template <class T>
class SeqList:public LinearList<T>
{
public:
SeqList(int mSize);
~SeqList(){delete[] elements;}
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 Output(ostream& out) const;
private:
int maxLength;
T* elements;
};
template <class T>
SeqList<T> ::SeqList(int mSize)
{
maxLength = mSize;
elements = new T[maxLength];
n = 0;
}
template <class T>
bool SeqList<T>::Isempty()const
{
return n == 0;
}
template <class T>
int SeqList<T>::Length() const
{
return n;
}
template <class T>
bool SeqList<T>::Find(int i,T& x)const
{
if(i<0||i>n-1)
{
cout << "Out of Bounds"<< endl;
return false;
}
x = elements[i];
return true;
}
template <class T>
int SeqList<T> ::Search(T x) const
{
for(int j=0;j<n; j++)
if(elements[j]==x)
return j;
return -1;
}
template <class T>
bool SeqList<T> ::Insert(int i, T x)//i=-1 在a0前插入;在ai后插入
{
if(i<-1||i>n-1)
{
cout << "Out of Bounds"<< endl;
return false;
}
if(n==maxLength)
{
cout <<"OverFlow"<<endl;
return false;
}
for(int j=n-1;j>i;j--)
elements[j+1] = elements[j];
elements[i+1]=x;
n++;
return true;
}
template<class T>
bool SeqList<T>::Delete(int i)
{
if (!n)
{
cout << "UnderFlow"<<endl;
return false;
}
if (i<0||i>n-1)
{
cout << "Out of Bounds"<<endl;
return false;
}
for (int j=i+1;j<n;j++)
{
elements[j-1]=elements[j];
}
n--;
return true;
}
template <class T>
bool SeqList<T>::Update(int i, T x)
{
if (i<0||i>n-1)
{
cout << "Out of Bounds"<<endl;
return false;
}
elements[i] = x;
return true;
}
template <class T>
void SeqList<T>::Output(ostream& out) const
{
for (int i=0; i<n; i++)
out << elements[i]<<' ';
out << endl;
}
#endif
//seqlistu.h
#ifndef SEQLISTU_H
#define SEQLISTU_H
#include "seqlist.h"
template <class T>
void Uinon(SeqList <T> &LA, SeqList <T> LB)
{
T x;
for (int i=0; i<LB.Length();i++)
{
LB.Find(i,x); //查找小标为i的元素x
if (LA.Search(x)==-1) //如果LA中没有
{
LA.Insert(LA.Length()-1,x);//在LA的最后一个位置插入x
}
}
}
#endif
//union.cpp
#include "SeqListu.h"
const int SIZE = 20;
int main()
{
SeqList<int> LA(SIZE);
SeqList<int> LB(SIZE);
for(int i=0;i<5;i++)
LA.Insert(i-1,i); //LA(0 1 2 3 4)
for(int i=5;i<10;i++)
LB.Insert(i-6,i); //LB(5 6 7 8 9)
LB.Insert(-1,0); //LB 0 5 6 7 8 9
LB.Insert(3,2); //LB 0 5 6 7 2 8 9
LB.Insert(LB.Length()-1,4);// 0 5 6 7 2 8 9 4
Uinon(LA,LB); //0 1 2 3 4 5 6 7 8 9
LA.Output(cout);
return 0;
}