欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

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

C語言

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

C++順序表的實(shí)例代碼

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

本文實(shí)例為大家分享了C++實(shí)現(xiàn)順序表的具體代碼,供大家參考,具體內(nèi)容如下

#include <iostream> 
using namespace std; 
 
typedef int DataType; 
 
class SeqList 
{ 
public: 
  SeqList() 
    :_a(NULL) 
    , _size(0) 
    , _capacity(0) 
  {} 
 
  SeqList(const SeqList& s) 
    :_a(new DataType[s._size]) 
    , _size(s._size) 
    , _capacity(s._capacity) 
  { 
    memcpy(_a, s._a, sizeof(DataType)*s._size); 
  } 
 
  SeqList& operator=(const SeqList& s) 
  { 
    if (this != &s) 
    { 
      DataType* tmp = new DataType[s._size]; 
      delete[] _a; 
      _a = tmp; 
      memcpy(_a, s._a, sizeof(DataType)*s._size); 
      _size = s._size; 
      _capacity = s._capacity; 
    } 
     
    return *this; 
  } 
 
  //SeqList& operator=(SeqList s)  //若傳引用會(huì)改變引用對(duì)象的值 
  //{ 
  // swap(_a, s._a); 
  // swap(_size, s._size); 
  // swap(_capacity, s._capacity); 
 
  // return *this; 
  //} 
 
  ~SeqList() 
  { 
    if (_a) 
    { 
      delete[] _a; 
    } 
  } 
 
  void PushBack(DataType d) 
  { 
    CheckCapacity(); 
    _a[_size] = d; 
    _size++; 
  } 
 
  void PopBack() 
  { 
    if (_size > 0) 
    { 
      _size--; 
    } 
    else 
    { 
      cout << "順序表為空" << endl; 
    } 
  } 
 
  void PushFront(DataType d) 
  { 
    CheckCapacity(); 
 
    int i = (int)_size; 
    for (; i > 0; i--) 
    { 
      _a[i] = _a[i - 1]; 
    } 
    _a[0] = d; 
    ++_size; 
  } 
 
  void PopFront() 
  { 
    if (_size > 0) 
    { 
      int i = 0; 
      for (; i < (int)_size; i++) 
      { 
        _a[i] = _a[i + 1]; 
      } 
      _size--; 
    } 
    else 
    { 
      cout << "順序表為空" << endl; 
    } 
  } 
 
  void Print() 
  { 
    if (_size > 0) 
    { 
      int i = 0; 
      for (; i < (int)_size; i++) 
      { 
        cout << _a[i] << " "; 
      } 
      cout << endl; 
    } 
    else 
    { 
      cout << "順序表為空" << endl; 
    } 
  } 
 
  void Insert(size_t pos, DataType d) //在pos之前插入一個(gè)數(shù)據(jù) 
  { 
    CheckCapacity(); 
 
    if (_size > 0) 
    { 
      if (pos <= 0 || pos > _size)  
      { 
        cout << "pos位置非法" << endl; 
      } 
      else 
      { 
        int i = 0; 
        for (i = (int)_size + 1; i > pos - 1; i--) 
        { 
          _a[i] = _a[i - 1]; 
        } 
        _a[pos - 1] = d; 
        _size++; 
      } 
    } 
    else 
    { 
      PushFront(d); 
    } 
     
  } 
 
  void Erase(size_t pos) //刪除pos位置的數(shù)據(jù) 
  { 
    if (_size > 0) 
    { 
      if (pos <= 0 || pos > _size) 
      { 
        cout << "pos位置非法" << endl; 
      } 
      else 
      { 
        int i = pos - 1; 
        for (; i < (int)_size; i++) 
        { 
          _a[i] = _a[i + 1]; 
        } 
        _size--; 
      } 
    } 
    else 
    { 
      cout << "順序表為空,無法進(jìn)行刪除" << endl; 
    } 
  } 
 
  int Find(DataType d) 
  { 
    int i = 0; 
     
    for (; i < (int)_size; i++) 
    { 
      if (_a[i] == d) 
      { 
        return i + 1; 
      } 
    } 
    return 0; 
  } 
 
private: 
  void CheckCapacity() 
  { 
    if (_size == _capacity) 
    { 
      _capacity = _capacity * 2 + 3; 
      _a = (DataType*)realloc(_a, sizeof(DataType)*_capacity); 
    } 
  } 
 
private: 
  DataType* _a; 
  size_t _size; 
  size_t _capacity; 
}; 

以下為測(cè)試函數(shù)

#include "SeqList.h"; 
 
void Test1() 
{ 
  SeqList s1; 
  s1.PushBack(1); 
  s1.PushBack(2); 
  s1.PushBack(3); 
  s1.PushBack(4); 
  s1.Print(); 
  SeqList s2(s1); 
  s2.Print(); 
  s2.PopBack(); 
  s2.PopBack(); 
  s2.PopBack(); 
  s2.PopBack(); 
  s2.PopBack(); 
  s2.Print(); 
  s2.PushFront(4); 
  s2.PushFront(3); 
  s2.PushFront(2); 
  s2.PushFront(1); 
  s2.Print(); 
  s2.PopFront(); 
  s2.Print(); 
  s2.PopFront(); 
  s2.PopFront(); 
  s2.PopFront(); 
  s2.PopFront(); 
  s2.PopFront(); 
  SeqList s3; 
  s3 = s1; 
  s3.Print(); 
} 
 
void Test2() 
{ 
  SeqList s1; 
  s1.PushBack(1); 
  s1.PushBack(2); 
  s1.PushBack(3); 
  s1.PushBack(4); 
  s1.Print(); 
 
  //s1.Insert(1, 0); 
  //s1.Print(); 
 
  /*s1.Erase(1); 
  s1.Erase(1); 
  s1.Erase(1); 
  s1.Erase(1); 
  s1.Print();*/ 
 
  int i = s1.Find(5); 
  cout << i << endl; 
} 
 
int main() 
{ 
  //Test1(); 
  Test2(); 
 
  system("pause"); 
  return 0; 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:c語言實(shí)現(xiàn)基數(shù)排序解析及代碼示例

欄    目:C語言

下一篇:利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法示例

本文標(biāo)題:C++順序表的實(shí)例代碼

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1017.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(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)所有