线性表--顺序表

作者: 修夏之夏i | 来源:发表于2018-06-14 07:26 被阅读10次

list.h

#include <iostream>
using namespace std;

class List{
private:
    int *m_pList;//指针指向线性表
    int m_iSize;//线性表的大小
    int m_iLength; //线性表存储的元素
public:
    List(int size);   //构造函数
    ~List(); //默认传递this指针
    void ClearList();//清空当前线性表里的元素,不等于释放线性表的内存
    bool ListEmpty(); //判断线性表是否为空
    int ListLength(); // 获取线性表的长度
    bool GetElem(int i,int *e); //获取指定的元素
    int LocateElem(int *e); //获取指定元素的位置
    bool PriorElem(int* currentElem, int* preElem);//获取元素的前驱
    bool NextElem(int* currentElem, int* nextElem);//获取元素的后继
    void ListTraverse(); //遍历线性表
    bool ListInsert(int i,int* e);//插入
    bool ListDelete(int i,int* e);//删除
};

list.cpp

#define _CRT_SECURE_N0_WARNINGS 1
//线性表 
//线性表是n个数据元素的有限数列。
//分为1.顺序表(数组)   2. 链表(静态链表 单链表 循环链表 双向链表)
//应用场景 通讯录 一元多项式 

//顺序表
/*
  线性表——顺序表

  3 5 7 2 9 1 8

  前驱 后继

*/

/*
创建线性表                                 销毁

C语言                                      
BOOL InitList(List** list)                 void  DestoryList(List *list)

C++ 
构造函数                                   析构函数

*/

//要求:建表 清空 插入 删除 查找
#include"list.h"

List::List(int size)
{
    m_iSize = size;
    m_pList = new int[m_iSize];//分配内存 线性表的容量
    m_iLength = 0;//初始情况下 元素为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 *e)  
{
    if (i < 0 || i >= m_iSize)
        return false;
    *e=m_pList[i];
    return true;
}
int List::LocateElem(int *e)
{
    for (int i = 0; i < m_iLength; i++)
    {
        if (m_pList[i] == *e)
        {
            return i; //找到了
        }
    } 
        return -1;
}
bool List::PriorElem(int* currentElem, int* preElem)
{
    int temp=LocateElem(currentElem);//找到当前元素的位置,依据位置判断是否具有前驱
    if (temp == -1)  //没找到当前元素
        return false;
    else             //找到了
    {
        if (temp == 0)  //找到当前元素为首元素 不存在前驱 返回错误值
            return false;
        else            //找到存在前驱的元素 下标-1 ——>前驱
        {
            *preElem = m_pList[temp - 1];
            return true;
        }
    }
}

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;
        }
    }
}
void List::ListTraverse()   
{
    for (int i = 0; i < m_iLength; i++)
        cout << m_pList[i] << endl;
}
bool List::ListInsert(int i, int* e)
{
    if (i < 0 || i > m_iLength)
    {
        return false;
    }

    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, int* 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;
}

test.cpp

#define _CRT_SECURE_N0_WARNINGS 1
#include<stdlib.h>
#include"list.h"

int main()
{
    //3 5 7 2 9 1 8
    int e1 = 3;
    int e2 = 5;
    int e3 = 7;
    int e4 = 2;
    int e5 = 9;
    int e6 = 1;
    int e7 = 8;
    int temp=0;
    //插入
    List *list1 = new List(10);
    list1->ListInsert(0, &e1);
    list1->ListInsert(1, &e2);
    list1->ListInsert(2, &e3);
    list1->ListInsert(3, &e4);
    list1->ListInsert(4, &e5);
    list1->ListInsert(5, &e6);
    list1->ListInsert(6, &e7);

    list1->ListTraverse();

    list1->GetElem(0, &temp);
    cout << "temp:" << temp << endl; 
    temp = 5;
    cout << list1->LocateElem(&temp) << endl;

    list1->PriorElem(&e3, &temp);  //前驱
    cout << "temp:" << temp << endl;

    list1->NextElem(&e3, &temp);   //后继
    cout << "temp:" << temp << endl;

    delete list1;
    system("pause");
    return 0;
}
运行结果: 线性表_顺序表.png

相关文章

  • 数据结构之线性表

    1、线性表-顺序表线性表-顺序表

  • 顺序表和链表的区别

    参考:线性表和链表的区别 注:参考文中的‘线性表’准确的说应该是’顺序表‘,链表与顺序表都是线性表。 顺序表:顺序...

  • 数据结构03-线性表之顺序表

    第三章 线性表之顺序表 第三章 线性表之顺序表一、什么是线性表?1> 概念2> 线性表的基本操作二、线性表的顺序存...

  • 数据结构之线性表的链式存储结构

    之前写了线性表的顺序存储结构和有序线性表的顺序存储结构,今天接着写线性表的链式存储结构 数据结构之线性表的顺序存储...

  • 数据结构-双向链表

    (一)什么是链表? 链表是线性表的一种,所谓的线性表包含顺序线性表和链表,顺序线性表是用数组实现的,在内存中有顺序...

  • 记录十一 线性表的链式存储结构

    前言 在前面记录八 线性表的顺序存储结构和记录九 线性表的顺序存储结构扩展(动态顺序表)中我们了解到线性表的顺序存...

  • 线性表及应用

    线性表 “线性表(List):零个或多个数据元素的有限序列。” 线性表的顺序存储结构 线性表的顺序存储结构,指的是...

  • 线性链表

    线性链表 线性表的顺序存储结构:顺序表线性表的链式存储结构:线性链表 线性表的链式存储所占存储空间大于顺序存储。 ...

  • 数据结构的标准形式(C、Python版本):1.顺序表

    一:C语言版本 顺序表基本操作 顺序表的定义/*****InitSize 线性表长度MaxSize 线性表允许的...

  • 数据结构之有序线性表的链式存储结构

    之前写了线性表的顺序存储结构和有序线性表的顺序存储结构以及线性表的链式存储结构,今天接着写有序线性表的链式存储结 ...

网友评论

    本文标题:线性表--顺序表

    本文链接:https://www.haomeiwen.com/subject/dxfeeftx.html