最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用C++簡單實(shí)現(xiàn)順序表和單鏈表的示例代碼

 更新時間:2017年08月01日 11:32:23   作者:Suhw  
這篇文章主要給大家介紹了關(guān)于利用C++簡單實(shí)現(xiàn)順序表和單鏈表的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友可以參考借鑒,下面來跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。

本文主要給大家介紹了關(guān)于C++實(shí)現(xiàn)順序表和單鏈表的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說,來一起看看詳細(xì)的介紹:

一、順序表示例代碼:

#include <assert.h>
#include <iostream>
using namespace std;

typedef int Datatype;

class SeqList
{
public:
 SeqList()
  :_array(NULL)
  ,_size(0)
  ,_capacity(0)
 {
 }

 SeqList(const SeqList& s)
 {
  _array = (Datatype*)malloc(s._size*(sizeof(Datatype)));
  memcpy(_array, s._array, s._size*(sizeof(Datatype)));
  _size = _capacity = s._size;
 }

 SeqList& operator=(SeqList& s)
 {
  free(_array);
  Swap(s);
  return *this;
 }

 void Swap(SeqList& s)
 {
  _array = s._array;
  _size = s._size;
  _capacity = s._capacity;
 }

 ~SeqList()
 {
  if (_array)
  {
   free(_array);
   _array = NULL;
   _size = _capacity = 0;
  }
 }

 void Print()
 {
  for (size_t i = 0; i < _size; i++)
  {
   cout << _array[i] << " ";
  }
  cout << endl;
 }

 void CheckCapcacity()
 {
  if (_size == _capacity)
  {
   _capacity = 2 * _capacity + 3;
   _array = (Datatype*)realloc(_array, _capacity*sizeof(Datatype));
   assert(_array);
  }
 }

 //后插
 void PushBack(Datatype x)
 {
  Insert(_size, x);
 }

 //前插
 void PushFront(Datatype x)
 {
  Insert(0, x);
 }

 //刪除最后一個
 void PopBack()
 {
  Erase(_size);
 }

 //刪除第一個
 void PopFront()
 {
  Erase(0);
 }

 //[]運(yùn)算符重載
 Datatype& operator[](size_t pos)
 {
  assert(pos < _size);
  return _array[pos];
 }

 //pos位置前插入x
 void Insert(size_t pos, Datatype x)
 {
  assert(pos <= _size);
  CheckCapcacity();
  int end = (int)_size - 1;
  if (pos == 0)
  {
   while (end >= 0)
   {
    _array[end + 1] = _array[end];
    end--;
   }
   _array[0] = x;
  }
  else
  {
   while (end >= (int)pos)
   {
    _array[end + 1] = _array[end];
    end--;
   }
   _array[pos] = x;
  }
  _size++;
 }

 //刪除pos位置的元素
 void Erase(size_t pos)
 {
  assert(pos < _size);
  //popfront的實(shí)現(xiàn)
  if (_size > 0)
  {
   if (pos == 0)
   {
    int end = 0;
    while (end < (int)_size - 1)
    {
     _array[end] = _array[end + 1];
     end++;
    }
    _size--;
   }
   //popback的實(shí)現(xiàn)
   else if (pos == _size)
   {
    _size--;
   }
   //erase
   else
   {
    int end = pos;
    while (end < (int)_size - 1)
    {
     _array[end] = _array[end + 1];
     end++;
    }
    _size--;
   }
  }
  return; 
 }

private:
 Datatype* _array;
 size_t _size;
 size_t _capacity;
};

二、單鏈表(不含頭結(jié)點(diǎn))示例代碼

#include <iostream>
#include <assert.h>
using namespace std;

typedef int DataType;

struct SListNode
{
 SListNode* _next;
 DataType _data;

 SListNode(DataType x)
  :_data(x)
  , _next(NULL)
 {}
};

typedef SListNode Node;

class SList
{
public:
 SList()
  :_head(NULL)
  , _tail(NULL)
 {}

 SList(const SList& s)
  :_head(NULL)
  ,_tail(NULL)
 {
  Copy(s);
 }

 SList& operator=(const SList& s)
 {
  Destroy();
  Copy(s);
  return *this;
 }

 ~SList()
 {
  Destroy();
 }


 void Copy(const SList& s)
 {
  Node* cur = s._head;
  while (cur)
  {
   PushBack(cur->_data);
   cur = cur->_next;
  }
 }

 void Destroy()
 {
  Node* cur = _head;
  while (_head != NULL)
  {
   cur = _head;
   _head = cur->_next;
   delete cur;
  }
  _head = _tail = NULL;
 }

 void PushBack(DataType x)
 {
  if ((_head == NULL)&&(_tail == NULL))
  {
   _head = _tail = new Node(x);
  }
  else
  {
   _tail->_next = new Node(x);
   _tail = _tail->_next;
  }
 }

 void PopBack()
 {
  if (_head == NULL)
  {
   return;
  }
  else if (_head ->_next == NULL)
  {
   delete _head;
   _head = _tail = NULL;
  }
  else
  {
   Node* tmp = _head;
   while (tmp->_next->_next != NULL)
   {
    tmp = tmp->_next;
   }
   _tail = tmp;
   tmp->_next = NULL;
  }  
 }

 void PushFront(DataType x)
 {
  if ((_head == NULL) && (_tail == NULL))
  {
   _head = _tail = new Node(x);
  }
  else
  {
   Node* tmp = new Node(x);
   tmp->_next = _head;
   _head = tmp;
  }
 }

