C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)
更新時(shí)間: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)幾個(gè)核心的功能,語(yǔ)言是C++,如果有更好的想法和意見(jiàn),歡迎留言~~~
/* 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)前最大長(zhǎng)度 }; SeqList :: SeqList(int size) { //size不可以小于零,也不可以超過(guò)系統(tǒng)規(guī)定最大長(zhǎng)度 //否則做截?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ì)本站的支持!
相關(guān)文章
掌握C++:揭秘寫時(shí)拷貝與淺深拷貝之間的關(guān)系
探索C++的奧秘,本指南將揭秘寫時(shí)拷貝與淺深拷貝之間的微妙關(guān)系,摸索這些復(fù)雜概念背后的邏輯,讓你的編程技能瞬間提升,來(lái)吧,讓我們一起進(jìn)入這個(gè)引人入勝的C++世界!2024-01-01C語(yǔ)言實(shí)現(xiàn)隊(duì)列的示例詳解
隊(duì)列是一種特殊的線性表,特殊之處在于它只允許在表的前端(head)進(jìn)行刪除操作,而在表的后端(tail)進(jìn)行插入操作。本文將用C語(yǔ)言實(shí)現(xiàn)隊(duì)列,感興趣的可以了解一下2022-06-06C++ LeetCode1812判斷國(guó)際象棋棋盤格子顏色
這篇文章主要為大家介紹了C++ LeetCode1812判斷國(guó)際象棋棋盤格子顏色, 有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12C++?實(shí)現(xiàn)單鏈表創(chuàng)建、插入和刪除
這篇文章主要介紹了C++?實(shí)現(xiàn)單鏈表創(chuàng)建、插入和刪除方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07