加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

《数据结构》实验二: 线性表实验

发布时间:2020-12-15 05:51:56 所属栏目:安全 来源:网络整理
导读:一..实验目的 ???? 巩固线性表的数据结构,学会线性表的应用。 1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。 2.学习运用线性表的知识来解决实际问题。 3.进一步巩固程序调试方法。 4.进一步巩固模板程序设计。 二.实验时间 ?? 准备时间为第2

一..实验目的

???? 巩固线性表的数据结构,学会线性表的应用。

1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。

2.学习运用线性表的知识来解决实际问题。

3.进一步巩固程序调试方法。

4.进一步巩固模板程序设计。

二.实验时间

?? 准备时间为第2周到第4周,具体集中实验时间为第4周第2次课。2个学时。

三..实验内容

1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。

要求如下:

1)用顺序表来实现。

/*   用顺序表来实现    */

#include<iostream>
using namespace std;

const int Max=700;
template<class TT>
class Score
{
	TT score[Max];
	int sum;
public:
	Score(){sum=0;}           //建立一个空的顺序表
	Score(TT a[],int n);     //建立一个长度为N的顺序表(析构函数省略)
	void insert(int i,TT x);   //插入函数
	int locate( TT x);         //按分数查找
	TT get(int i);             //按学号查找
	TT Delete(int i);          //删除函数
};

template<class TT>
Score<TT>::Score(TT a[],int n)
{
	int i;
	if (n>Max)throw"参数非法";
	for(i=0;i<n;i++)
		score[i]=a[i];
	sum=n;
}
template<class TT>
void Score<TT>::insert(int i,TT x)
{
	int j;
	if(sum>=Max)throw"上溢";
	if(i<1||i>sum+1)throw"位置";
	for (j=sum;j>=i;j--)
		score[j]=score[j-1];
	score[i-1]=x;
	sum++;
}

template<class TT>
int Score<TT>::locate(TT x)
{
	int i;
	for(i=0;i<sum;i++)
		if(score[i]==x)return i+1; //返回第i+1位同学
	return 0;//找不到,退出循环
}

template<class TT>
TT Score<TT>::get(int i)
{
	if(i<1 && i>sum)throw "查找位置非法";
	else return score[i-1];
}

template<class TT>
TT Score<TT>::Delete(int i)
{
	TT x;
	int j;
	if(sum==0)throw"下溢";
	if(i<1||i>sum)throw"位置";
	x=score[i-1];
	for(j=i;j<sum;j++)
		score[j-1]=score[j];
	sum--;
	return x;
}

int main()
{
	int a[Max];
	Score<int> s(a,2);
	s.insert(1,98);
	s.get(1);
	s.locate(98);
	s.Delete(1);
}

对于第一个程序,还算简单,就是在实例化的地方卡了一下。后来找了挺多资料,一次次地尝试,一次次愤怒想要砸电脑到恢复平静继续编译,改错。终于在老师要求提交作业的截止日期的之后第三周,我终于搞明白了实例化,不过程序中的实例化,我感觉还是表达有点问题。希望老师能再指点一下。

算法图,以及截图就不上传了,因为我不会画。。。又没有输出,所以结果就一个空的dos窗口。

2)用单链表来实现。

/* 用单链表来实现 */
#include<iostream>
using namespace std;

const int Max=70;
template<class TT>
struct Node
{
	TT score;
	Node<TT> *next;
};
template<class TT>
class Score
{
	Node<TT> *first;
public:
	Score();                   //建立一个空的单链表
	Score(TT a[],int n);  //建立一个有N个元素的单链表(析构函数省略)
	//~Score();
	void insert(int i,TT x);   //插入函数
	int locate( TT x);         //按分数查找
	TT get(int i);             //按学号查找
	TT Delete(int i);          //删除函数
};

template<class TT>
Score<TT>::Score()
{
	first=new Node<TT>;
	first->next= NULL;
}

template<class TT>
Score<TT>::Score(TT a[],int n)
{
	int i;
	Node<TT> *s;
	first=new Node<TT>;
	first->next=NULL;
	for(i=0;i<n;i++){
		s=new Node<TT>;
		s->score=a[i];
		s->next=first->next;
		first->next=s;
	}
}

/*
template<class TT>
Score<TT>::Score(TT a[],int n)                  //这个是尾插法
{
	Node<TT> *s,*r;
	first=new Node;
	r=first;
	for(i=0;i<n;i++)
	{
		s=new Node; 
		s->score=a[i];
		r->next=s;
		r=s;
	}
	r-next=NULL;
}
*/
template<class TT>
void Score<TT>::insert(int i,TT x)
{
	Node<TT> *p,*s;        
	p=first;                       //从头结点开始       
	int count=0;
	for (count=0;p!=NULL && count<i-1;count++)         //查找第i-1个节点
		p=p->next;
	if(p==NULL)throw "位置";                      //没找到
	else{                                         //找到了,插入新节点
		s=new Node<TT>;
		s->score=x;
		s->next=p->next;
		p->next=s;
	}
}

