数据结构实验三:线性表综合实验

一.实验目的
 巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相

关知识来解决具体问题。

二.实验内容
1.建立一个由 n 个学生成绩的顺序表,n 的大小由自己确定,每一个学生的成绩信息由自己
确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
要求如下:
1)用顺序表来实现
2)用单链表来实现
3)用双链表实现
4)用静态链表实现

5)用间接寻址实现

实验代码如下:

1)用顺序表来实现

#include <iostream>
using namespace std;
const int N=5;
class Student{
private:
	int scores[N];
	int lenght;
public:
	Student(int score[],int n);
	~Student(){}
	void Insert(int i,int x);
	int Delete(int i);
	int Get(int i);
	int Locate(int x);
	void Show();
};
Student::Student(int score[],int n){
	if (n>N) throw "参数非法";
	for (int i=0;i<n;i++)
		scores[i]=score[i];
	lenght=n;
}
void Student::Insert(int i,int x){
	if (lenght>N) throw "上溢";
	if (i<0||i>lenght+1) throw "位置非法";
	for (int j=lenght;j>=i;j--)
		scores[j]=scores[j-1];
	scores[i-1]=x;
	lenght++;
}
int Student::Delete(int i){
	if (lenght<0) throw "下溢";
	if (i<0||i>lenght+1) throw "位置非法";
	int x=scores[i-1];
	for (int j=i-1;j<lenght-1;j++)
		scores[j]=scores[j+1];
	lenght--;
	return x;
}
int Student::Get(int i){
	return scores[i-1];
}
int Student::Locate(int x){
	int n;
	for (int i=0;i<lenght;i++)
		if (scores[i]==x)
			n=i+1;
		return n;
}
void Student::Show(){
	for (int i=0;i<lenght;i++)
		cout<<scores[i]<<' ';
	cout<<endl;
}

int main (){
	int a[3]={90,80,75};
	Student S(a,3);
	cout<<"原表:"<<endl;
	S.Show();
	cout<<"在第3位插入98:"<<endl;
	S.Insert(3,98);
	cout<<"插入后:"<<endl;
	S.Show();
	cout<<"删除第2位:"<<endl;
	S.Delete(2);
	cout<<"删除后:"<<endl;
	S.Show();
	cout<<"查75分的位置:"<<S.Locate(75)<<','<<"查第2位的成绩:"<<S.Get(2)<<endl;
	return 0;
}

2)用单链表来实现

#include <iostream>
using namespace std;
const int N=5;
struct Std{
	int date;
	Std *next;
}*p,*r;
class Student{
private:
	Std *first;
	int lenght;
public:
	Student();
	Student(int a[],int n);
	~Student(){}
	void Insert(int i,int x);
	int Delete(int i);
	int Get(int i);
	int Locate(int x);
	void Show();
};
Student::Student(){
	first =new Std;
	first->next=NULL;
}
Student::Student(int a[],int n){
	first=new Std;
	first->next=NULL;
	r=first;
	for (int i=0;i<n;i++){
		p=new Std;
		p->date=a[i];
		r->next=p;
		r=p;
	}
	r->next=NULL;
	lenght=n;
}
void Student::Insert(int i,int x){
	r=first;
	int j=0;
	while (r!=NULL&&j<i-1){
		r=r->next;
		j++;
	}
	if (r==NULL) throw "位置非法";
	else {
		p=new Std;
		p->next=r->next;
		p->date=x;
		r->next=p;
		lenght++;
	}
}
int Student::Delete(int i){
	r=first;
	int j=0;
	while (r!=NULL&&j<i-1){
		r=r->next;
		j++;
	}
	if (r==NULL) throw "位置非法";
	else {
		p=r->next;
		r->next=p->next;
		delete p;
		lenght--;
	}
	return p->date;
}
int Student::Get(int i) {
	r = first;
	int j = 0;
	while (r != NULL && j<i - 1) {
		r = r->next;
		j++;
	}
	if (r == NULL) throw "位置非法";
	else 
		p = r;
	return p->date;
}
int Student::Locate(int x) {
	r = first;
	int j=0;
	while (r != NULL) {
		r = r->next;
		j++;
		if (r->date == x) break;
	}
	if (r == NULL) throw "位置非法";
	return j;
}
void Student::Show() {
	r = first;
	while (r->next!= NULL) {
		r = r->next;
		cout << r->date << ' ';
	}
	cout<<endl;
}

