线性表之顺序表C++实现

源代码:https://github.com/cjy513203427/C_Program_Base/tree/master/53.%E9%A1%BA%E5%BA%8F%E8%A1%A8

定义:

线性表是n个数据元素的有限序列

需要实现的方法如下:

#pragma once
#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
class List
{
public:
    List(int size);//初始顺序表
    ~List();//删除顺序表
    void ClearList();//清空顺序表
    bool ListEmpty();//顺序表判空
    int ListLength();//获取顺序表长度
    bool GetElem(int i,int *e);//获取指定元素
    int LocateElem(int *e);//寻找第一个等于e的数据元素的位序
    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_H

1.构造函数

传入用户指定的容量参数赋值给m_iSize

声明指针m_pList指向int数组

m_iLength置0

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

2.析构函数

删除数组指针并置空

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

3.清空顺序表

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

4.判空和获取顺序表长度

m_iLength等于0返回正确,否则返回错误

返回m_iLength获取长度

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

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

5.获取指定元素

对索引i进行判断

如果小于0或者大于等于长度,是不符合规则的,返回错误

将m_pList[i]的值赋值给e指向的内存空间的值

bool List::GetElem(int i, int *e)//*e相当于引用,不作为参数,只作为返回结果使用
{
    if (i < 0 || i >= m_iSize)
    {
        return false;
    }
    else {
        *e = m_pList[i];
        return true;
    }
}

6.寻找第一个等于e的数据元素的索引

遍历循环数组,当e所指向的内存空间的值和m_pList[i]相等,返回i

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

7.获取前驱结点

判断是否获取到指定元素的索引,没获取到返回-1

获取到的话索引不能等于0,因为首元素没有前驱结点

当索引不等于-1和0,将m_pList[temp-1]赋值给preElem指向的内存空间的值

bool List::PriorElem(int *currentElem, int *preElem)
{
    int temp = LocateElem(currentElem);
    if (temp == -1)
    {
        return false;
    }
    else {
        if (temp == 0)
        {
            return false;
        }
        else 
        {
            *preElem = m_pList[temp - 1];
            return true;
        }
    }
}

8.获取后继结点

逻辑同上,索引等于-1没有说明该元素不存在

如果索引等于m_iLength即m_pList[m_iLength-1]是最后一个元素,最后一个元素没有后继结点

当索引既不等于-1也不等于m_iLength时,将m_pList[temp+1]赋值给nextElem所指向的内存空间的值

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

9.遍历

没什么好说的

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

10.插入元素

先判断索引是否合法

索引如果合法

先对插入元素的位置后面进行移动,并且是倒序移动,因为顺序移动顺序表前面的元素值会一直覆盖掉后面的

假如按顺序插入,如

list1->ListInsert(0, &e1);
 list1->ListInsert(1,&e2);

是不会进入for循环的,只有插入同一个位置,才会向后移动元素

移动完元素后进行赋值操作

长度++

PS:这里m_pList[k+1] = m_pList[k]写成m_pList[k] = m_pList[k-1]效果是一样的,那么上面k就要等于m_iLength = m_iLength,不然顺序表尾元素会丢失掉

bool List::ListInsert(int i, int *Elem)
{
    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] = *Elem;
        m_iLength++;
        return true;
    }
}

11.删除元素

对索引判断是否合法

然后先将要删的元素赋值给Elem所指向空间的值,后赋值,元素已经被删掉,获取不到了

从前往后移动,从后面往前移动会造成元素值的丢失

移动完毕

长度--

bool List::ListDelete(int i, int *Elem)
{
    if (i<0 || i>=m_iLength)
    {
        return false;
    }
    else
    {
        *Elem = m_pList[i];
        //先在前面进行移动,从后面开始移动会造成值的替代
        //k=i+1对应着k-1,若k=i,k <= m_iLength-1,m_pList[k] = m_pList[k+1];
        for (int k = i+1; k <= m_iLength; k++)
        {
            m_pList[k-1] = m_pList[k];
        }

        m_iLength--;
        return true;
    }
}
原文地址:https://www.cnblogs.com/Java-Starter/p/9397368.html