实验一:线性表的基本操作实现及其应用

一、实验目的

1、 熟练掌握线性表的结构特点,掌握顺序表的基本操作。

2、 巩固C++相关的程序设计方法与技术。

3、 学会使用顺序表解决实际问题。

二、实验内容

1、顺序表的建立与操作实现

建立n个元素的顺序表(n 的大小和表里数据自己确定),实现相关的操作:输出,插入,删除,查找等功能。编写完整程序实现,程序语言不限定,使用技术形式不定。

2、实际问题的解决(*)

使用顺序表来实现约瑟夫环问题。

三、实验步骤

1、依据实验内容分别说明实验程序中用到的数据类型的定义;

2、相关操作的算法表达;

3、完整程序;

4、总结、运行结果和分析。

5、总体收获和不足,疑问等。

四、实验要求

1、 按照数据结构实验任务书,提前做好实验预习与准备工作。

2、 加“*”为选做题。做好可加分。

3、 严格按照数据结构实验报告模板和规范,及时完成实验报告。

4、 在 CSDN上建立个人博客,并在上面发文章形式上交作业

答:

1、依据实验内容分别说明实验程序中用到的数据类型的定义;

答 :



int 整型 MaxSize的元素,insert函数的参数,Delete函数的参数,Locate函数的参数,length线性表长度 都定义为整形int。


char 字符类型 catch函数的参数定义为字符类型char 。
2、相关操作的算法表达;

答:(1)插入操作

void SeqList::Insert(int i,int x)

{

      if(length>=MaxSize)throw"上溢";

      if(i<1||i>length+1)throw"位置非法";

      for(intj=length;j>=1;j--)

            data[j]=data[j-1];                                             //第J个元素存在数组下标j-1处

      data[i-1]=x;

      length++;

}

(2)删除操作

int SeqList::Delete(int i)

{

      if(length==0)throw"下溢";

      if(i<1||i>length)throw"位置非法";

      intx=data[i-1];

      for(intj=1;j<length;j++)

            data[j-1]=data[j];                                         //此处j已经是元素所在的数组下标

      length--;

      returnx;

}

(3)查找元素位置操作

int SeqList::Locate(int x)

{

      for(inti=0;i<length;i++)

            if(data[i]==x)returni+1;                       //下标为1的元素其序号为i+1;

            return0;                                                //退出循环,说明查找失败;

}

3、完整程序;

答:

#ifndef SeqList_H

#define SeqList_H

const int MaxSize=10;

class SeqList

{

public:

      SeqList(){length=0;}

      SeqList(inta[],int n);

      ~SeqList(){}

      voidInsert(int i,int x);

      intDelete(int i);

      int   Locate(int x);

      voidPrintList();

private:

      intdata[MaxSize];

      intlength;

};

#endif


#include<iostream.h>

#include "SeqList.h";

SeqList::SeqList(int a[],int n)

{

      if(n>MaxSize)throw"参数非法";

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

                  data[i]=a[i];

            length=n;

}


void SeqList::Insert(int i,int x)

{

      if(length>=MaxSize)throw"上溢";

      if(i<1||i>length+1)throw"位置非法";

      for(intj=length;j>=1;j--)

            data[j]=data[j-1];

      data[i-1]=x;

      length++;

}


int SeqList::Delete(int i)

{

      if(length==0)throw"下溢";

      if(i<1||i>length)throw"位置非法";

      intx=data[i-1];

      for(intj=1;j<length;j++)

            data[j-1]=data[j];

      length--;

      returnx;

int SeqList::Locate(int x)

{

      for(inti=0;i<length;i++)

            if(data[i]==x)returni+1;

            return0;

}

void SeqList::printList()

{

      for(inti=0;i<length;i++)

            cout<<data[i]<<"";

      cout<<endl;

}

 #include<iostream.h>

#include "SeqList.h"

void main()

{

      intr[5]={1,2,3,4,5};

      SeqListL(r,5);

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

      L.PrintList();

      try

      {

            L.Insert(2,3);

      }

      catch(char*s)

      {

            cout<<s<<endl;

      }

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

      L.printList();

      cout<<"值为3的元素位置为:";

      cout<<L.Locate(3)<<endl;

      cout<<"执行删除第一个元素操作,删除前数据为:"<<endl;

      L.PrintList();

      try

      {

            L.Delete(1);

      }

      catch(char*s)

      {

            cout<<s<<endl;

      }

      cout<<"删除后数据为:"<<endl;

      L.PrintList();

}

4、总结、运行结果和分析。

答:

运行结果如图:




猜你喜欢

转载自blog.csdn.net/lum_0813/article/details/79948920