南邮数据结构实验1.2 单链表的操作

内容和提示:

类似1.1


#include <iostream>
using namespace std;
const int SIZE=20 ;


template <class T>
class LinearList
{
protected:
int n; 


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)=0;
virtual bool Delete(int i)=0;
virtual bool Update(int i,T x)=0;
virtual void Output(ostream &out) const=0;
};


template <class T> class 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;
void Reserve();                 //链接表逆置
bool DeleteX(const T &x);       //删除所有元素


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
{
if (i<0 || i>n - 1)
{
cout << "out of bounds"; 
return false;
}
Node<T> *p = first;
for (int j = 0; j < i - 1; j++)
p = p->link;
x = p->element;
return true;
}


template<class T>
int SingleList<T>::Search(T x)const
{
int j;
Node<T> *p = first;
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;
Node<T>*p = first;
for (int 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 SingleList<T>::Reserve()          //链接表逆置
{ 
for (int i = 0; i <n -1;i++)
{
Insert(n - 1-i, first->element);
Delete(0);
}
}


template<class T>
bool SingleList<T>::DeleteX(const T &x)     
{
if (Search(x) < 0)
return false;
else
{
while (Search(x)>=0)
Delete(Search(x));
}
return true;
}


void main()
{
SingleList <int> LB;
int x,n,a;
cout<<"Please input the length:"<<endl;
cin>>n;
cout<<"Please input the SingleList:"<<endl;
for(int  i=0;i<n;i++)
{ 
cin>>x;
LB.Insert(i-1,x);
}
LB.Output(cout);


cout<<"Please input x to be deleted:"<<endl;
cin>>a;
LB.DeleteX(a);
cout<<"After delete:"<<endl;
LB.Output(cout);


LB.Reserve();
cout<<"After reserved:"<<endl;
LB.Output(cout);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值