欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用C++簡單實現順序表和單鏈表的示例代碼

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

本文主要給大家介紹了關于C++實現順序表和單鏈表的相關內容,分享出來供大家參考學習,話不多說,來一起看看詳細的介紹:

一、順序表示例代碼:

#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);
 }

 //[]運算符重載
 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的實現
  if (_size > 0)
  {
   if (pos == 0)
   {
    int end = 0;
    while (end < (int)_size - 1)
    {
     _array[end] = _array[end + 1];
     end++;
    }
    _size--;
   }
   //popback的實現
   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;
};

二、單鏈表(不含頭結點)示例代碼

#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é)點在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;
};

總結

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

相關文章

  • c++ const引用與非const引用介紹

    c++ const引用與非const引用介紹

    const引用是指向const對象的引用,可以讀取ref,但不能修改所以也就有將const變量賦值給非const引用是非法的,感興趣的朋友可以了解下,或許本文對你有所幫助
    2013-01-01
  • OpenCV圖像文件批量讀取編程實例

    OpenCV圖像文件批量讀取編程實例

    這篇文章主要為大家詳細介紹了OpenCV圖像文件批量讀取編程實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 《C++ Primer》隱式類類型轉換學習整理

    《C++ Primer》隱式類類型轉換學習整理

    在本篇文章里小編給大家整理的是關于《C++ Primer》隱式類類型轉換學習筆記內容,需要的朋友們參考下。
    2020-02-02
  • C++實現簡單版通訊錄管理系統

    C++實現簡單版通訊錄管理系統

    這篇文章主要為大家詳細介紹了C++實現簡單版通訊錄管理系統,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語言實現簡易的掃雷游戲

    C語言實現簡易的掃雷游戲

    這篇文章主要為大家詳細介紹了C語言實現簡易的掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Sersync+Rsync實現觸發(fā)式文件同步實戰(zhàn)過程

    Sersync+Rsync實現觸發(fā)式文件同步實戰(zhàn)過程

    sersync是使用c++編寫,而且對linux系統文 件系統產生的臨時文件和重復的文件操作進行過濾。下面通過本文給大家分享Sersync+Rsync實現觸發(fā)式文件同步實戰(zhàn)過程,需要的朋友參考下吧
    2017-09-09
  • C++中的操作符重載詳細解析

    C++中的操作符重載詳細解析

    運算符重載后不能改變運算符的操作對象(操作數)的個數;如:"+"是實現兩個操作數的運算符,重載后仍然為雙目運算符
    2013-09-09
  • C語言數據結構系列隊列篇

    C語言數據結構系列隊列篇

    本章我們將學習 "隊列" ,首先介紹隊列的概念和結構,然后我們將著重講解棧的實現。我們從零開始寫隊列的接口,并從零開始步步解讀。本章將繼續(xù)鞏固畫思路草圖的能力,只要思路草圖畫好了,就可以很輕松地將其轉換成代碼
    2022-02-02
  • C++實現xml解析器示例詳解

    C++實現xml解析器示例詳解

    這篇文章主要為大家介紹了C++實現xml解析器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 仿現代C++智能指針實現引用計數

    仿現代C++智能指針實現引用計數

    這篇文章主要為大家詳細介紹了如何仿現代C++智能指針實現引用計數,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以了解下
    2024-03-03

最新評論