 void PopFront()
 {
  if (_head == NULL)
  {
   return;
  }
  Node* cur = _head;
  _head = _head->_next;
  delete cur;
 }

 Node* Find(DataType x)
 {
  Node* tmp = _head;
  while (tmp)
  {
   if (tmp->_data == x)
    return tmp;
   tmp = tmp->_next;
  }
  return NULL;
 }

 // 插入一個節(jié)點(diǎn)在pos的前面
 void Insert(Node* pos, DataType x)
 {
  assert(pos);
  if (pos == 0)
  {
   PushFront(x);
  }
  else
  {
   Node* cur = _head;
   while (cur->_next != pos)
   {
    cur = cur->_next;
   }
   Node* tmp = new Node(x);
   tmp->_next = pos;
   cur->_next = tmp;
  }
 }

 void Erase(Node* pos)
 {
  assert(pos);
  if (pos == 0)
  {
   PopFront();
  }
  else if (pos->_next == NULL)
  {
   PopBack();
  }
  else
  {
   Node* cur = _head;
   while (cur->_next != pos)
   {
    cur = cur->_next;
   }
   Node* tmp = cur->_next;
   cur->_next = tmp->_next;
   delete tmp;
  }
 }

 void Print()
 {
  Node* tmp = _head;
  while (tmp != NULL)
  {
   cout <<tmp->_data << "->";
   tmp= tmp->_next;
  }
  cout <<"NULL"<<endl;
 }

private:
 Node* _head;
 Node* _tail;
};

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持

相關(guān)文章

  • C++的sstream標(biāo)準(zhǔn)庫詳細(xì)介紹

    C++的sstream標(biāo)準(zhǔn)庫詳細(xì)介紹

    以下是對C++中的的sstream標(biāo)準(zhǔn)庫進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下
    2013-09-09
  • C語言封裝函數(shù)字符串練習(xí)匯總分享

    C語言封裝函數(shù)字符串練習(xí)匯總分享

    這篇文章主要介紹了C語言封裝函數(shù)字符串練習(xí)匯總分享,分享內(nèi)容有字符串查找、字符串拼接、字符串轉(zhuǎn)整數(shù)等內(nèi)容,需要而小伙伴可以參考一下
    2022-03-03
  • C++文件讀寫代碼分享

    C++文件讀寫代碼分享

    本文給大家分享的是2個C++實(shí)現(xiàn)文件讀寫的代碼,都非常的簡單實(shí)用,有需要的小伙伴可以參考下。
    2015-07-07
  • C語言超詳細(xì)講解循環(huán)與分支語句基礎(chǔ)

    C語言超詳細(xì)講解循環(huán)與分支語句基礎(chǔ)

    各位小伙伴們,今天給大家?guī)淼氖茄h(huán)與分支語句,本篇將會向大家介紹這些語句的格式和使用的基本方法,感興趣的朋友來看看吧
    2022-04-04
  • opencv3/C++ 實(shí)現(xiàn)SURF特征檢測

    opencv3/C++ 實(shí)現(xiàn)SURF特征檢測

    今天小編就為大家分享一篇opencv3/C++ 實(shí)現(xiàn)SURF特征檢測,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • C語言中幾種常量的認(rèn)識和理解

    C語言中幾種常量的認(rèn)識和理解

    這篇文章主要為大家介紹了C語言常量的認(rèn)識和理解,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • C++實(shí)現(xiàn)FTP綜合應(yīng)用詳解

    C++實(shí)現(xiàn)FTP綜合應(yīng)用詳解

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)FTP綜合應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C語言實(shí)現(xiàn)socket簡單通信實(shí)例

    C語言實(shí)現(xiàn)socket簡單通信實(shí)例

    這篇文章主要介紹了C語言實(shí)現(xiàn)socket簡單通信的方法,是學(xué)習(xí)C語言網(wǎng)絡(luò)編程非?;A(chǔ)而又實(shí)用的實(shí)例,需要的朋友可以參考下
    2014-09-09
  • C++操作.json文件的超詳細(xì)新手教程

    C++操作.json文件的超詳細(xì)新手教程

    最近因?yàn)轫?xiàng)目原因需要解析JSON格式數(shù)據(jù),所以這篇文章主要給大家介紹了關(guān)于C++操作.json文件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • C++中實(shí)現(xiàn)子進(jìn)程執(zhí)行和管道通信詳解

    C++中實(shí)現(xiàn)子進(jìn)程執(zhí)行和管道通信詳解

    在這篇博客中,我們將深入探索如何在 C++ 程序中實(shí)現(xiàn)子進(jìn)程的創(chuàng)建與執(zhí)行,以及父子進(jìn)程間的管道通信,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01

最新評論

敖汉旗| 梓潼县| 阜城县| 舞阳县| 双鸭山市| 阿合奇县| 滦平县| 余庆县| 望都县| 岐山县| 许昌市| 家居| 临夏县| 虞城县| 密云县| 磐安县| 竹山县| 油尖旺区| 竹溪县| 丹凤县| 黎川县| 丽江市| 阿拉善右旗| 阿瓦提县| 九龙坡区| 克山县| 云梦县| 开远市| 乌拉特中旗| 廊坊市| 卓资县| 广南县| 南郑县| 丽江市| 石城县| 怀来县| 龙州县| 东乡县| 汕尾市| 莱西市| 鄂伦春自治旗|