int main() {
	int a[3] = { 90,80,75 };
	Student S(a, 3);
	cout << "原表:" << endl;
	S.Show();
	cout << "在第3位插入98:" << endl;
	S.Insert(3, 98);
	cout << "插入后:" << endl;
	S.Show();
	cout << "删除第2位:" << endl;
	S.Delete(2);
	cout << "删除后:" << endl;
	S.Show();
	cout << "查75分的位置:" << S.Locate(75) << ',' << "查第2位的成绩:" << S.Get(2) << endl;
	return 0;
}

3)用双链表实现

#include <iostream>
using namespace std;
const int N = 5;
struct Std {
	int date;
	Std *next,*prior;
}*p, *r;
class Student {
private:
	Std * first;
	int lenght;
public:
	Student();
	Student(int a[], int n);
	~Student() {}
	void Insert(int i, int x);
	int Delete(int i);
	int Get(int i);
	int Locate(int x);
	int Getper(int x,int i);
	void Show();
};
Student::Student() {
	first = new Std;
	first->next =first;
	first->prior = first;
}
Student::Student(int a[], int n) {
	first = new Std;
	first->next = NULL;
	r = first;
	for (int i = 0; i<n; i++) {
		p = new Std;
		p->date = a[i];
		r->next = p;
		p->prior = r;
		r = p;
	}
	r->next = first;
	first->prior = r;
	lenght = n;
}
void Student::Insert(int i, int x) {
	r = first;
	int j = 0,count = 0;
	while (count<=lenght && j<i - 1) {
		r = r->next;
		count++;
		j++;
	}
	if (count>lenght) throw "位置非法";
	else {
		p = new Std;
		p->next = r->next;
		r->next->prior = p;
		r->next = p;
		p->date = x;
		p->prior = r;
		lenght++;
	}
}
int Student::Delete(int i) {
	r = first;
	int x,j = 0,count = 0;
	while (count<=lenght && j<i - 1) {
		r = r->next;
		count++;
		j++;
	}
	if (count>lenght) throw "位置非法";
	else {
		p = r->next;
		p->next->prior = r;
		r->next = p->next;
		x = p->date;
		delete p;
	}
	return x;
}
int Student::Get(int i) {
r = first;
	int j = 0,count = 0;
	while (count<=lenght && j<i - 1) {
		r = r->next;
		count++;
		j++;
	}
	if (count>lenght) throw "位置非法";
	else
		p = r->next;
	return p->date;
}
int Student::Locate(int x) {
	r = first;
	int j = 0,count = 0;
	while (count<=lenght) {
		r = r->next;
		count++;
		j++;
		if (r->date == x) break;
	}
	if (count>lenght) cout<<"找不到该数据"<<endl;
	return j;
}
void Student::Show() {
	r = first;
	while (r->next != first) {
		r = r->next;
		cout << r->date << ' ';
	}
	cout << endl;
}
int Student::Getper(int x,int i){
	r = first;
	int j,count = 0;
	while (count<=lenght) {
		r = r->next;
		count++;
		if (r->date == x) break;
	}
	if (count>lenght) throw "找不到该数据";
	if (i>lenght) throw "前移非法";
	for (j=0;j<i;j++)
		r=r->prior;
	return r->date;
}
int main() {
	int a[3] = { 90,80,75 };
	try{
	Student S(a, 3);
	cout << "原表:" << endl;
	S.Show();
	cout << "在第3位插入98:" << endl;
	S.Insert(1, 98);
	cout << "插入后:" << endl;
	S.Show();
	cout << "删除第2位:" << endl;
	S.Delete(2);
	cout << "删除后:" << endl;
	S.Show();
	cout << "查75分的位置:" << endl;
	cout<<S.Locate(75) <<endl;
	cout<< "查第2位的成绩:" <<endl;
	cout<< S.Get(2) << endl;
	cout<< "查75分前2位的成绩:" <<endl;
	cout<< S.Getper(75,2) << endl;
	}
	catch(char *s){
		cout<<s<<endl;
	}
	return 0;
}

4)用静态链表实现

