옵션 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #ifndef __DNode_H__ #define __DNode_H__ #include <iostream> #include <string> using namespace std; typedef string Elem; class DNode { private: Elem elem; DNode* prev; DNode* next; friend class DLinkedList; }; #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include "DNode.h" class DLinkedList { public: DLinkedList(); ~DLinkedList(); bool empty() const; const Elem& front() const; const Elem& back() const; void addFront(const Elem& e); void addBack(const Elem& e); void removeFront(); void removeBack(); private: DNode* header; DNode* trailer; protected: void add(DNode* v, const Elem& e); void remove(DNode* v); }; | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include "DLinkedList.h" DLinkedList::DLinkedList() { header = new DNode; trailer = new DNode; header->next = trailer; trailer->prev = header; } DLinkedList::~DLinkedList() { while (!empty()) removeFront(); delete header; delete trailer; } bool DLinkedList::empty() const { return header->next == trailer; } const Elem& DLinkedList::front() const { return header->next->elem; } const Elem& DLinkedList::back() const { return trailer->prev->elem; } void DLinkedList::add(DNode* v, const Elem& e) { DNode* u = new DNode; u->elem = e; u->next = v; u->prev = v->prev; v->prev->next = v->prev = u; } void DLinkedList::addFront(const Elem& e) { add(header->next, e); } void DLinkedList::addBack(const Elem& e) { add(trailer, e); } void DLinkedList::remove(DNode* v) { DNode* u = v->prev; DNode* w = v->next; u->next = w; w->prev = u; delete v; } void DLinkedList::removeFront() { remove(header->next); } void DLinkedList::removeBack() { remove(trailer->prev); } | cs |