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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
template <typename TYPE>
class ListDouble {
template <typename T> friend ostream& operator<<(ostream&, ListDouble<T>&);
// ===== Class Node intern ====================
class Node {
public:
TYPE value;
Node *next, *previous;
Node (TYPE v, Node * p = NULL, Node * s = NULL)
{ value= v; previous= p; next= s; }
};
// ===============================================
Node *head, *queue;
int size;
public:
// ===== Class Iterator intern ================
class Iterator {
Node*node;
friend class ListDouble; //Allows methods of list to read Node
public:
Iterator (Node* n = NULL) // Constructor
{ node= n; }
Iterator (Iterator &I) // Copy constructor
{ node= I.node; }
Iterator & operator++() { // Operator ++ used as ++I
if (node!= NULL) node= node->next;
return *this;
}
Iterator & operator++(int) { // Operator ++ used as I++
Iterator temp = *this;
if (node!= NULL) node= node->next;
return temp;
}
Iterator & operator--() { // Operator -- used as --I
if (node!= NULL) node= node->previous;
return *this;
}
Iterator & operator--(int) { // Operator -- used as I--
Iterator temp = *this;
if (node!= NULL) node= node->previous;
return temp;
}
Iterator & operator+(int n) const {
Iterator it(node);
if (n > 0)
for (int i=0; i<n; i++) it++;
else
for (int i=0; i>n; i--) it--;
return it;
}
Iterator & operator-(int n) const {
return operator+(-n);
}
TYPE& operator*() { // Operator * used as *I
return node->value; // Don't use if node = 0
}
bool operator==(Iterator &I) { // Operator == between Iterator
return node == I.node ;
}
bool operator!=(Iterator &I) { // Operator != between Iterator
return node != I.node ;
}
Iterator & operator=(Iterator &I) {
node = I.node ; // Assigns I1 = I2
return *this;
}
};
// ===============================================
ListDouble() {
head= NULL;
queue = NULL;
size= 0;
}
~ListDouble() {
while (head!= NULL) {
Node*N = head;
head = head->next;
delete N;
}
}
int getSize() {
return size;
}
void insertHead(TYPE element) {
if (head== NULL)
head = queue = new Node(element, NULL, NULL);
else
head= head->previous= new Node(element, NULL, head);
size++;
}
void insertQueue(TYPE element) {
if (queue == NULL)
head= queue = new Node(element, NULL, NULL);
else
queue = queue->next= new Node(element, queue, NULL);
size++;
}
TYPE extractHead() {
if (head== NULL) return -1; // Error
TYPE element = head->value; // Keeps the value to extract.
Node *temp = head; // Keeps the node to destruct.
head= head->next; // The head is the next node.
if (head== NULL) // If there's no head the list is empty
queue = NULL;
else // If theres a next node now he has no previous
head->previous= NULL;
delete temp; // Destruct node.
size--; // Adjust the size of the list.
return element;
}
TYPE extractQueue() {
if (queue == NULL) return -1;
TYPE element = queue->value;
Node *temp = queue;
queue = queue->previous;
if (queue == NULL)
head= NULL;
else
queue->next= NULL;
delete temp;
size--;
return element;
}
Iterator start() { // Puts the iterator at the start of list
return Iterator(head);
}
Iterator end() { // Puts the iterator at the end of list
return Iterator (NULL);
}
};
|
template <typename T> ostream& operator<<(ostream& ostr, ListDouble<T> &L) {
// Friend function of list that prints out.
ListDouble<T>::Node *N = L.head;
ostr << "HEAD -> ";
while (N != NULL) {
ostr << N->value;
if (N == L.queue)
ostr << " <- ";
else
ostr << " <-> ";
N = N->next;
}
ostr << "QUEUE" << endl;
return ostr;
}
//------------------------------------------------------------------------
// Main program
//------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
ListDouble<int> L;
L.insertHead(10);
L.insertHead(20);
L.insertQueue(30);
L.insertQueue(40);
L.insertHead(50);
L.insertHead(60);
L.insertQueue(70);
L.insertQueue(80);
cout << "With the friend function... \n";
cout << L;
cout << "With Iterators... \n";
ListDouble<int>::Iterator I;
for(I = L.start(); I != L.end(); ++I)
cout << *I << " ";
cout << endl;
return 0;
} |