数据结构复习之 顺序表的C++实现

这部分学的时候由于自己刚转专业 确实也没什么编程基础 几个实验做起来也是稀里糊涂 虽然原理课上都听懂了 具体实现的时候还是一脸懵 所以正好自己复习数据结构 就来再实现一遍

这次先实现顺序表,实现的具体功能包括:头插 头删 插入具体位置 删除具体位置元素 输出所有元素 按值搜索 按位置搜索

由于之前做的就是基于模板来实现的 这里也这样实现 实话实说现在看来当时学的东西真的没有那么难

代码具体实现

#include <iostream>
using namespace std;
/*设定顺序表最长长度100*/
const int MaxSize = 100;
//顺序表类 
template<class ElemType>
class SqList
{
	public:
		SqList(){};
		SqList(ElemType a[],int len);
		~SqList(){};
		int GetLength(){return length;};
		//获取位置i的元素 
		ElemType GetXElem(int i);
		//指定位置插入 越界:<0 头插 >length  尾插 
		void InsertElem(ElemType x,int pos);
		//头插 
		void HeadInsert(ElemType x);
		//头删 
		void HeadDelete();
		// 删除某位置元素 
		void DeleteElem(int pos);
		//输出所有元素 空格分开
		int LocateElem(int x);
		void PrintElem(); 
	private:
		ElemType data[MaxSize];
		int length;
};
//带参构造函数
template <class ElemType>
SqList<ElemType>::SqList(ElemType a[],int len)
{
	if(len>MaxSize)
		cout<<"too many elems"<<endl;
	else
	{
		for(int i=0;i<len;i++)
			data[i] = a[i];
		length = len;
	}
} 
//获取某位置元素
template <class ElemType>
ElemType SqList<ElemType>::GetXElem(int pos)
{
	if(pos<0 || pos>=length)
		cout<<"Position Error"<<endl;//return;
	return data[pos];
} 
//插入某位置元素
template <class ElemType>
void SqList<ElemType>::InsertElem(ElemType x,int pos) 
{
	if(pos<0)
	{
		cout<<"position<0 ,Head Insert"<<endl;
		for(int i=length-1;i>=0;i--)
		{
			data[i+1] = data[i];
		}
		data[0] = x;
		length++;
	}
	else if(pos>=length)
	{
		cout<<"position>length-1,Tail Insert"<<endl;
		data[length] = x;
		length++;
	}
	else
	{
		cout<<"Insert Ok"<<endl;
		for(int i=length-1;i>=pos;i--)
		{
			data[i+1] = data[i];
		}
		data[pos] = x;
		length++;
	}
}
//头插
template<class ElemType>
void SqList<ElemType>::HeadInsert(ElemType x)
{
	for(int i = length-1;i>=0;i--)
		data[i+1] = data[i];
	data[0] = x;
	length++;
 } 
//头删
template<class ElemType>
void SqList<ElemType>::HeadDelete()
{
	for(int i=1;i<length;i++)
		data[i-1] = data[i];
	length--;
} 
//删除某位置
template <class ElemType>
void SqList<ElemType>::DeleteElem(int pos)
{
	for(int i=pos;i<length-1;i++)
		data[i] = data[i+1];
	length--;
} 
//输出
template<class ElemType>
void SqList<ElemType>::PrintElem()
{
	for(int i=0;i<length;i++)
		cout<<data[i]<<" ";
	cout<<endl;
 } 
template<class ElemType>
int SqList<ElemType>::LocateElem(int x)
{
	int pos;
	for(int i=0;i<length;i++)
	{
		if(data[i]==x)
		{
			pos=i;
			return pos;
		}
	}
	cout<<"no found"<<endl;
}

int main()
{
	int a[10] = {1,2,3,4,5,6,7,8,9,10};
	SqList<int>sq = SqList<int>(a,10);
	sq.PrintElem();
	cout<<"Please Input Your Choice"<<endl;
	cout<<"1:Head Insert      2:Head Delete"<<endl;
	cout<<"3:Insert Elem      4:Delete Elem"<<endl;
	cout<<"5:Get x elem       6:Locate Elem"<<endl;
	int c;
	while(cin>>c && c<=6)
	{
		if(c == 1)
		{
			int x;
			cout<<"Please input elem"<<endl;
			cin>>x;
			sq.HeadInsert(x);
			sq.PrintElem();
		}
		if(c==2)
		{
			sq.HeadDelete();
			sq.PrintElem();
		}
		if(c==3)
		{
			int x,pos;
			cout<<"please input elem and position"<<endl;
			cin>>x>>pos;
			sq.InsertElem(x,pos);
			sq.PrintElem();
		}
		if(c==4)
		{
			int pos;
			cout<<"please input the position to delete"<<endl;
			cin>>pos;
			sq.DeleteElem(pos);
			sq.PrintElem();
		}
		if(c==5)
		{
			int pos;
			cout<<"please input the position to search"<<endl;
			cin>>pos;
			cout<<sq.GetXElem(pos)<<endl;
			sq.PrintElem();
		}
		if(c==6)
		{
			int x;
			cin>>x;
			cout<<sq.LocateElem(x);
		}
	}
	return 0;
}

实验效果

发布了63 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/LieberVater/article/details/95920234