C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)
C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)
線性表的數(shù)組實(shí)現(xiàn),實(shí)現(xiàn)幾個(gè)核心的功能,語言是C++,如果有更好的想法和意見,歡迎留言~~~
/* Author : Moyiii
* 線性表的數(shù)組實(shí)現(xiàn),僅作學(xué)習(xí)之用,當(dāng)然如果
* 你想拿去用,隨你好啦。
*/
#include<iostream>
using namespace std;
//順序表
class SeqList
{
public:
//構(gòu)造函數(shù),接受一個(gè)默認(rèn)的列表大小
SeqList(int size = MAX_LIST_SIZE);
//析構(gòu)函數(shù),釋放elems占用的內(nèi)存空間
~SeqList();
//清空表
void clear();
//判斷表是否為空
bool isEmpty();
//獲得表的當(dāng)前元素個(gè)數(shù)
int getLength();
//在第pos個(gè)元素位置之前插入一個(gè)新元素
bool insertElem(int pos, int elem);
//刪除第pos個(gè)元素
bool deleteElem(int pos);
//打印表中元素
void print();
int *elems;//表元素,
private:
static const int MAX_LIST_SIZE;
int m_length;//表的元素個(gè)數(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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:C++基于隨機(jī)數(shù)實(shí)現(xiàn)福彩雙色球的方法示例
欄 目:C語言
下一篇:C語言數(shù)據(jù)結(jié)構(gòu)之雙向循環(huán)鏈表的實(shí)例
本文標(biāo)題:C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1417.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)- 解析最少換車次數(shù)的問題詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


