数据结构——顺序表(c++)

myList.h

//myList.h
// abstract data type for List  

template <class T>  
class List  { 
      void clear();                     // 置空线性表 
      bool isEmpty();                   // 线性表为空时,返回True 
      bool append(T value);             // 在表尾添加一个元素value,表的长度增1 
      bool insert(int p, T value);          // 在位置p上插入一个元素value,表的长度增1 
      bool del(int p);              // 删除位置p上的元素,表的长度减 1 
      T getValue(int p);                // 返回位置p的元素值  
      T setValue(int p, T value);           // 用value修改位置p的元素值 
};  


arrList.h

//arrList.h
#include <iostream> 
#include <string>  
#include "myList.h" 

using namespace std; 

template  <class T>             // 假定顺序表的元素类型为T 
class arrList : public List<T>  {       // 顺序表,向量 
private:                        // 线性表的取值类型和取值空间 
    int  maxSize;               // 私有变量,顺序表实例的最大长度 
    int  curLen;                    // 私有变量,顺序表实例的当前长度 
    int  position;              // 私有变量,当前处理位置 
    T  *aList ;                 // 私有变量,存储顺序表的实例 
public:                         // 顺序表的运算集 
    arrList(const int size) {       // 创建一个新的顺序表,参数为表实例的最大长度 
        maxSize = size; 
        aList = new T[maxSize]; 
        curLen = position = 0; 
    } 
    ~arrList() {                   // 析构函数,用于消除该表实例 
        delete [] aList; 
    }  
    void clear() {                  // 将顺序表存储的内容清除,成为空表 
        delete [] aList; 
        curLen = position = 0; 
        aList = new T[maxSize]; 
    } 
    int length();                   // 返回此顺序表的当前实际长度 
    bool append(T value);           // 在表尾添加一个元素value,表的长度增1 
    bool insert(int p, T value);        // 在位置p上插入一个元素value,表的长度增1 
    bool del(int p);            // 删除位置p上的元素,表的长度减 1 
    int getPos(const T value);      // 在线性表中查找值为value的元素,并返回第1次出现的位置 
    void print();                     // 打印线性表  
};  

template <class T>                      // 假定顺序表的元素类型为T  
int arrList<T> :: getPos (const T value) { 
        int i;
        for(i=0;i<n;i++)
        {
            if(value==arrList[i])
            {
                p=i;
                return true;
            }
        }
        return false;

} 

// 设元素的类型为T, aList是存储顺序表的数组, maxSize是其最大长度; 
// p为新元素value的插入位置, 
// 插入成功则返回true, 否则返回false 
template <class T>                          // 假定顺序表的元素类型为T 
bool arrList<T> :: insert(int p, const T value)  { 
        int i;
        if(curLen>=maxSize)
        {
            cout<<"The List is overflow"<<endl;
            return false;
        }
        if(p<0||p>curLen)
        {
            cout<<"Insertion point is illegal"<<endl;
            return false;
        }
        for(i=curLen;i>p;i--)
        {
            aList[i]=aList[i-1];
        }
        aList[p]=value;
        curLen++;
        return true;

} 


template <class T>                  // 假定顺序表的元素类型为T 
bool arrList<T> :: del(int p)  { 
    int i;
    if(curLen<=0)
    {
        cout<<"No element to delete\n"<<endl;
        return false;
    }
    if(p<0||p>curLen-1)
    {
        cout<<"deletion is illegal\n"<<endl;
        return false;
    }
    for(i=p;i<curLen-1;i++)
    {
        aList[i]=aList[i+1];
    }
    curLen--;
    return true;
} 

template <class T>                  // 假定顺序表的元素类型为T 
void arrList<T> :: print()  { 
     for (int i = 0; i < curLen; i++) 
        cout << aList[i]<<" ";  
     cout << endl;              // 从位置p开始每个元素左移直到curLen, 
} 




main.cpp

//main.cpp
#include <cstdlib>    
#include <iostream>    

#include "arrList.h"    

using namespace std;   

int main(int argc, char *argv[])   
{   
    arrList<int>arr(8);
    arr.insert(0,1);
    arr.insert(1,2);
    arr.insert(2,3);
    arr.insert(3,4);
    arr.insert(4,5);
    arr.insert(5,6);
    arr.insert(6,7);
    cout<<"print elem:\n";
    arr.print();
    cout<<"Insert a elem 15:\n";
    arr.insert(4,15);
    cout<<"print elem:\n";
    arr.print();
    cout<<"delete a elem:\n";
    arr.del(4);
    cout<<"print elem:\n";
    arr.print();
    return 0;     
}   

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值