#include <iostream>
using namespace std;
const int N=100;
struct Std{
	int date;
	int next;
}score[N];
class Student{
private:
	static int first;
	static int avail;
	int lenght;
public:
	Student();
	Student(int a[],int n);
	~Student(){}
	void Insert(int i,int x);
	int Delete(int i);
	int Get(int i);
	int Locate(int x);
	void Show();
};
int Student::first=0;
int Student::avail=1;
Student::Student(){
	score[first].next=-1;
	for (int i=avail;i<N-1;i++)
		score[i].next=i+1;
	score[i].next=-1;
}
Student::Student(int a[],int n){
	int s;cout<<first;
	score[first].next=avail;
	for (int i=0;i<n;i++){
		s=avail;
		avail=score[avail].next;
		score[s].date=a[i];
		score[s].next=avail;
	}
	lenght=n;
}
void Student::Insert(int i,int x){
	if (i>lenght) throw "位置非法";
	int s;
	s=avail;
	avail=score[avail].next;
	score[s].date=x;
	score[s].next=score[i-1].next;
	score[i-1].next=s;
	lenght++;
}
int Student::Delete(int i){
	if (i>lenght) throw "位置非法";
	int r=first;
	while (score[r].next<i)
	r=score[r].next;
	score[r].next=score[i].next;
	score[i].next=avail;
	avail=i;
	lenght--;
	return score[i].date;
}
int Student::Get(int i) {
	if (i>lenght) throw "位置非法";
	int r=first;
	while (r<i)
	r=score[r].next;
	return score[r].date;
}
int Student::Locate(int x) {
	int r=first;
	while (score[r].date!=x)
	r=score[r].next;
	return r;
}
void Student::Show() {
	int r=first;
	for (int j=0;j<lenght;j++){
		r=score[r].next;
		cout<<score[r].date<<' ';}
	cout<<endl;
}

int main() {
	Student Ava;
	int a[3] = { 90,80,75 };
	Student S(a, 3);
	cout << "原表:" << endl;
	S.Show();
	cout << "在第3位插入98:" << endl;
	S.Insert(3, 98);
	cout << "插入后:" << endl;
	S.Show();
	cout << "删除第2位:" << endl;
	S.Delete(2);
	cout << "删除后:" << endl;
	S.Show();
	cout << "查75分的位置:" << S.Locate(75) << ',' << "查第2位的成绩:" << S.Get(2) << endl;
	return 0;
}

5)用间接寻址实现

#include <iostream>
using namespace std;
const int N=5;
class Student{
private:
	int *scores[N];
	int lenght;
public:
	Student(int score[],int n);
	~Student(){}
	void Insert(int i,int *x);
	int Delete(int i);
	int Get(int i);
	int Locate(int x);
	void Show();
};
Student::Student(int score[],int n){
	if (n>N) throw "参数非法";
	for (int i=0;i<n;i++)
		scores[i]=&score[i];
	lenght=n;
}
void Student::Insert(int i,int *x){
	if (lenght>N) throw "上溢";
	if (i<0||i>lenght+1) throw "位置非法";
	for (int j=lenght;j>=i;j--)
		scores[j]=scores[j-1];
	scores[i-1]=x;
	lenght++;
}
int Student::Delete(int i){
	if (lenght<0) throw "下溢";
	if (i<0||i>lenght+1) throw "位置非法";
	int x=*scores[i-1];
	for (int j=i-1;j<lenght-1;j++)
		scores[j]=scores[j+1];
	lenght--;
	return x;
}
int Student::Get(int i){
	return *scores[i-1];
}
int Student::Locate(int x){
	int n;
	for (int i=0;i<lenght;i++)
		if (*scores[i]==x)
			n=i+1;
		return n;
}
void Student::Show(){
	for (int i=0;i<lenght;i++)
		cout<<*scores[i]<<' ';
	cout<<endl;
}

int main (){
	int a[3]={90,80,75};
	Student S(a,3);
	cout<<"原表:"<<endl;
	S.Show();
	cout<<"在第3位插入98:"<<endl;
	int x=98;
	S.Insert(3,&x);
	cout<<"插入后:"<<endl;
	S.Show();
	cout<<"删除第2位:"<<endl;
	S.Delete(2);
	cout<<"删除后:"<<endl;
	S.Show();
	cout<<"查75分的位置:"<<S.Locate(75)<<','<<"查第2位的成绩:"<<S.Get(2)<<endl;
	return 0;
}

截图:

1)2)4)5)


3)双链表


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值