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

前端之家收集整理的这篇文章主要介绍了《数据结构》实验二:线性表实验前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

一、实验目的

巩固线性表的数据结构,学会线性表的应用。1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。2.学习运用线性表的知识来解决实际问题。3.进一步巩固程序调试方法4.进一步巩固模板程序设计。

二、实验内容1.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。要求如下:1)用顺序表来实现。#include<iostream>
using namespace std;

const int Maxsize=100;
class score
{
public:
score(){length=0;}
score(int a[],int n);
~score(){ }
int Locate(int x);
void Insert(int i,int x);
int Delete(int i);
void Printlist();
private:
int data[Maxsize];
int length;
};


score::score(int a[],int n)
{
if(n>Maxsize) throw"参数非法";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}


void score::Insert(int i,int x)
{
if(length>=Maxsize) throw"上溢";
if(i<1||i>length+1) throw"位置非法";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}


int score::Delete(int i)
{
if(length==0) throw"下溢";
if(i<1||i>length) throw"位置非法";
int x=data[i-1];
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}


int score::Locate(int x)
{
for(int i=0;i<length;i++)
if(data[i]==x) return i+1;
return 0;
}


void score::Printlist()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}


void main()
{
int r[10]={89,65,74,81,67,43,66,84,100,59};
score L(r,10);
cout<<"执行插入操作的前的数据为:"<<endl;
L.Printlist();
try
{
L.Insert(4,55);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"执行插入操作后的数据为:"<<endl;
L.Printlist();
cout<<"值为81的元素位置为:";
cout<<L.Locate(81)<<endl;
cout<<"执行删除第一个元素操作,删除前数据为:"<<endl;
L.Printlist();
try
{
L.Delete(1);
}
catch(char *s)
{
cout<<s<<endl;
}
cout<<"删除后的数据为:"<<endl;
L.Printlist();

}

2)用单链表来实现。

@H_890_301@#include<iostream>

@H_890_301@usingnamespacestd;

@H_890_301@

@H_890_301@template<classscore>

@H_890_301@structNode

@H_890_301@{

@H_890_301@scoredata;

@H_890_301@Node<score>*next;

@H_890_301@};

@H_890_301@template<classscore>

@H_890_301@classLinklist

@H_890_301@{

@H_890_301@public:

@H_890_301@Linklist();

@H_890_301@Linklist(scorea[],intn);

@H_890_301@~Linklist();

@H_890_301@intLocate(scorex);

@H_890_301@voidInsert(inti,scorex);

@H_890_301@scoreDelete(inti);

@H_890_301@voidPrintlist();

@H_890_301@private:

@H_890_301@Node<score>*first;

@H_890_301@};

@H_890_301@template<classscore>

@H_890_301@Linklist<score>::Linklist()

@H_890_301@{

@H_890_301@first=newNode<score>;

@H_890_301@first->next=NULL;

@H_890_301@}

@H_890_301@template<classscore>

@H_890_301@Linklist<score>::Linklist(scorea[],intn)

@H_890_301@{

@H_890_301@Node<score>*r,*s;

@H_890_301@first=newNode<score>;

@H_890_301@r=first;

@H_890_301@for(inti=0;i<n;i++)

@H_890_301@{

@H_890_301@s=newNode<score>;

@H_890_301@s->data=a[i];

@H_890_301@r->next=s;r=s;

@H_890_301@}

@H_890_301@r->next=NULL;

@H_890_301@}

@H_890_301@template<classscore>

@H_890_301@Linklist<score>::~Linklist()

@H_890_301@{

@H_890_301@Node<score>*q=NULL;

@H_890_301@while(first!=NULL)

@H_890_301@{

@H_890_301@q=first;

@H_890_301@first=first->next;

@H_890_301@deleteq;

@H_890_301@}

@H_890_301@}

@H_890_301@template<classscore>

@H_890_301@voidLinklist<score>::Insert(inti,scorex)

@H_890_301@{

@H_890_301@Node<score>*p=first,*s=NULL;

@H_890_301@intcount=0;

@H_890_301@while(p!=NULL&&count<i-1)

@H_890_301@{

@H_890_301@p=p->next;

@H_890_301@count++;

@H_890_301@}

@H_890_301@if(p==NULL)throw"位置";

@H_890_301@else{

@H_890_301@s=newNode<score>;s->data=x;

@H_890_301@s->next=p->next;p->next=s;

@H_890_301@}

@H_890_301@}

@H_890_301@template<classscore>

@H_890_301@scoreLinklist<score>::Delete(inti)

@H_890_301@{

@H_890_301@Node<score>*p=first,*q=NULL;

@H_890_301@scorex;

@H_890_301@intcount=0;

@H_890_301@while(p!=NULL&&count<i-1)

@H_890_301@{

@H_890_301@p=p->next;

@H_890_301@count++;

@H_890_301@}

@H_890_301@if(p==NULL||p->next==NULL)

@H_890_301@throw"位置";

@H_890_301@else{

@H_890_301@q=p->next;

@H_890_301@x=q->data;

@H_890_301@p->next=q->next;

@H_890_301@deleteq;

@H_890_301@returnx;

@H_890_301@}

@H_890_301@}

@H_890_301@template<classscore>

@H_890_301@intLinklist<score>::Locate(scorex)

@H_890_301@{

@H_890_301@Node<score>*p=first->next;

@H_890_301@intcount=1;

@H_890_301@while(p!=NULL)

@H_890_301@{

@H_890_301@if(p->data==x)returncount;

@H_890_301@p=p->next;

@H_890_301@count++;

@H_890_301@}

@H_890_301@return0;

@H_890_301@}

@H_890_301@template<classscore>

@H_890_301@voidLinklist<score>::Printlist()

@H_890_301@{

@H_890_301@Node<score>*p=first->next;

@H_890_301@while(p!=NULL)

@H_890_301@{

@H_890_301@cout<<p->data<<"";

@H_890_301@p=p->next;

@H_890_301@}

@H_890_301@cout<<endl;

@H_890_301@}

@H_890_301@voidmain()

@H_890_301@{

@H_890_301@intr[10]={72,49,82,61,83,98,70,73,84};

@H_890_301@Linklist<int>L(r,10);

@H_890_301@cout<<"执行插入操作前的数据为:"<<endl;

@H_890_301@L.Printlist();

@H_890_301@try

@H_890_301@{

@H_890_301@L.Insert(3,89);

@H_890_301@}

@H_890_301@catch(char*s)

@H_890_301@{

@H_890_301@cout<<s<<endl;

@H_890_301@}

@H_890_301@cout<<"执行插入操作后的数据为:"<<endl;

@H_890_301@L.Printlist();

@H_890_301@cout<<"值为100的元素的位置为:";

@H_890_301@cout<<L.Locate(100)<<endl;

@H_890_301@cout<<"执行删除操作前的数据为:"<<endl;

@H_890_301@L.Printlist();

@H_890_301@try

@H_890_301@{

@H_890_301@L.Delete(1);

@H_890_301@}

@H_890_301@catch(char*s)

@H_890_301@{

@H_890_301@cout<<s<<endl;

@H_890_301@}

@H_890_301@cout<<"执行删除操作后的数据为:"<<endl;

@H_890_301@L.Printlist();

@H_890_301@}

原文链接:https://www.f2er.com/datastructure/382904.html

猜你在找的数据结构相关文章