root/DoublyLinkedList.hpp

Revision 13, 1.4 KB (checked in by Douglas Thrift, 5 years ago)

Take that David!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1// Doubly Linked List
2//
3// Douglas Thrift
4//
5// $Id$
6
7#ifndef _DoublyLinkedList_hpp_
8#define _DoublyLinkedList_hpp_
9
10#include "NullNode.hpp"
11#include "Iterator.hpp"
12
13template<typename Type>
14class DoublyLinkedList
15{
16private:
17        NullNode<Type> front;
18        NullNode<Type> back;
19public:
20        DoublyLinkedList() { front.setNext(&back); back.setPrevious(&front); }
21        ~DoublyLinkedList() { while (front.getNext() != &back) front.removeNext(); }
22        void addFront(const Type& value) { front.addNext(value); }
23        void addBack(const Type& value) { back.addPrevious(value); }
24        bool contains(const Type& value);
25        void removeFirst(const Type& value);
26        void removeLast(const Type& value);
27        Iterator<Type> iterator(void) { return Iterator<Type>(&front, &back); }
28        void accept(Visitor<Type>* visitor);
29};
30
31template<typename Type>
32bool DoublyLinkedList<Type>::contains(const Type& value)
33{
34        return front.contains(value, dynamic_cast<Node<Type>*>(&back), true);
35}
36
37template<typename Type>
38void DoublyLinkedList<Type>::removeFirst(const Type& value)
39{
40        front.remove(value, dynamic_cast<Node<Type>*>(&back), true);
41}
42
43template<typename Type>
44void DoublyLinkedList<Type>::removeLast(const Type& value)
45{
46        back.remove(value, dynamic_cast<Node<Type>*>(&front), false);
47}
48
49template<typename Type>
50void DoublyLinkedList<Type>::accept(Visitor<Type>* visitor)
51{
52        front.accept(visitor, dynamic_cast<Node<Type>*>(&back), true);
53}
54
55#endif // _DoublyLinkedList_hpp_
Note: See TracBrowser for help on using the browser.