• 双头链表的实现


    • // 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;
      }




  • 相关阅读:
    P3371 【模板】单源最短路径(弱化版)
    村村通
    P1551 亲戚题解
    P4467 [SCOI2007]k短路
    P2483 【模板】k短路([SDOI2010]魔法猪学院)
    POJ——2449 Remmarguts' Date
    P1337 [JSOI2004]平衡点 / 吊打XXX
    P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
    P1621 集合
    P1514 引水入城
  • 原文地址:https://www.cnblogs.com/sysu-zhengwsh/p/3657384.html
Copyright © 2020-2023  润新知