实验二 线性表 综合实验(c++)

5、间接寻址


实验目的

     巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。

 

 实验内容

1建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。



#include<iostream>
using namespace std;
template<class T>
class LinearList
{
private:
	T **Data;
	int Length;
	int Size;
public:
	LinearList();
	LinearList(int m);
	LinearList(T a[], int n);
	~LinearList();
	int GetLength();
	int GetSize();
	T GetPos(int pos);
	void Insert(T m, int n);
	T Delete(int a);
	int Locate(T k);
	void PrintList();
	T SetPO(int p,T O);
	void SetLength(int l);
	void RevListSeg(int f, int t);
	void LeftRotate(int k);
};
template<class T>
LinearList<T>::LinearList()
{
	Length=0;
	Size=5;
	Data=new T*[5];
}
template<class T>
LinearList<T>::LinearList(int m)
{
	Length=0;
	Size=10;
	Data=new T*[m];
}
template<class T>
LinearList<T>::LinearList(T a[],int n)
{
	this->Length=n;
	this->Size=n;
	this->Data=new T*[n];
	if (this->Data==nullptr)
	{
		throw "LinearList (T a[] , int n) trapped!\n";
	}

	for (int i =0;i<n;i++)
	{
		this->Data[i]=new T;
		*(this->Data[i])=a[i];
	}
}
template<class T>
LinearList<T>::~LinearList()
{
	for (int i=0;i< Length;i++)
	{
		delete [] (this->Data[i]);
	}
	delete [] this->Data;
	Length=0;
}
template<class T>
int LinearList<T>::GetLength()
{
	return this->Length;
}
template<class T>
int LinearList<T>::GetSize()
{
	return Size;
}
template<class T>
T LinearList<T>::GetPos(int pos)
{
	return *(this->Data[pos]);
}
template<class T>
void LinearList<T>::Insert(T m, int n)
{
	if (n>Length+1||n<1)
	{
		throw"插入位置有误1";
		return;
	}
	this->Length++;
	if(Length > Size)
	{
		Size += 10;
		this->Data = (T **)realloc(this->Data, Size * sizeof(T));
		if (this->Data==nullptr)
		{
			throw "插入位置有误!";
		}
	}
	T * temp=this->Data[Length-1] =new T;
	*temp =m;
	for (int i=Length-1;i>=n;i--)
	{
		this->Data[i]=this->Data[i-1];
	}
	this->Data[n-1] =temp;
}
template<class T>
T LinearList<T>::Delete(int a)
{
	if (a<1||a>this->Length)
	{
		throw"删除有误!";
	}
	T temp=*(this->Data[a-1]);
	T *p=this->Data[a-1];
	for (int i=a-1;i<Length-1;i++)
	{
		this->Data[i]=this->Data[i+1];
	}
	delete p;
	this->Data[Length-1]=nullptr;
	Length--;
	return temp;
}
template<class T>
int LinearList<T>::Locate(T k)
{
	int pos=-1;
	for(int i=0;i<Length;i++)
	{
		if (*(this->Data[i])==k)
		{
			pos=i+1;
			return pos;
		}
	}
	return pos;
}
template<class T>
void LinearList<T>::PrintList()
{
	for (int i =0;i<this->Length;i++)
	{
	cout<<*(this->Data[i])<<" ";
	}
	cout<<endl;
}
template<class T>
T LinearList<T>::SetPO(int p, T O)
{
	if (p<1||p>this-Length+1)
	{
		throw "删除有误!";
	}
	if (p==Length+1)
	{
		Length++;
		this->Data[Length-1]=new T;
		*(this->Data[p-1])=O;
	}
	*(this->Data[p-1])=O;
	return T();
}
template<class T>
void LinearList<T>::SetLength(int l)
{
	this->Length =l;
}
template<class T>
void LinearList<T>::RevListSeg(int f,int t)
{
	if (f<1||f>Length||t<1||t>Length)
	{
		throw "异常!";
	}
	T *temp;
	for (int i=0;i<=(t-f)/2;i++)
	{
		temp=Data[f-1+i];
		Data[f-1+i]=Data[t-1-i];
		Data[t-1-i] = temp;
	}
}
template<class T>
void LinearList<T>::LeftRotate(int k)
{
	RevListSeg(1,k);
	RevListSeg(k+1,Length);
	RevListSeg(1,Length);
}
int main()  
{  
    int score[5]={78,100,99,88,93};  
    LinearList<int> a(score, 5);  
    cout<<"学生成绩的顺序表为:"<<endl;  
    a.PrintList();  
	cout<<endl;
    cout<<"在第1个位置插入91"<<endl;  
    a.Insert(91, 1);  
    a.PrintList(); 
	cout<<endl;
    cout<< "在第4个位置插入84"<<endl;  
    a.Insert(84,4);  
    a.PrintList();
	cout<<endl;
    cout<<"查找数据88所在的位置:"<<a.Locate(88) <<endl;  
	cout<<endl;
    cout<<"删除数据99后输出:"<<endl;  
    a.Delete(a.Locate(99)); 
    a.PrintList();    
    cout<<endl;
    cout<<"倒置2-4位后:";  
    a.RevListSeg(2, 4);  
    a.PrintList();
	cout<<endl;
    cout<<"左移3位后:";  
    a.LeftRotate(3);  
    a.PrintList();  
    cout<<endl;
    return 0;  
} 









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值