Simple implementation of 7:c++ double-linked list based on algorithm and data structure

Source: Internet
Author: User

A doubly linked list adds a pointer to the previous node based on a single linked list.


DList.h

#include <cstdio> #include <cassert> #include <iostream>using namespace Std;class dlist{public://* * * Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/* * * * Dlist ();D list (const dlist &list);D list & operator= (const dlist &list); ~dlist ();//*************************************************************** Additions and deletions//**************************************************************************void PushFront ( const int& data); int popfront (); void pushback (const int& data); int popback (); void reverse (); unsigned short size ( ), bool IsEmpty (), void Printlist ();p rivate:void free ();p rivate:typedef struct node{node (int d):d ata (d), Next (NULL), Prev (NULL) {}int data;struct node* next;struct node* prev;} Node; node* M_head; node* m_tail;/* This variable adds a bit of chicken, originally wanted to use to manipulate the end of the chain, found that only when the insertion can be used, delete the time is not used, because there is no previous pointer */unsigned short m_size;};/ / *****************************Private method//********************************************************** void Dlist::free () {if (m_head) {node* tmp = Null;while (m_head) {tmp = M_head;m_head = m_head->next; Delete tmp;}} M_head = Null;m_tail = Null;m_size = 0;} Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/ /**************************************************************************dlist::D list () {m_head = NULL;m_tail = null;m_size = 0;} Dlist::D list (const dlist &list) {m_head = Null;m_tail = Null;m_size = List.m_size;if (list.m_head) {node* tmp = List.m_h ead node* q = null;while (tmp) {node* node = new node (tmp->data), if (!m_head) {m_head = Node;q = M_head;} Else{node->prev = Q;q->next = node;q = node;} TMP = Tmp->next;} M_tail = q; }else{m_head = Null;m_tail = NULL;}} dlist& dlist::operator= (const dlist &list) {free (); m_size = List.m_size;if (list.m_head) {node* tmp = List.m_head ; node* q = NULL;while (TMP) {node* node = new node (tmp->data); if (!m_head) {m_head = Node;q = M_head;} Else{node->prev = Q;q->next = node;q = node;} TMP = Tmp->next;} M_tail = q; }else{m_head = Null;m_tail = NULL;} return *this;} Dlist::~dlist () {if (m_head) {node* TMP = M_head;while (tmp) {m_head = M_head->next;delete tmp;tmp = M_head;}} M_head = Null;m_tail = NULL;} Additions/deletions//*************************** void Dlist::P ushfront (const int& data) {node* TMP = new Node (data); if (!m_head) {m_head = Tmp;m_tail = tmp;} Else{tmp->next = M_head;m_head->prev = Tmp;m_head = tmp;} ++m_size;} int dlist::P opfront () {assert (M_size > 0); node* tmp = M_head;m_head = M_head->next;m_head->prev = Null;int val = Tmp->data;delete tmp;--m_size;return val;} void Dlist::P ushback (const int& data) {node* TMP = new Node (data); Tmp->prev = M_tail;m_head? (M_tail->next = tmp): (m_heAD = tmp); m_tail = Tmp;++m_size;} int dlist::P opback () {assert (M_size > 0); int val = 0; Node *pre = m_tail->prev;if (!pre) {val = m_tail->data;delete m_tail;m_head = null;m_tail = Null;m_size = 0;} Else{delete M_tail;m_tail = Pre;m_tail->next = null;--m_size;} return Val;} void Dlist::reverse () {//list is empty or only one element, return directly if (!m_head | |!m_head->next) {return;}  node* pre = M_head; node* cur = m_head->next;  node* next = null;while (cur) {next = cur->next;  Cur->next = Pre;pre->prev = Cur;pre = Cur;cur = Next;} Pre->prev = Null;m_head->next = Null;m_tail = M_head;m_head = pre;} unsigned short dlist::size () {return m_size;} BOOL Dlist::isempty () {return (m_size = = 0);}  void Dlist::p rintlist () {cout << "size:" << m_size << ", Content:" << endl;cout << "from Head: "; if (m_head) {node* TMP = M_head;while (tmp) {cout << tmp->data <<"; tmp = Tmp->next;}} cout << "NULL" << endl;cout << "from Tail:"; if (M_taIL) {node* tmp = M_tail;while (tmp) {cout << tmp->data << "; tmp = Tmp->prev;}} cout << "NULL" << Endl;}



Main.cpp

Test for double list#include "DList.h" #include <cstdlib> #include <iostream>using namespace Std;int main () {dlist list;for (int i = 1; i < 5; ++i) {list. Pushfront (i);} List.printlist (); list. Pushback (0); List.printlist ();D list listcopy (list), listcopy.printlist ();D list listassig;listassig = list; Listassig.printlist (); int popfront = list. Popfront (); cout << "Popfront:" << popfront << endl;list.printlist (); int popback = list. Popback (); cout << "Popback:" << popback << endl;list.printlist (); List.reverse (); cout << "Do Reverse "<< endl;list.printlist (); System (" pause "); return 0;}



Output


Simple implementation of 7:c++ double-linked list based on algorithm and data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.