線性表--順序表(C++)

list.h

#include <iostream>
using namespace std;

class List{
private:
    int *m_pList;//指針指向線性表
    int m_iSize;//線性表的大小
    int m_iLength; //線性表存儲的元素
public:
    List(int size);   //構(gòu)造函數(shù)
    ~List(); //默認傳遞this指針
    void ClearList();//清空當前線性表里的元素,不等于釋放線性表的內(nèi)存
    bool ListEmpty(); //判斷線性表是否為空
    int ListLength(); // 獲取線性表的長度
    bool GetElem(int i,int *e); //獲取指定的元素
    int LocateElem(int *e); //獲取指定元素的位置
    bool PriorElem(int* currentElem, int* preElem);//獲取元素的前驅(qū)
    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個數(shù)據(jù)元素的有限數(shù)列。
//分為1.順序表(數(shù)組)   2. 鏈表(靜態(tài)鏈表 單鏈表 循環(huán)鏈表 雙向鏈表)
//應用場景 通訊錄 一元多項式 

//順序表
/*
  線性表——順序表

  3 5 7 2 9 1 8

  前驅(qū) 后繼

*/

/*
創(chuàng)建線性表                                 銷毀

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

C++ 
構(gòu)造函數(shù)                                   析構(gòu)函數(shù)

*/

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

List::List(int size)
{
    m_iSize = size;
    m_pList = new int[m_iSize];//分配內(nèi)存 線性表的容量
    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);//找到當前元素的位置,依據(jù)位置判斷是否具有前驅(qū)
    if (temp == -1)  //沒找到當前元素
        return false;
    else             //找到了
    {
        if (temp == 0)  //找到當前元素為首元素 不存在前驅(qū) 返回錯誤值
            return false;
        else            //找到存在前驅(qū)的元素 下標-1 ——>前驅(qū)
        {
            *preElem = m_pList[temp - 1];
            return true;
        }
    }
}

bool List::NextElem(int* currentElem, int* nextElem)
{
    int temp = LocateElem(currentElem);//找到當前元素的位置,依據(jù)位置判斷是否具有后繼
    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);  //前驅(qū)
    cout << "temp:" << temp << endl;

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

    delete list1;
    system("pause");
    return 0;
}

運行結(jié)果:
線性表_順序表.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,202評論 0 3
  • 2017年5月17日,碩碩閱讀第二十四天,今天碩碩閱讀的是《龜兔賽跑》的故事,讓碩碩明白了,做任何事情不論能力強弱...
    AAmily閱讀 245評論 0 0

友情鏈接更多精彩內(nèi)容