利用C++實現(xiàn)雙鏈表基本接口示例代碼
鏈表
鏈表是一種物理存儲單元上非連續(xù)、非順序的存儲結構,數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序實現(xiàn)的。鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態(tài)生成。每個結點包括兩個部分:一個是存儲數(shù)據(jù)元素的數(shù)據(jù)域,另一個是存儲下一個結點地址的指針域。 相比于線性表順序結構,鏈表比較方便插入和刪除操作。
本文主要給大家介紹了關于C++實現(xiàn)雙鏈表基本接口的相關內容,分享出來供大家參考學習,話不多說,來一起看看詳細的介紹吧。
首先先簡單通過圖示區(qū)分單鏈表和雙鏈表的結構差異:
單鏈表的基本接口實現(xiàn)可參考:單鏈表簡單實現(xiàn)
接下來就是雙鏈表的基本接口實現(xiàn):
#include <iostream>
#include <assert.h>
using namespace std;
typedef int DataType;
struct ListNode
{
 ListNode* _next;
 ListNode* _prev;
 DataType _data;
 ListNode(DataType x)
  :_next(NULL)
  , _prev(NULL)
  , _data(x)
 {}
};
typedef ListNode Node;
class List
{
public:
 List()
  :_head(NULL)
  ,_tail(NULL)
 {}
 List(const List& l)
  :_head(NULL)
  ,_tail(NULL)
 {
  Copy(l);
 }
 void Copy(const List& l)
 {
  Node* cur = l._head;
  while (cur)
  {
   PushBack(cur->_data);
   cur = cur->_next;
  }
 }
 List& operator=(const List& l)
 {
  Destory();
  Copy(l);
  return *this;
 }
 ~List()
 {
  Destory();
 }
 void Destory()
 {
  if (_head)
  {
   Node* cur = _head;
   while (_head)
   {
    cur = _head;
    _head = _head->_next;
    delete cur;
   }
   _head = _tail = NULL;
  }
 }
 void PushBack(DataType x)
 {
  if (_head == NULL)
  {
   Node* tmp = new Node(x);
   tmp->_next = tmp->_prev = NULL;
   _head = _tail = tmp;
  }
  else
  {
   Node* tmp = new Node(x);
   _tail->_next = tmp;
   tmp->_prev = _tail;
   _tail = tmp;
  }
 }
 void PopBack()
 {
  if (_head == NULL)
  {
   return;
  }
  else if (_head->_next == NULL)
  {
   delete _head;
   _head = _tail = NULL;
  }
  else
  {
   Node* tmp = _tail;
   _tail = _tail->_prev;
   _tail->_next = NULL;
   delete tmp;
  }
 }
 void PushFront(DataType x)
 {
  if (_head == NULL)
  {
   _head = _tail = new Node(x);
  }
  else
  {
   Node* tmp = new Node(x);
   tmp->_next = _head;
   _head->_prev = tmp;
   _head = _head->_prev;
  }
 }
 void PopFront()
 {
  if (_head == NULL)
  {
   return;
  }
  else if (_head->_next == NULL)
  {
   delete _head;
   _head = _tail = NULL;
  }
  else
  {
   Node* tmp = _head;
   _head = _head->_next;
   delete tmp;
   _head->_prev = NULL;
  }
 }
 Node* Find(DataType x)
 {
  Node* cur = _head;
  while (cur)
  {
   if (cur->_data == x)
    return cur;
   cur = cur->_next;
  }
  return NULL;
 }
 // 在pos的前面插入x
 void Insert(Node* pos, DataType x)
 {
  assert(pos);
  if ((pos == 0) || (pos->_prev == NULL))
  {
   PushFront(x);
  }
  else
  {
   Node* font = pos->_prev;
   Node* tmp = new Node(x);
   tmp->_prev = font;
   tmp->_next = pos;
   font->_next = tmp;
   pos->_prev = tmp;
  }
 }
 //刪除pos位置的元素
 void Erase(Node* pos)
 {
  assert(pos);
  if ((pos == 0) || (pos->_prev == NULL))
  {
   PopFront();
  }
  else if (pos->_next == NULL)
  {
   PopBack();
  }
  else
  {
   Node* font = pos->_prev;
   Node* last = pos->_next;
   font->_next = last;
   last->_prev = font;
   delete pos;
  }
 }
 //逆序整個雙鏈表
 void Reverse()
 {
  Node* cur = _head;
  while (cur)
  {
   swap(cur->_next,cur->_prev);
   cur = cur->_prev;
  }
  swap(_head, _tail);
 }
 void Print()
 {
  Node* cur = _head;
  while (cur)
  {
   cout << cur->_data << "->";
   cur = cur->_next;
  }
  cout << "NULL" << endl;
 }
private:
 Node* _head;
 Node* _tail;
};
注:在一些操作實現(xiàn)時,一定要要考慮清楚各種情況,再進行情況的分類盡量提高代碼的復用程度。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對我們的支持
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
 - 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
 - 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
 - 01-10深入理解C++中常見的關鍵字含義
 - 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
 - 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
 - 01-10使用C++實現(xiàn)全排列算法的方法詳解
 - 01-10c++中inline的用法分析
 - 01-10用C++實現(xiàn)DBSCAN聚類算法
 - 01-10深入全排列算法及其實現(xiàn)方法
 


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
 
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開發(fā)環(huán)境設置
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10C#中split用法實例總結
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 


