# 线性表的基本运算

51 篇文章 0 订阅

Linearlist.h：

#include "iostream.h"

template <class T>
class Linearlist
{
protected:
int n;     //线性表长度
public:
virtual bool Find(int i,T& x)=0;
virtual int Search(T x)=0;
virtual bool Insert(int i,T x)=0;
virtual bool Delete(int i)=0;
};

Node.h：

#include "iostream.h"

template <class T>
class Node
{
private:
T element;
};

SeqList.h:

#include "Linearlist.h"

template <class T>
class SeqList:public Linearlist<T>
{
public:
SeqList(int msize);
~SeqList();
bool Find(int i,T& x) ;
int Search(T x) ;
bool Insert(int i,T x);
bool Delete(int i);
private:
int maxLength;
T *elements;
};

template <class T>
SeqList<T>::SeqList(int msize)
{
maxLength=msize;
elements=new T[maxLength];
n=0;
}

template <class T>
SeqList<T>::~SeqList()
{
delete []elements;
}

template <class T>
bool SeqList<T>::Find(int i,T &x)
{
if(i<0 || i>=n)
{
cout<<"out of bounds"<<endl;
return false;
}
else
{
if(x==elements[i])
{return true;}
else
{return false;}

}
}

template <class T>
int SeqList<T>::Search(T x)
{
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)
{
if(i<0 || i>n)
{
cout<<"out of bounds"<<endl;
return false;
}
if(n==maxLength)
{
cout<<"overflow"<<endl;
}
for(int j=n-1;j>i;j--)
{
elements[j]=elements[j-1];
}
elements[i]=x;
n++;
return true;
}
template <class T>
bool SeqList<T>::Delete(int i)
{
if(i<0 || i>=n)
{
cout<<"out of bounds"<<endl;
return false;
}
if(!n)
{
cout<<"overflow"<<endl;
}
for(int j=i;j<n-1;j++)
{
elements[j]=elements[j+1];
}
n--;
return true;
}

SingelList:

#include "Linearlist.h"
#include "Node.h"
template <class T>
class SingelList:public Linearlist<T>
{
private:
Node<T> *first;
public:
SingelList();
~SingelList();
bool Find(int i,T& x);
int Search(T x);
bool Insert(int i,T x);
bool Delete(int i);

};

template <class T>
SingelList<T>::SingelList()
{
first=NULL;
n=0;
}

template <class T>
SingelList<T>::~SingelList()
{
Node<T> *p;
while(first)
{
delete first;
first=p;
}
}

template <class T>
bool SingelList<T>::Find(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<n-1;j++)
{
x=p->element;
}
return true;
}
template <class T>
int SingelList<T>::Search(T x)
{
Node<T> *p=first;
for(int j=0;p&&p->element!=x;j++)
{
}
if(p)
{
return j;
}
return -1;
}

template <class T>
bool SingelList<T>::Insert(int i,T x)
{
if(i<-1 || i>n-1)
{
cout<<"out of bounds"<<endl;
return false;
}
Node<T> *q=new Node;
q->element=x;

Node<T> *p=first;

for(int j=0;j<i;j++)
{
}
if(i!=-1)
{
}
else
{
first=q;
}
n++;
return true;
}
template <class T>
bool SingelList<T>::Delete(int i)
{
if(i<0 || i>n-1)
{
cout<<"out of bounds"<<endl;
return false;
}
if(!n)
{
cout<<"overflow"<<endl;
return false;
}

Node<T> *p=first;
for(int j=0;j<i-1;j++)
{
}
if(i!=0)
{
}
else
{
}

delete p;
n--;
return true;
}

Main.cpp:

#include "iostream.h"
#include "SeqList.h"

void main()
{
SeqList <int> se1(5);
int k,x;
for(int i=0;i<5;i++)
{
cin>>k;
se1.Insert(i,k);
}
cin>>i>>x;
cout<<"se1.find(i,x): "<<se1.Find(i,x)<<endl;
cout<<"Here end OK."<<endl;
}

• 0
点赞
• 2
收藏
• 打赏
• 0
评论
10-10 3154
05-26 3283
05-15 2626
04-12 683
11-23 1463
02-09 2851
12-22 459
03-04 1276
12-08 2万+
04-25 2427

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。