C++实现顺序表的基本操作

头文件:

#ifndef List_H
#define List_H

typedef int Elem;
class List{
public:
    List(int size);             //初始化顺序表
    ~List();                    //销毁表,析构函数  
    bool isEmpty();             //是否为空
    int listLength();           //表中元素个数
    void clearList();           //清空表
    bool getElem(int i,Elem *e);     //
    int locateElem(Elem *e);         //返回指定元素的位置
    bool priorElem(Elem *currentElem,Elem *preElem);        //找前驱
    bool nextElem(Elem *currentElem,Elem *nextElem);        //找后继
    void listTraverse();         //遍历顺序表
    bool listInsert(int i,Elem *e);     //在指定位置插入元素
    bool listDelete(int i,Elem *e);     //删除指定位置的元素
private:
    int *m_pList;            //指针
    int m_iSize;             //定义表的大小
    int m_iLength;           //表中元素个数
};
#endif

关键代码:

#include "List.h"
#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::isEmpty(){
    return m_iLength==0?true:false;
}
int List::listLength(){
    return m_iLength;
}
bool List::getElem(int i,Elem *e){
    if (i<0||i>=m_iSize)
    {
        return false;
    }else{
    *e=m_pList[i];
    return true;
    }
}
int List::locateElem(Elem *e){
    for (int i=0;i<m_iLength;i++)
    {
        if (m_pList[i]==*e)
        {
            return i;
        }
    }
    return -1;
}
bool List::priorElem(Elem *currentElem,Elem *preElem){
    int curLoc=locateElem(currentElem);
    if (curLoc==-1)
    {
        return false;
    }else if(curLoc==0){
        return false;
    }else{
        *preElem=m_pList[curLoc-1];
        return true;
    }
}
bool List::nextElem(Elem *currentElem,Elem *nextElem){
    int curLoc=locateElem(currentElem);
    if (curLoc==-1)
    {
        return false;
    }else if(curLoc==m_iLength-1){
        return false;
    }else{
        *nextElem=m_pList[curLoc+1];
        return true;
    }
}
void List::listTraverse(){
    for (int i=0;i<m_iLength;i++)
    {
        cout<<m_pList[i]<<" ";
    }
}
bool List::listInsert(int i,Elem *e){
    if (i<0||i>m_iLength)
    {
        return false;
    }else{
    for (int k=m_iLength-1;k>=i;k--)
    {
        m_pList[k+1]=m_pList[k];
    }
    m_pList[i]=*e;
    m_iLength++;
    return true;
    }
}
bool List::listDelete(int i,Elem *e){
    if (i<0||i>=m_iLength)
    {
        return false;
    }
    *e=m_pList[i];

    for (int k=i+1;k<m_iLength;k++)
    {
        m_pList[k-1]=m_pList[k];
    }
    m_iLength--;
    return true;
}

实现:

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

using namespace std;
int main(){
    List *p=new List(7);
    int e1=5;
    int e2=9;
    int e3=4;
    int e4=6;
    int e5=8;
    int e6=7;
    int e7=3;
    if(p->isEmpty())
        cout<<"Empty"<<endl;
    p->listInsert(0,&e1);
    p->listInsert(1,&e2);
    p->listInsert(2,&e3);
    p->listInsert(3,&e4);
    p->listInsert(4,&e5);
    p->listInsert(5,&e6);
    p->listInsert(6,&e7);
    p->listTraverse();
    cout<<endl;
    p->locateElem(&e5);
    int e=0;
    p->priorElem(&e2,&e);
    cout<<"第二个元素的前驱"<<e<<endl;
    p->nextElem(&e2,&e);
    cout<<"后继"<<e<<endl;
    p->listDelete(5,&e);
    cout<<"删除元素:"<<e<<endl;
    p->listTraverse();
    cout<<endl;
    p->getElem(7,&e);
    cout<<"最后位置元素:"<<e<<endl;
    e=24;
    p->listInsert(4,&e);
    p->listTraverse();
    cout<<endl;
    cout<<"24在:"<<p->locateElem(&e)+1<<endl;
    p->clearList();
    if(p->isEmpty())
        cout<<"Empty"<<endl;

    return 0;
}

c++顺序表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值