C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)
更新時間:2017年06月23日 17:08:09 投稿:lqh
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)
線性表的數(shù)組實(shí)現(xiàn),實(shí)現(xiàn)幾個核心的功能,語言是C++,如果有更好的想法和意見,歡迎留言~~~
/* Author : Moyiii
* 線性表的數(shù)組實(shí)現(xiàn),僅作學(xué)習(xí)之用,當(dāng)然如果
* 你想拿去用,隨你好啦。
*/
#include<iostream>
using namespace std;
//順序表
class SeqList
{
public:
//構(gòu)造函數(shù),接受一個默認(rèn)的列表大小
SeqList(int size = MAX_LIST_SIZE);
//析構(gòu)函數(shù),釋放elems占用的內(nèi)存空間
~SeqList();
//清空表
void clear();
//判斷表是否為空
bool isEmpty();
//獲得表的當(dāng)前元素個數(shù)
int getLength();
//在第pos個元素位置之前插入一個新元素
bool insertElem(int pos, int elem);
//刪除第pos個元素
bool deleteElem(int pos);
//打印表中元素
void print();
int *elems;//表元素,
private:
static const int MAX_LIST_SIZE;
int m_length;//表的元素個數(shù)
int m_size;//表的當(dāng)前最大長度
};
SeqList :: SeqList(int size)
{
//size不可以小于零,也不可以超過系統(tǒng)規(guī)定最大長度
//否則做截?cái)嗵幚?
if(size > MAX_LIST_SIZE)
{
m_size = MAX_LIST_SIZE;
}
else if(size < 0)
{
m_size = 0;
}
else
{
m_size = size;
}
elems = new int[m_size];
m_length = 0;
if(!elems)
{
cout << "Space allocate failed!" << endl;
}
}
SeqList :: ~SeqList()
{
delete []elems;
}
void SeqList :: clear()
{
m_length = 0;
}
bool SeqList :: isEmpty()
{
if(m_length == 0)
{
return true;
}
else
{
return false;
}
}
int SeqList :: getLength()
{
return m_length;
}
bool SeqList :: insertElem(int pos, int elem)
{
if(m_length == m_size)
{
cout << "List is Full" << endl;
return false;
}
if(pos < 1 || pos > m_length + 1)
{
cout << "Over Bound!" << endl;
return false;
}
//插入位置之后元素后移
for(int i = m_length; i >= pos - 1; --i)
{
elems[i+1] = elems[i];
}
elems[pos-1] = elem;
m_length++;
return true;
}
bool SeqList :: deleteElem(int pos)
{
if(pos < 1 || pos > m_length)
{
return false;
}
for(int i = pos - 1; i <= m_length - 1; ++i)
{
elems[i] = elems[i+1];
}
m_length--;
return false;
}
void SeqList :: print()
{
for(int i = 0; i < m_length; ++i)
{
cout << elems[i] << " ";
}
cout << endl;
}
//初始化
const int SeqList :: MAX_LIST_SIZE = 100;
int main()
{
SeqList myList;
for(int i = 1; i <= 10; ++i)
{
myList.insertElem(1,i);
}
myList.print();
cout << "Length= " << myList.getLength() <<endl;
myList.deleteElem(5);
myList.print();
cout << "Length= " << myList.getLength() <<endl;
myList.clear();
cout << myList.isEmpty() << endl;
return 0;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(648.替換單詞)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(648.替換單詞),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C++實(shí)現(xiàn)LeetCode(13.羅馬數(shù)字轉(zhuǎn)化成整數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(13.羅馬數(shù)字轉(zhuǎn)化成整數(shù)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言錯誤信息報告strerror函數(shù)和perror函數(shù)詳解
這篇文章主要介紹了C語言錯誤信息報告strerror函數(shù)和perror函數(shù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
C/C++實(shí)現(xiàn)枚舉網(wǎng)上鄰居信息的示例詳解
在Windows系統(tǒng)中,通過網(wǎng)絡(luò)鄰居可以方便地查看本地網(wǎng)絡(luò)中的共享資源和計(jì)算機(jī),本文將介紹一個簡單的C++程序,使用Windows API枚舉網(wǎng)絡(luò)鄰居信息,并獲取對端名稱、本機(jī)名稱、主機(jī)名稱以及主機(jī)IP等信息,文中通過代碼示例給大家講解非詳細(xì),需要的朋友可以參考下2023-12-12
C++中string類的常用方法實(shí)例總結(jié)
string類是C++提供的抽象數(shù)據(jù)類型,其支持可變長字符串,下面這篇文章主要給大家總結(jié)介紹了關(guān)于C++中string類的常用方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03

