順序存儲結構線性表

線性表的順序存儲結構,指用一段地址連續的存儲單元依次存儲線性表的數據元素。node

優勢:ios

一、能夠快速存取表中的任一位置的元素。算法

二、不須要爲表中元素之間的邏輯關係增長額外的存儲空間。數據結構

缺點:ide

一、插入和刪除操做須要移動大量的元素。函數

二、當線性表長度變化較大時,難以肯定存儲空間的容量。測試

三、形成存儲空間的「碎片」。this

1、創建順序存儲結構線性表的類,包括.h文件和.cpp文件spa

// ArrayList.h
#pragma once

/*
實現基本功能:
一、元素插入
二、元素輸出
三、元素刪除
*/
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];
}

測試代碼:code

#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

 

 

 

 

 

 

 

參考:《大話數據結構》

《數據結構、算法與應用》

相關文章
相關標籤/搜索