复习(数据结构):线性表 : c++_标准写法

1. 设计数据结构接口

LinearList{
    实例:
    0或多个元素的有序集合
    操作:
    Creat();
    Destroy();
    IsEmpty();
    Length();
    Find(k,x); //寻找第k个数,并保存到x
    Search(x); //返回元素x的位置
    Delete(k,x); //删除表中第k个元素,并保存在x
    Insert(k,x); //在第k个元素的后面插入x
    Output(out); //线性表的输出
}

2. 设计类的基本描述

#ifndef LinearList_
#define LinearList_
#include <iostream>
#include <stdlib.h>
#include "xcept.h"

using namespace std;

template<class T>
class LinearList{
public:
    LinearList(int MaxListSize=10); //构造函数
    ~LinearList(){delete[] element;}  // 析构函数
    bool IsEmpty() const {return length==0;}  
    //const 成员函数,不可以修改成员变量
    int Length()const {return length;}
    bool Find(int k,T &x) const; //返回第K个元素至x
    int Search(const T &x) const; //返回x所在的位置
    /*
    引用参数在函数内不可以改变;
    这样的一个const引用传递和最普通的函数按值传递的效果是一模一样的,
    他禁止对引用的对象的一切修改,唯一不同的是按值传递会先建立一个类对象的副本, 
    然后传递过去,而它直接传递地址,
    所以这种传递比按值传递更有效.
     */

    LinearList<T>& Delete(int k,T& x)//删除第k个元素并将它返回至x中
    //返回引用
    LinearList<T>& Insert(int k,const T& x); //在第k个元素之后插入x
    void Output() const;

private:
    int length;
    int MaxSize;
    T *elemment; //一维动态数组



}; 

#endif
//内存异常
//内存异常
#include<new.h>
// insufficient memory
class NoMem {
   public:
      NoMem() {}
};

//new引发Nomen的异常,而不是xalloc
void my_new_handler(){
    throw NoMem();
}

new handler Old_Handler =  set_new_handly(my_new_handler);

//超出边界
class OutOfBounds {
   public:
      OutOfBounds() {}
};



操作的实现

//构造函数
tempalte<class T>
LinearList<T>::LinearList(int MaxListSize){
    MaxSize = MaxListSize;
    element=new T[MaxSize];
    length=0;
}
template<class T>
bool LinearList<T>::Find(intk,T& x)const{
    if(K<1||k>length)
        return false;  // 不存在第k个元素
    x=element[k-1];
    return true;
}

template<class T>
int LinearList<T>::Search(const T& x)const{
    for(int i=0;i<length;i++)
        if(element[i]==x) return ++i;
    return 0;
}

// 删除第k个元素,将值放入x
// k不存在则引发异常
template<class T>
LinearList<T>& LinearList<T>::Delete(int k,T& x){
if(Find(k,x)){
    for(int i=k;i<length;i++)
        element[i-1] = element[i];
    length--;
    return *this;
}
else throw OutBounds();
}
//插入
//k不存在则引发异常
//内存不够引发异常
template<class T>
LinearList<T>& LinearList<T>::Insert(int k,const T& x){
    if(k<0||k>length)  throw OutBounds();
    if(length==MaxSize) throw NoMem();
    for(int i=length-i;i>=k;i--)
        element[i+1]=element[i];
    element[k]=x;
    length++;
    return *this;
}

template<class T>
void LinearList<T>::Output(ostream& out) const{
    //表输出到输出端
    for(int i=0;i<length;i++)
        out<<element[i]<<" "''
}

// 重载<<
template<class T>
ostream& operator<<(ostream& out,const LinearList<T>& x){
    x.Output(out);
    return out;
}

void main(void)
{
   try {
      LinearList<int> L(5);
      cout << "Length = " << L.Length() << endl;
      cout << "IsEmpty = " << L.IsEmpty() << endl;
      L.Insert(0,2).Insert(1,6);
      cout << "List is " << L << endl;
      cout << "IsEmpty = " << L.IsEmpty() << endl;
      int z;
      L.Find(1,z);
      cout << "First element is " << z << endl;
      cout << "Length = " << L.Length() << endl;
      L.Delete(1,z);
      cout << "Deleted element is " << z << endl;
      cout << "List is " << L << endl;
      }
   catch (...) {
      cerr << "An exception has occurred" << endl;
      }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值