-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDList.CC
167 lines (148 loc) · 3.35 KB
/
DList.CC
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Greg Kaiser
//
// CS290 with Prof. Francis
// 4D Tetris
//
// DList.C
// Last modified: April 17, 1996
//
// (C) 1996 Board of Trustees University of Illinois
#include "DList.h"
//---------------------------DList CONSTRUCTORS----------------------//
//Given: nothing
//Task: creates generic constructor, with everything NULL
//Return: nothing
template<class T>
DList<T>::DList()
{
head = tail = current = NULL;
Assert(head==NULL || tail==NULL || current==NULL,
"Not enough memory to create list");
}
//Given: one element of type T
//Task: creates constructor with that element
//Return: nothing
template<class T>
DList<T>::DList(T *e)
{
Node<T> *temp = new Node<T>;
temp->data = e;
temp->next = temp->prev = NULL;
head = tail = current = temp;
}
template<class T>
DList<T>::~DList()
{
if (head != NULL)
delete head;
}
//-----------------------------DList MANIPULATORS------------------------//
template <class T>
void DList<T>::Output()
{
Node<T> *Temp = head;
while (Temp != NULL) {
std::cout << Temp->data << std::endl;
Temp = Temp->next;
}
}
//Given: one element of type T
//Task: inserts a new Node after the current pointer
//Return: nothing
template<class T>
void DList<T>::InsertAfter(T *e)
{
Node<T> *temp = new Node<T>;
temp->data = e;
if(head==NULL && tail==NULL) { //empty list
temp->next = temp->prev = NULL;
current = head = tail = temp;
} else {
temp->next = current->next;
current->next = temp;
temp->prev = current;
if(current == tail)
tail = temp;
else
temp->next->prev = temp;
current = temp;
}
}
//Given: one element of type T
//Task: inserts a new Node before the current pointer
//Return: nothing
template<class T>
void DList<T>::InsertBefore(T *e)
{
Node<T> *temp = new Node<T>;
temp->data = e;
if(head==NULL && tail==NULL) { //empty list
temp->prev = temp->next = NULL;
current = head = tail = temp;
} else {
temp->prev = current->prev;
current->prev = temp;
temp->next = current;
if(current = head)
head = temp;
else
temp->prev->next = temp;
current = temp;
}
}
//Given: nothing
//Task: removes the current Node
//Return: nothing
template<class T>
void DList<T>::Remove()
{
if(head==NULL && tail==NULL) //empty list
return;
if(head==tail) //one Node list
head = tail = current = NULL;
else {
if(current!=head)
current->prev->next = current->next;
else
head = current->next;
if(current!=tail)
current->next->prev = current->prev;
else
tail = current->prev;
if (current->next == NULL)
current = tail;
else
current = current->next;
}
}
template<class T>
Node<T> *DList<T>::ReturnCurrent()
{
return current;
}
template<class T>
void DList<T>::ChangeCurrent(Node<T> *NewCurrent)
{
current = NewCurrent;
}
//------------------------DList CURRENT POINTER ADJUSTMENT-------------------//
//Given: nothing
//Task: move current to next Node if not the last one
//Return: the list
template<class T>
DList<T>& DList<T>::operator++ (int)
{
if(current != tail)
current = current->next;
return *this;
}
//Given: nothing
//Task: move current to previous Node if not the first one
//Return: the list
template<class T>
DList<T>& DList<T>::operator-- (int)
{
if(current != head)
current = current->prev;
return *this;
}