本文主要是介绍南邮数据结构实验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);
}
这篇关于南邮数据结构实验1.2 单链表的操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!