##顺序表 编码##

#ifndef LIST_H
#define LIST_H
class List
{
    public:
        List(int size);
        ~List();//析构函数
        void ClearList();//清空线性表
        bool ListEmpty();//判断线性表是否为空
        int ListLength();//获取线性表长度
        bool GetElem(int i,Elem *e);//获取指定元素
        int LocateElem(Elem *e);//寻找第一个满足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_size;//内存大小
        int  m_length;//当前线性表长度
}
#endif
#include "list.h"
#include <iostream>
using namespace std;
List::list(int size)
{
    m_size=size;
    m_plist=new int[m_size];
    m_length=0;
}
List::~List()
{
    delete []m_plist;
    m_plist=NULL;
}
void List::ClearList()
{
    m_length=0;
}
bool List::ListEmpty()
{
    /*
    if(m_length==0)
     {
         return true;
     }
     else
     {
         return false;
     }
     */
      return m_length==0?true:false;
}
int List::ListLength()
{
    return m_length;
}
bool List::GetElem(int i,Elem *e)
{
    if(i<0||i>=m_size)
    {
        return false;
    }
        *e=m_plist[i];
        return true;
}
int List::LocateElem(Elem *e)
{
    for(int i=0;i<m_length;i++)
    {
        if(m_plist[i]==*e)
        {
            return i;
        }
    }
    return -1;
}
bool List::PriorElem(Elem *currentElem,Elem *preElem)//寻找前驱
{
    int temp=locateElem(currentElem);
    if(temp==-1||temp==0)
    {
        return false;
    }
    else
    {
        preElem=m_plist[temp-1];
        return true;
    }
}
bool List::NextElem(Elem *currentElem,Elem *nextElem)
{
    int temp=locateElem(currentElem);
    if(temp==-1||temp==m_length-1)
    {
        return false;
    }
    else
    {
        nextElem=m_plist[temp+1];
        return true;
    }
}
void List::ListTraverse()
{
    for(int i=0;i<m_length;i++)
    {
        cout<<m_plist[i]<<endl;
    }
}
bool List::ListInsert(int i,Elem *e)
{
    if(i<0||i<=m_length)
    {
        return false;
    }
    if(m_iLength+1>m_iSize)
    {
        return false;
    }
    for(int k=m_length-1;k>=i;k--)
    {
        m_plist[k+1]=m_plist[k];
    }
    m_plist[i]=*e;
    return true;
}
bool List::ListDelete(int i,Elem *e)
{
    if(i<0||i>=m_length)
    {
        return false;
    }
    *e=m_plist[i];
    for(int k=i+1;k<m_length;k++)
    {
        m_plist[k-1]=m_plist[k];
    }
    m_length--;
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值