顺序存储结构线性表

线性表的顺序存储结构,指用一段地址连续的存储单元依次存储线性表的数据元素。

优点:

1、可以快速存取表中的任一位置的元素。

2、不需要为表中元素之间的逻辑关系增加额外的存储空间。

缺点:

1、插入和删除操作需要移动大量的元素。

2、当线性表长度变化较大时,难以确定存储空间的容量。

3、造成存储空间的“碎片”。

一、建立顺序存储结构线性表的类,包括.h文件和.cpp文件

// ArrayList.h
#pragma once

/*
实现基本功能:
1、元素插入
2、元素输出
3、元素删除
*/
template<class T>
class ArrayList
{
public:
    ArrayList(int theCapacity);                    //构造函数
    ArrayList(const ArrayList& list);              //复制构造函数
    ~ArrayList();
    int listInsert(T node, int index);           //在索引 index处插入元素
    int listDelete(int index);                   //删除索引是 index的元素
    T& getElement(int index);                   //返回索引为index的元素
    void listClear();                           //清楚线性表
    int GetLength();                           //得到数据长度
private:
    int length;
    int capacity;
    T* Node;
};
//ArrayList.cpp 
#include "ArrayList.h"
#include<iostream>
#include<string>
using namespace std;
template<class T>
ArrayList<T>::ArrayList(int theCapacity)    //构造函数
{
    if (theCapacity <= 0)
        throw("abc");
    capacity = theCapacity;
    Node = new T[theCapacity];
    length = 0;

}
template<class T>
ArrayList<T>::ArrayList(const ArrayList& list)
{
    this->capacity = list.capacity;
    this->length = list.length;
    this->Node = new T[this->capacity];
    for (int i = 0; i < list.length; i++)
        this->Node[i] = list.Node[i];
}
template<class T>
ArrayList<T>::~ArrayList()
{
    delete[]Node;
}
template<class T>
int ArrayList<T>::listInsert(T node, int index)    //在索引 index处插入元素
{
    if (this->length >= this->capacity)
        throw("abc");
    if (index >= this->length)
        index = this->length;
    for (int i = length; i > index; i--)
        this->Node[i] = this->Node[i - 1];
    this->Node[index] = node;
    this->length++;
    return 0;
}

template<class T>
int ArrayList<T>::listDelete(int index)                  //删除索引是 index的元素
{
    if (index<0 || index>this->length)
        throw("abc");
    for (int i = index; i < this->length-1; i++)
        this->Node[i] = this->Node[i + 1];
    this->length--;
    return 0;
}

template<class T>
T& ArrayList<T>::getElement(int index)              //返回索引为index的元素
{ 
    return this->Node[index];
}

测试代码:

#include<iostream>
#include<string>
#include"ArrayList.h"
#include"ArrayList.cpp"
using namespace std;

typedef struct myStruct
{
    int age;
    string name;
}Teacher;
void print(const ArrayList<Teacher>&list)
{
    ArrayList<Teacher>List(list);
    Teacher t;
    t = List.getElement(0);
    cout << t.age << endl;
    List.listDelete(0);
    t = List.getElement(0);
    cout << t.age << endl;
}

int main()
{
    try{
        int ret = 0;
        Teacher t0, t1, t2, t3, t4, t;
        t0.age = 20;
        t1.age = 21;
        t2.age = 22;
        t3.age = 23;
        t4.age = 24;
        ArrayList<Teacher>list(5);
        ret = list.listInsert(t0, 0);
        ret = list.listInsert(t1, 0);
        ret = list.listInsert(t2, 0);
        ret = list.listInsert(t3, 0);
        ret = list.listInsert(t4, 0);
        print(list);
        list.listDelete(6);
        t = list.getElement(-1);
        cout << t.age << endl;
    }
    catch (string)
    {
        cout << "there are somethine wrong!" << endl;
    }
    system("pause");
    return 0;
}
View Code

 

 

 

 

 

 

 

参考:《大话数据结构》

《数据结构、算法与应用》

 

posted @ 2017-05-26 17:29  hello_gogogo  阅读(350)  评论(0编辑  收藏  举报