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

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

C語言

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

利用C++實現(xiàn)雙鏈表基本接口示例代碼

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

鏈表

鏈表是一種物理存儲單元上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。鏈表由一系列結(jié)點(鏈表中每一個元素稱為結(jié)點)組成,結(jié)點可以在運行時動態(tài)生成。每個結(jié)點包括兩個部分:一個是存儲數(shù)據(jù)元素的數(shù)據(jù)域,另一個是存儲下一個結(jié)點地址的指針域。 相比于線性表順序結(jié)構(gòu),鏈表比較方便插入和刪除操作。

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

首先先簡單通過圖示區(qū)分單鏈表和雙鏈表的結(jié)構(gòu)差異:

單鏈表的基本接口實現(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)時,一定要要考慮清楚各種情況,再進(jìn)行情況的分類盡量提高代碼的復(fù)用程度。

總結(jié)

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

上一篇:C++中的聚合類定義與用法分析

欄    目:C語言

下一篇:關(guān)于C++中菱形繼承和虛繼承的問題總結(jié)

本文標(biāo)題:利用C++實現(xiàn)雙鏈表基本接口示例代碼

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

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

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

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

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