线性表(1)--顺序表模拟

线性表(1)--顺序表模拟

顺序表:
知识点简要介绍:
    基本的数据结构,在计算机内存中以数组的形式保存的线性表,指用一组地址连续的存储单元依次存储数据元素的线性结构
队列,有”第一元素“、”最后元素“;有前驱、后继之说
优点:
    遍历和寻址时效率高,定位具体元素快
缺点:
    插入和删除效率一般
    
链表的模板实现:
    适用于任意类型
    
模拟开始:
//--------------------------------------------------------------
List .h 顺序表的模板定义及实现
<span style="font-size:14px;">#ifndef LIST_H
#define LIST_H
//-------------------
template <typename T>
class List{

public:
    List(int size);//创建线性表
    ~List();//销毁线性表
    void clearList();//清除
    bool listEmpty();//判空
    int listLength() const;//线性表长度
    bool getElement(int i, T *e);//获取指定元素
    int locElement(T *e);//寻找第一个满足e数据元素的位序
    bool priorElement(T *currentList, T *preElem);//获得前驱
    bool nextElement(T *currentList, T *nextElem);//获得后驱
    void listTraverse();//遍历
    bool listInsert(int i, T *e);//在i位置插入元素e
    bool listDelete(int i, T *e);//删除第i个位置
    
private:
    T *m_pList;
    int m_iSize;
    int m_iLength;
};

//------------------------------------
//--------List.cpp--------------------
//-------------------------------------
//#include"List.h"
#include<iostream>
using namespace std;
//----------------------

template <typename T>
List<T>::List(int size){//初始化
    m_iSize = size;
    m_pList = new T[m_iSize];
    m_iLength = 0;
}
template <typename T>
List<T>::~List(){
    delete []m_pList;//释放内存
    m_pList = NULL;
}

template <typename T>
void List<T>::clearList(){
    m_iLength = 0;
}
template <typename T>
bool List<T>::listEmpty(){
    return 0 == m_iLength ? true : false;
}
template <typename T>
int List<T>::listLength() const{
    return m_iLength;
}

template <typename T>
bool List<T>::getElement(int i, T *e){
    if(i < 0 || i > m_iSize)
        return false;
    *e = m_pList[i];
    return true;
}
template <typename T>
int List<T>::locElement(T *e){
    for(int i = 0; i < m_iLength; i++){
        if(m_pList[i] == *e)
            return i;
        else return -1;
    }
    
}
template <typename T>
bool List<T>::priorElement(T *currentList, T *preElem){
    T t = locElement(currentList);
    if(t != -1) {
        if(0 == t) return false;
        else{
            *preElem = m_pList[t-1];
            return true;
        }
    }
    else return false;
}
template <typename T>
bool List<T>::nextElement(T *currentList, T *nextElem){
        T t = locElement(currentList);
    if(t != -1) {
        if(m_iLength-1 == t) return false;
        else{
            *nextElem = m_pList[t+1];
            return true;
        }
    }
    else return false;
}

template <typename T>
void List<T>::listTraverse(){
    for(int i = 0; i < m_iLength; i++){
        cout << m_pList[i] << " ";
    }
    cout << endl;
}

template <typename T>
bool List<T>::listInsert(int i, T *e){
    
    if(i < 0 || i > m_iLength){
        return false;
    }
    for(int j = m_iLength - 1; j >= i ; j++){//先移动后插入元素
        m_pList[j+1] = m_pList[j];//i位置后面的元素都向后移位
    }
    
    m_pList[i] = *e;
    m_iLength++;
    return true;
}
template <typename T>
bool List<T>::listDelete(int i, T *e){
    
    if(i < 0 || i >= m_iLength){
        return false;
    }
    
    *e = m_pList[i];
    
    for(int j = i+1; j < m_iLength; j++){
        m_pList[j-1] = m_pList[j];
    }
    
    m_iLength--;
    return true;
}

#endif LIST_H</span>


测试开始:
Coordinate.h
<span style="font-size:14px;">#ifndef COORDINATE_H
#define COORDINATE_H
//------------------
#include<ostream>
using namespace std;
//------------------
class Coordinate{
    friend ostream& operator<<(ostream& o, Coordinate& coor);//"<<"操作符重载    
public:
    Coordinate(int x = 0, int y = 0);
    void printCoor();
    bool operator==(Coordinate &coor);
private:
    int m_iX;
    int m_iY;    
};

#endif COORDINATE_H</span>


Coordinate.cpp
<span style="font-size:14px;">#include"Coordinate.h"
#include<iostream>
using namespace std;

Coordinate::Coordinate(int x, int y):m_iX(x), m_iY(y){}

void Coordinate::printCoor(){
    cout << "(" << m_iX << "," << m_iY << ")" << endl;
}

ostream& operator<<(ostream& o, Coordinate& coor){
    o << "(" << coor.m_iX << "," << coor.m_iY << ")" ;//<< endl;
    return o;
}

bool Coordinate::operator==(Coordinate &coor){
    if(this->m_iX == coor.m_iX && this->m_iY == coor.m_iY)
        return true;
    else return false;
}</span>


demo.cpp
<span style="font-size:14px;">#include"List.h"
#include"Coordinate.h"
#include<iostream>
//#include<string.h>
//#include<stdlib.h>
using namespace std;
int main(){
    //int类型与Coordinate类类型测试
    //int类型
    cout << "int类型:\n" << endl;
    int e[] = {3, 4, 6, 7, 8, 9, 2};

    List<int> *list1 = new List<int>(10);
    
    for(int i = 0; i < sizeof(e)/sizeof(e[0]); i++){//i < e.length,哎,竟然没有.length方法
        list1->listInsert(i, &e[i]);
    }
    cout << "遍历得:" ;
    list1->listTraverse();
    
    
    int t = 0;
    list1->listDelete(0, &t);
    cout << "删除的值:" << t << endl;
    
    list1->clearList();
    if(list1->listEmpty()){
        cout << "删除后,线性表为空!" << endl;
    }
    
    //Coordinate类型测试
    cout << "\nCoordinate类类型:\n" << endl;
    List<Coordinate> *list2 = new  List<Coordinate>(10);
    
    Coordinate e1(2, 3);
    Coordinate e2(3, 5);
    Coordinate e3(4, 7);
    
    list2->listInsert(0, &e1);
    list2->listInsert(1, &e2);
    list2->listInsert(2, &e3);
    
    list2->listTraverse();
    
    delete list1;
    list1 = NULL;
    
    system("pause");
    return 0;
} </span>


//控制台运行结果:
int类型:

遍历得:3 4 6 7 8 9 2
删除的值:3
删除后,线性表为空!

Coordinate类类型:

(2,3) (3,5) (4,7)

//-------------------------------------------------
//-------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值