• 双头链表的实现


    • // List.h
    • #ifndef SSCPP2014_DOULIST_A_H
    • #define SSCPP2014_DOULIST_A_H
    •  
    • #include<string>
    •  
    • structDouListNode{
    • int elem;
    • DouListNode*prev,*next;
    • DouListNode(int e =0,DouListNode*p =0,DouListNode*n =0){
    • elem = e;
    • prev = p;
    • next = n;
    • }
    • };
    •  
    • classDouList{
    • private:
    • DouListNode*m_head,*m_tail;
    • public:
    • DouList();
    • DouList(constDouList&src);
    • ~DouList();
    • void clear();
    • bool empty()const;
    • std::string to_str()const;
    • int front()const;
    • int back()const;
    • void push_front(constint&e);
    • void push_back(constint&e);
    • void pop_front();
    • void pop_back();
    • voidoperator=(constDouList&other);
    • friend std::ostream&operator<<(std::ostream &out,
    • constDouList&list);
    • // non-meaning static value
    • staticint _error_sign;// for illegal front()/back()
    • };
    •  
    • #endif
    •  
    •  
    •  
    •  
    •  
    •  
    • // List.cpp
    • #include<iostream>
      #include<string>
      #include "DouList.h"
      using namespace std;

      DouList::DouList() {
      m_head = m_tail = NULL;
      }

      DouList::DouList(const DouList &src) {
      m_head = m_tail = NULL;
      *this = src;
      }

      DouList::~DouList() {
      clear();
      }

      void DouList::clear() {
      DouListNode *temp;
      while (m_head != NULL) {
      temp = m_head;
      m_head = m_head->next;
      delete temp;
      }
      m_head = m_tail = NULL;
      }

      bool DouList::empty() const {
      return (m_head == NULL && m_tail == NULL);
      }

      std::string DouList::to_str() const {
      DouListNode *t = m_head;
      string strtemp;
      strtemp += "[";
      if (m_head == NULL) {
      } else if (m_head == m_tail) {
      int i = 0, x = t->elem, ch[10] = {0};
      if (x == 0) {
      strtemp.push_back('0');
      } else {
      while (x) {
      ch[i] = x % 10;
      x /= 10;
      i++;
      }
      for (int j = i-1; j >= 0; j--) {
      strtemp.push_back(ch[j] + '0');
      }
      }
      } else {
      while (t->next != NULL) {
      int i = 0, x = t->elem, ch[10] = {0};
      if (x == 0) {
      strtemp.push_back('0');
      strtemp += ", ";
      t = t->next;
      } else {
      while (x) {
      ch[i] = x % 10;
      x /= 10;
      i++;
      }
      for (int j = i-1; j >= 0; j--) {
      strtemp.push_back(ch[j] + '0');
      }
      strtemp += ", ";
      t = t->next;
      }
      }
      int i = 0, x = t->elem, ch[10] = {0};
      if (x == 0) {
      strtemp.push_back('0');
      } else {
      while (x) {
      ch[i] = x % 10;
      x /= 10;
      i++;
      }
      for (int j = i-1; j >= 0; j--) {
      strtemp.push_back(ch[j] + '0');
      }
      }
      }
      strtemp += "]";
      return strtemp;
      }

      int DouList::front() const {
      return m_head->elem;
      }

      int DouList::back() const {
      return m_tail->elem;
      }

      void DouList::push_front(const int &e) {
      if (m_head == NULL && m_tail == NULL) {
      DouListNode *temp = new DouListNode(e);
      m_head = temp;
      m_tail = temp;
      } else {
      DouListNode *temp = new DouListNode(e);
      temp->next = m_head;
      temp->prev = NULL;
      m_head->prev = temp;
      m_head = temp;
      }
      }

      void DouList::push_back(const int &e) {
      if (m_head == NULL && m_tail == NULL) {
      DouListNode *temp = new DouListNode(e);
      m_head = temp;
      m_tail = temp;
      } else {
      DouListNode *temp = new DouListNode(e);
      temp->prev = m_tail;
      temp->next = NULL;
      m_tail->next = temp;
      m_tail = temp;
      }
      }

      void DouList::pop_front() {
      if (empty()) {
      return;
      }
      if (m_head == m_tail) {
      clear();
      return;
      } else {
      DouListNode *temp = m_head;
      m_head = m_head->next;
      m_head->prev = NULL;
      delete temp;
      }
      }

      void DouList::pop_back() {
      if (empty()) {
      return;
      }
      if (m_head == m_tail) {
      clear();
      return;
      } else {
      DouListNode *temp = m_tail;
      m_tail = m_tail->prev;
      m_tail->next = NULL;
      delete temp;
      }
      }

      void DouList::operator=(const DouList &other) {
      clear();
      DouListNode *temp2 = other.m_head;
      DouListNode *temp = m_head = new DouListNode(temp2->elem);
      temp2 = temp2->next;
      while (temp2 != NULL) {
      temp->next = new DouListNode(temp2->elem, temp);
      temp = temp->next;
      temp2 = temp2->next;
      }
      m_tail = temp;
      }

      std::ostream& operator<<(std::ostream &output, const DouList &list) {
      output << list.to_str();
      return output;
      }




  • 相关阅读:
    VS Visual Studio connection(); Microsoft Visulal Studio vNext & Azure
    无废话WCF入门教程六[一个简单的Demo]
    JS代码混淆 支持PHP .NET PERL
    C#/vbscript/JS如何加密保护HTML/javascript源代码
    JavaScript加密解密7种方法总结分析
    模型融合---Stacking调参总结
    模型融合---Xgboost调参总结
    模型融合---GBDT调参总结
    为什么正则化可以减小过拟合?
    python中的filter、map、reduce、apply用法
  • 原文地址:https://www.cnblogs.com/sysu-zhengwsh/p/3657384.html
Copyright © 2020-2023  润新知