template<class TT>
int Score<TT>::locate(TT x)
{
	Node<TT> *p;
	p=first->next;
	int count;
	for(count=1;p!=NULL;count++)
		if(p->score==x)return count; //返回第i+1位同学
	return 0;//找不到,退出循环
}

template<class TT>
TT Score<TT>::get(int i)
{
	Node<TT> *p;
	p=first->next;
	int count;
	for(count=1;p!=NULL;count++)
		p=p->next;
		if(p==NULL)throw "位置";
		else return p->score;         //找到了,返回这位同学
	
}

template<class TT>
TT Score<TT>::Delete(int i)
{
	Node<TT> *p,*q;
	TT x;
	p=first;
	int count=0;
	for(count=0;p!=NULL && count<i-1;count++)
		p=p->next;
	if(p==NULL||p->next==NULL) //节点p不存在,或者p的后继节点不存在
		throw "位置";
	else{
		q=p->next;x=q->score;
		p->next=q->next;
		delete q;
		return x;
}
}

int main()
{

	int a[Max]={0,2};
	Score<int> s(a,4);
	s.insert(1,98);
	s.get(1);
	s.locate(98);
	s.Delete(1);
        return 0;
}

对于这一个,我只想哭,电脑有点问题+编译器有点问题=我现在都有问题。

上面的代码编译器还是不让过的,说静态堆栈溢出。我也不知道怎么回事!

2.解决约瑟夫问题

? ??设有编号为1,2,3,n的n(n>0)个人围在一起,每人持有一个密码m,从第一个人开始报数,报到m时停止报数,报m的人出圈,再从下一个人开始重新报数,报到m时停止报数,报m的人出圈,……直到的所有人出圈为止。当给定n和m后,输出出圈的次序。

要求如下:自定义数据结构,确定存储方法,并设计算法。在主程序中输入n和m后,输出结果。

/* 约瑟夫问题 */

#include<iostream>
using namespace std;
const int MAX=5;
template<class TT>
struct Node
{
	TT num;
	Node<TT> *next;
};
template<class TT>
class Josephus
{
	TT n,m;
	Node<TT> *rear;
public:
	Josephus();
	Josephus(TT a[],int n);
	~Josephus(){};
	void kill(int m);
};

template<class TT>
Josephus<TT>::Josephus(){
	rear = new Node<TT>;
	rear->next=rear;
}

template<class TT>
Josephus<TT>::Josephus(TT a[],int n){
	rear = new Node<TT>;
	rear->next=rear;
	int i;
	rear->num=a[1];
	Node<TT> *s;
	//Josephus<TT> *s;
	for(i=2;i<=n;i++){
		s = new Node<TT>;
		s->num=a[i];
		s->next=rear->next;
		rear->next=s;
		rear=s;
	}
}

template<class TT>
void Josephus<TT>::kill(int m){
	Node<TT> *p,*q;
	int j;
	p=rear->next;
	//for(i=1;;i++)
		for(j=1;;){
			if(j+1==m){
				q=p->next;
				cout<<q->num;
				p->next=q->next;
				delete q;
				j=1;
			}
			else j++;
			p=p->next;
			if(p->next==p){
				cout<<p->num;
				break;
			}
		}
}

int main()
{
	int a[MAX],i;
	for(i=1;i<=MAX;i++)
		a[i]=i;
	Josephus<int> j(a,5);
	j.kill(2);
	return 0;
}
编译结果:


这一个,不解释,编译器问题,差点把电脑砸了。

3.实现两个集合的相等判定、并、交和差运算。要求:

? 1)自定义数据结构

? 2)自先存储结构,并设计算法。在VC中实现。

以上三题,第1题必须完成。第2和第3题可以作为选做题。

四.参考资料

??? 实验教材P170到182.

五.实验报告

1.在博客中先写上实习目的和内容,画出主要操作运算算法图,然后分别上传程序代码。插入调试关键结果截图。

2.写一个博文,比较总结线性表的两种主要存储结果:顺序表和单链表。

总结:编程基础还是差了点,对于单链表的初始化,插入、删除操作经过这几天的一次次编译,一次次改错,终于有了一丝进步。也终于看清了一点,数据结构就是要解决数据的逻辑关系以及存储关系和操作,解决问题那归算法管。之前老有一个误区,或者说还没从大一的c++程序设计的思想转换过来。现在总算转换过来了!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读