数据结构与算法_实验一顺序表

实验名称

实验一 顺序表

实验目的

理解顺序表的定义掌握顺序表的检索、插入、删除等算法的实现

实验任务

实现顺序表完成线性表的基本操作:

初始化线性表、清空线性表、求线性表长度、检查线性表是否为空、遍历线性表、从线性表中查找元素、从线性表中查找与给定元素值相同的元素在线性表中的位置、插入元素、删除元素。

实验内容

新建一个项目

存放在文件LIST.h中

#ifndef _LIST_H_

#define _LIST_H_

#include <bits/stdc++.h>

template<class T>

class List{

    public:

        virtual void clear()=0;

        virtual bool empty()const=0;

        virtual int size()const=0;

        virtual void insert(int i,const T &value)=0;

        virtual void remove(int i)=0;

        virtual int search(const T&value)const=0;

        virtual T visit(int i)const=0;

        virtual void traverse()const=0;

        virtual void inverse()=0;

        virtual ~List(){};

};

class outOfRange;

class badSize;

class outOfRange : public exception {

public:

    const char* what()const throw() { return "ERROR OUT OF RANGE. \n";}

};

class badSize : public exception {

    public:

        const char* what()const throw() { return "ERROR BAD SIZE. \n";}

};

#endif

存放在seqList.h中

#ifndef _SEQLIST_H_

#define _SEQLIST_H_

template <class T>

class seqList: public List<T>{

private:

    T *data;

    int curLength;

    int maxSize;

    void resize();

public:

    seqList(int initSize = 10);

    seqList(seqList & SL);

    void clear()  {curLength = 0;}

    bool empty()const{return curLength==0;}

    int size() const  {return curLength;}

    void insert(int i,const T &value);

    void remove(int i);

    int search(const T &value) const;

    T visit(int i) const;

    void traverse() const  ;

    void inverse();

    bool ally(seqList<T> &B);

    ~seqList(){};

};

template <class T>

seqList<T>::seqList(int initSize)

{

    if (initSize <= 0 ) throw badSize();

    maxSize = initSize;

    data = new T[maxSize];

    curLength = 0;

   

}

template <class T>

seqList<T>::seqList(seqList & SL) {

    maxSize = SL.maxSize;

    curLength = SL.curLength;

    data = new T[maxSize];

    for  (int i =0; i< curLength; i++)

         data[i] = SL.data[i];

        

}

template <class T>

void seqList<T>::insert(int i, const T &value)

{

    if (i<0 || i>curLength) throw outOfRange();

    if (curLength == maxSize) resize();

    for (int j = curLength; j>i;j--)

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

    data[i] = value;

    ++curLength;

}

template <class T>

void seqList<T>::remove(int i)

{

    if (i < 0 || i > curLength-1) throw outOfRange();

    for (int j = i; j <curLength - 1; j++)

        data[j] = data[j+1] ;

    --curLength;

}

template <class T>

void seqList<T>::resize()

{

    T *p = data;

    maxSize *= 2;

    data = new T[maxSize];

    for (int i = 0; i < curLength; ++i)

        data[i] = p[i];

    delete [] p;

}

template<class T>

int seqList<T>::search(const T & value) const

{

    for (int i = 0; i < curLength; i++)

        if (value == data[i]) return i;

    return -1;

}

template<class T>

T seqList<T>::visit(int i) const

{

    if (i < 0 || i > curLength-1 ) throw outOfRange();

    return data[i];

     

}

template<class T>

void seqList<T>::traverse()const

{

    if(empty())cout<<"List is empty\n";

    else{

        cout<<"output T:\n";

        for (int i = 0; i < curLength; i++)

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

        cout<<endl;

    }

}

template<class T>

void seqList<T>::inverse()

{

    T tmp;

    for(int i = 0; i < curLength/2; i++)

    {

        tmp = data[i];

        data[i] = data[curLength-i-1];

        data[curLength-i-1] = tmp;

    }

}

template<class T>

bool seqList<T>::ally(seqList<T> &B){

    int m,n,k,i,j;

    m = this->curLength;

    n = B.curLength;

    k = m+n-1;

    i = m-1 , j = n - 1;

    if(m+n > this->maxSize){

        resize();

    }

    while (i>=0 && j>=0)

        if (data[i] >= B.data[j])  data[k--] = data[i--];

        else  data[k--] = B.data[j];

    while(j>=0)

        data[k--] = B.data[j--];

    curLength = m+n;

    return true;

}

#endif

存放在mian.cpp中

#include <bits/stdc++.h>

using namespace std;

#include"C:\Users\刘希\Documents\Projects\项目5\LIST.h"

#include"C:\Users\刘希\Documents\Projects\项目5\seqList.h"

template<class T>

void test(List<T>* p){

    if(p->empty())cout<<"empty list\n";

    cout << "test insert:\n";

    int n, i;

    T val;

    cout<<"input number of nodes;: \n";

    cin >>n;

    cout<<"input each node's insertion position and value:\n";

    while(n>0){

        cin >> i;

        cin >>val;

        try

        {

           p->insert(i, val);

        }

        catch (outOfRange)

        {

           cerr << "error,catch outOfRange\n";

        }

        --n;

    }

    p->traverse();

    cout << "curLength:"<<p->size()<<endl;

    cout << "test inverse:\n";

    p->inverse();

    p->traverse();

   

    cout<< "test search:\n";

    cout<<"input the value:\n";

    cin>>val;

    cout<<"the position of "<<val<<"p->search(val)"<<endl;

   

    cout<< "test delete:\n";

    cout<<"input the position:\n";

    cin >> i;

    try{

        p->remove(i);

    }

    catch(outOfRange){

        cerr <<"error,catch outOfRange\n";

    }

    p->traverse();

   

    cout<<"test the visit:\n";

    cout<<"input the position[0...n-1]:\n";

    cin>>i;

    try{

        cout<<"position:"<<i<<":"<<p->visit(i)<<endl;

    }

    catch(outOfRange){

        cerr<<"error, catch outOfRange.\n";

    }

}

int main(){

    seqList<int> *sq;

    sq = new seqList<int>(100);

    test(sq);

    delete[] sq;

    system("pause");

    return 0;

}

测试与小结

首先输入元素的的总数,输入总数为4

使用头插法依次输入插入元素的位置和值0 1 0 2 0 3 0 4 输出的值为 4 3 2 1 长度为4  

查找值为5的数输出的值为-1则为没有找到

删除位置为0 的数输出为2 3 4则成功删除位序为0的数的值

输入查找元素的位序0输出为2

小结:

#include"C:\Users\user\Documents\Projects\项目5\LIST.h"

#include"C:\Users\user\Documents\Projects\项目5\seqList.h"

文件的调用要以文件的位置的方式调用否则会出现错误

#include <bits/stdc++.h>

采用万能头文件可以减少代码输入量,避免因为某些头文件的未导入而出现的错误

提高代码输入的准确率

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

激稳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值