雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++ 數(shù)據(jù)結(jié)構(gòu)線性表-數(shù)組實(shí)現(xiàn)

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎ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

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有