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