实现顺序表存储的插入、删除和查找等操作。
实现链式存储的建链、插入、删除和查找等操作。
List.h
<pre name="code" class="cpp">template<class T>
struct LinkNode{
T data;
LinkNode<T> * link;
LinkNode(){link=0;}
LinkNode(T item,LinkNode<T> *ptr=0)
{data=item;link=ptr;}
bool operator==(T x){return data==x;}
bool operator!=(T x){return data!=x;}
};
template<class T>
class List{
//单链表类定义,不用继承也可以实现
protected:
LinkNode<T>*first;//表头
public:
List(){first=0;}
List(List<T>&L);
~List();
void HLinkList(int n);
void RLinkList1(int n);
void RLinkList2(int n);
void makeEmpty();
int Length()const;
LinkNode<T>*Search(T x);
LinkNode<T>*Locate(int i);
T * getData(int i);
void setData(int i,T x);
bool Insert(int i,T x);
bool Remove(int i, T&x);
bool IsEmpty()const
{return first->link==0?true:false;}
LinkNode<T>*getFirst()const{return first;}
void setFirst(LinkNode<T>*f){first=f;}
void Sort();
void Print();
};
template<class T>
void List<T>::HLinkList(int n)
{
LinkNode<T>*p;
first=0;
for(int i=0;i<n;i++)
{
p=new LinkNode<T>();
cin>>p->data;
p->link=first;
first=p;
}
}
template<class T>
void List<T>::RLinkList1(int n)
{
LinkNode<T> *tail,*p;
first=0;
for(int i=0;i<n;i++)
{
p=new LinkNode<T>();
cin>>p->data;
p->link=0;
if(first==0)
first=p;
else
tail->link=p;
tail=p;
}
}
template<class T>
void List<T>::RLinkList2(int n)
{
LinkNode<T> *tail,*p;
first=tail=new LinkNode<T>();
for(int i=0;i<n;i++)
{
p=new LinkNode<T>();
cin>>p->data;
p->link=0;
tail->link=p;
tail=p;
}
first=first->link;
}
template<class T>
void List<T>::Print()
{
LinkNode<T> *p=first;
while(p)
{
cout<<p->data<<" ";
p=p->link;
}
cout<<endl;
}
template<class T>
int List<T>::Length()const
{
LinkNode<T> *p=first;
int count=0;
while(p)
{
count++;
p=p->link;
}
return count;
}
template<class T>
LinkNode<T>*List<T>::Locate(int i)
{
if(i<=0)return 0;
LinkNode<T>*p=first;
while(p&&--i)
p=p->link;
return p;
}
template<class T>
LinkNode<T>*List<T>::Search(T x)
{
LinkNode<T>*p=first;
while(p&&p->data!=x)
p=p->link;
return p;
}
template<class T>
bool List<T>::Insert(int i,T x)
{
LinkNode<T>*current=Locate(i);
if(current==0)return false;
LinkNode<T>*p=new LinkNode<T>(x);
p->link=current->link;
current->link=p;
return true;
}
template<class T>
bool List<T>::Remove(int i,T&x)
{
LinkNode<T>*p;
if(i==1&&first)
{
p=first;
x=p->data;
first=first->link;
delete p;
return true;
}
LinkNode<T>*current=Locate(i-1);
if(current==0||current->link==0)
return false;
p=current->link;
x=p->data;
current->link=current->link->link;
delete p;
return true;
}
template<class T>
List<T>::~List()
{
LinkNode<T>*p;
while(first)
{
p=first;
first=first->link;
delete p;
}
}
SeqList.h
<pre name="code" class="cpp">#include"LinearList.h"
const int Size=100;
template<class T>
class SeqList//:public LinearList<T>
{
protected:
T*data;//存放数组
int maxSize;//最大可容纳表项的项数
int last;//数组中最后一个元素的下标
void reSize(int newSize);//改变数组的空间大小
public:
//SeqList();//构造函数
SeqList(int sz);//构造函数
SeqList(SeqList<T>&l);//复制构造函数
~SeqList(){delete []data;}//析构函数
int Size()const{return maxSize;}//求表的最大容量
int Length(){return last+1;}//计算表长度
int Search(T&x)const;//搜索x在表中位置,函数返回表项序号
int Locate(int i)const;//定位第i个表项,函数返回表项序号
T getData(int i){return data[i];}//取第i个元素
bool Insert(int i,T&X);//插入
bool Remove(int i,T&x);//删除
bool IsEmpty()const{return last==-1?true:false;}//判表空
bool IsFull(){return Length()==maxSize?true:false;}//判表满
void Sort();//排序
void input();//输入
void output();
};
template<class T>
SeqList<T>::SeqList(int sz){
if(sz>0)
{
last=-1;
maxSize=sz;
data=new T[maxSize];
if(data==0)
{cerr<<"存储分配错误!"<<endl;exit(1);}
for(last=0;last<maxSize/2;last++)
data[last]=last;
last--;
}
}
template<class T>
SeqList<T>::SeqList(SeqList<T>&l)
{
T value;
maxSize=l.maxSize;last=l.Length();
data=new T[maxSize];
if(data==0)
{cerr<<"存储分配错误!"<<endl;exit(1);}
for(int i=0;i<=last;i++)
{l.getData(i-1,value);data[i]=value;}
}
template<class T>
int SeqList<T>::Search(T&x)const
{
for(int i=0;i<last+1;i++)
if(data[i]==x) return i+1;
return 0;
}
template<class T>
bool SeqList<T>::Insert(int i,T&x)
{
if(IsFull())return false;
if(i<1||i>Length())return false;
for(int j=++last;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
return true;
}
template<class T>
bool SeqList<T>::Remove(int i,T&x)
{
if(IsEmpty())return false;
if(i<=0||i>Length())return false;
x=data[i-1];
for(int j=i;j<=last;j++)
data[j-1]=data[j];
last--;
return true;
}
void Union(SeqList<int>&la,SeqList<int>&lb)
{
int n1=la.Length(),n2=lb.Length();
int i,value;
for(i=0;i<n2;i++)
{
value=lb.getData(i);
if(la.Search(value)==0)
{la.Insert(n1,value);n1++;}
}
}
void Intersection(SeqList<int>&la,SeqList<int>&lb)
{
int n1=la.Length();
int i=0,value;
while(i<n1)
{
value=la.getData(i);
if(lb.Search(value)==0)
{la.Remove(i+1,value);n1--;}
else i++;
}
}
template<class T>
void SeqList<T>::output()
{
for(int i=0;i<=last;i++)
cout<<data[i]<<" ";
}
LinearList.cpp
<pre name="code" class="cpp">#include"SeqList.h"
#include"List.h"
#include<iostream>
using namespace std;
int main()
{
int select;
bool flag=true;;
SeqList<int>l(50);
SeqList<int>lb(75);
List<int>list;
LinkNode<int>*p;
int value;
while(flag)
{
cout<<"0.Seq_quit"<<endl;
cout<<"1.Seq_output"<<endl;
cout<<"2.Seq_search"<<endl;
cout<<"3.Seq_insert"<<endl;
cout<<"4.Seq_remove"<<endl;
cout<<"5.Seq_union"<<endl;
cout<<"6.Seq_intersection"<<endl;
cout<<"7.List_HLinkList"<<endl;
cout<<"8.List_RLinkList1"<<endl;
cout<<"9.List_RLinkList2"<<endl;
cout<<"10.List_Length"<<endl;
cout<<"11.List_Locate"<<endl;
cout<<"12.List_Search"<<endl;
cout<<"13.List_Insert"<<endl;
cout<<"14.List_Remove"<<endl;
cout<<"Please choose:"<<endl;
cin>>select;
switch(select)
{
case 1:l.output();cout<<endl;break;
case 2:
cout<<"Please input the value:";
cin>>value;
if(l.Search(value))
cout<<"the position is "<<l.Search(value)<<endl;
else cout<<"it is not find"<<endl;break;
case 3:
cout<<"Please input the position:";
cin>>select;
cout<<"Please input the value:";
cin>>value;
if(l.Insert(select,value))
cout<<"succeed!"<<endl;
else cout<<"false"<<endl;break;
case 4:
cout<<"Please input the position:";
cin>>select;
if(l.Remove(select,value))
cout<<"succeed,The delete number is "<<value<<endl;
else cout<<"false"<<endl;break;
case 5:
Union(l,lb);break;
case 6:
Intersection(lb,l);lb.output();break;
case 7:
cout<<"Please input the number:"<<endl;
cin>>select;
list.HLinkList(select);
list.Print();break;
case 8:
cout<<"Please input the number:"<<endl;
cin>>select;
list.RLinkList1(select);
list.Print();break;
case 9:
cout<<"Please input the number:"<<endl;
cin>>select;
list.RLinkList2(select);
list.Print();break;
case 10:
cout<<"The List's Length is "<<list.Length()<<endl;
break;
case 11:
cout<<"Please input the position:"<<endl;
cin>>select;
p=list.Locate(select);
if(p)
cout<<p->data<<endl;
else cout<<"NULL"<<endl;break;
case 12:
cout<<"Please input the value:"<<endl;
cin>>value;
p=list.Search(value);
if(p)
cout<<"find out!"<<endl;
else cout<<"NULL"<<endl;break;
case 13:
cout<<"Please input the position:"<<endl;
cin>>select;
cout<<"Please input the value:"<<endl;
cin>>value;
if(list.Insert(select,value))
cout<<"true"<<endl;
else cout<<"false"<<endl;
list.Print();break;
case 14:
cout<<"Please input the position:"<<endl;
cin>>select;
if(list.Remove(select,value))
cout<<value<<endl;
else cout<<"False"<<endl;break;
case 0:
flag=false;break;
}
}
return 0;
}