为了账号安全,请及时绑定邮箱和手机立即绑定

具体实现代码@数据结构探险——顺序表

标签:
C++
file:List.hpp

#ifndef List_hpp
#define List_hpp

#include <stdio.h>

class List{
public:
    List(int size);
    ~List();
    void clearList();
    bool ListEmpty();
    int ListLength();
    bool getElem(int i,int &elem);
    int locateElem(int elem);
    bool priorElem(int *currentElem,int *preElem);
    bool nextElem(int *currentElem,int *nextElem);
    void ListTraverse();
    bool ListInsert(int i,int *Elem);
    bool ListDelete(int i,int *Elem);

private:
    int *m_pList;
    int m_iSize;
    int m_iLength;
};

#endif /* List_hpp */
file:List.cpp

#include "List.hpp"
#include <iostream>

using namespace std;

List::List(int size){
    m_iSize = size;
    m_pList = new int[m_iSize];
    m_iLength = 0;

}

List::~List(){
    delete []m_pList;
    m_pList = NULL;
}

void List::clearList(){
    m_iLength = 0;
}

bool List::ListEmpty(){
    return m_iLength==0?true:false;
}

int List::ListLength(){
    return m_iLength;
}

bool List::getElem(int i , int &elem){
    if(i < 0 or i > m_iLength){
        return false;
    }
    else{
        elem = m_pList[i];
        return true;
    }
}

int List::locateElem(int elem){
    for(int i = 0;i < m_iLength;i++){
        if(m_pList[i] == elem){
            return i;
        }
    }
    return -1;
}

bool List::priorElem(int *currentElem,int *preElem){
    int index = locateElem(*currentElem);
    if(index == -1 or index ==0){
        return false;
    }
    else{
        *preElem = m_pList[index-1];
        return true;
    }
}

bool List::nextElem(int *currentElem,int *nextElem){
    int index = locateElem(*currentElem);
    if(index == -1 or index == m_iLength-1){
        return false;
    }
    else{
        *nextElem = m_pList[index+1];
        return true;
    }
}

void List::ListTraverse(){
    for(int i = 0;i < m_iLength;i++){
        cout << m_pList[i] ;
    }
    cout << endl;
}

bool List::ListInsert(int i,int *Elem){
    if(i < 0 or i > m_iLength){
        return false;
    }
    for(int j = m_iLength;j >= i;j--){
        m_pList[j+1] = m_pList[j];
    }
    m_pList[i] = *Elem;
    m_iLength++;
    return true;
}

bool List::ListDelete(int i,int *Elem){
    if(i < 0 or i > m_iLength){
        return false;
    }
    *Elem = m_pList[i];
    for(int j = i + 1;j < m_iLength;j++){
        m_pList[j-1] = m_pList[j];
    }

    m_iLength--;
    return true;
}
file:demo.cpp

#include <iostream>
#include <string>
#include "List.hpp"

using namespace std;

int main(void){
    List *p = new List(30);
    int e1 = 1;
    int e2 = 2;
    int e3 = 3;
    int e4 = 4;
    int e5 = 5;
    int e6 = 6;
    int e7 = 7;
    int e8 = 8;
    int e9 = 9;

    p->ListInsert(0,&e1);
    p->ListTraverse();
    p->ListInsert(1,&e2);
    p->ListTraverse();
    p->ListInsert(2,&e3);
    p->ListTraverse();
    p->ListInsert(2,&e4);
    p->ListTraverse();
    p->ListInsert(2,&e5);
    p->ListTraverse();
    p->ListInsert(2,&e6);
    p->ListTraverse();
    p->ListInsert(2,&e7);
    p->ListTraverse();
    p->ListInsert(2,&e8);
    p->ListTraverse();
    p->ListInsert(2,&e9);
    p->ListTraverse();

    int Elem = 0;
    p->ListDelete(3,&Elem);
    cout << Elem << endl;
    p->ListTraverse();
    cout << p->ListLength() << endl;

    p->nextElem(&e1, &Elem);
    cout << Elem << endl;

    p->priorElem(&e2, &Elem);
    cout << Elem << endl;

    cout << p->locateElem(e5) << endl;

    p->getElem(3,Elem);
    cout << Elem << endl;

    p->clearList();
    if(p->ListEmpty()){
        cout << "顺序表为空" << endl;
    };
    p->ListTraverse();

    delete p;
    p = NULL;

}

结果如下:图片描述

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消