-
Notifications
You must be signed in to change notification settings - Fork 0
/
listIter.h
181 lines (154 loc) · 3.06 KB
/
listIter.h
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
///////////////////////////////////////////////////////////////////////////////
// Written by Steve Coward
// PSU CS 202 Fall 2009 Project 3
//
// Iterator class to accompany CList class.
//
// Supports forward and reverse iteration through a list.
//
// Contains the following functions:
//
// int GetFirst();
// int GetLast();
// Iteration initialization.
//
// int GetNext();
// int operator++(); // prefix
// int operator++(int); // postfix
// int GetPrev();
// int operator--(); // prefix
// int operator--(int); // postfix
// Updating of iterator.
//
// void Iterate(void (*f)(T *ip));
// Apply function f to each node of list.
//
// T* CurrentItem();
// Get current item pointed to by iterator.
//
///////////////////////////////////////////////////////////////////////////////
#if !defined(LISTITER_H)
#define LISTITER_H
//#include "stdafx.h"
#include "linkedList.h"
template <typename T>
class listIter
{
public:
listIter(const CList<T> *ptr);
listIter(const CList<T> &ref);
int GetFirst();
int GetLast();
int GetNext();
int GetPrev();
int operator++(); // prefix
int operator++(int); // postfix
int operator--(); // prefix
int operator--(int); // postfix
void Iterate(void (*f)(T *ip));
T* CurrentItem();
private:
const CList<T> *m_pList;
node<T>* m_currNode;
};
template <typename T>
listIter<T>::listIter(const CList<T> *ptr) : m_pList(ptr)
{
GetFirst(); // set m_currNode to head
}
template <typename T>
listIter<T>::listIter(const CList<T> &ref) : m_pList(&ref)
{
m_currNode = ref.GetHeadNode();
}
template <typename T>
int listIter<T>::GetFirst()
{
if (m_pList == NULL) {
m_currNode = NULL;
}
else {
m_currNode = m_pList->GetHeadNode();
}
return(m_currNode != NULL);
}
template <typename T>
int listIter<T>::GetLast()
{
if (m_pList == NULL) {
m_currNode = NULL;
}
else {
m_currNode = m_pList->GetTailNode();
}
return(m_currNode != NULL);
}
template <typename T>
int listIter<T>::operator++()
{
if (m_currNode != NULL) {
m_currNode = m_currNode->m_next;
}
return(m_currNode != NULL);
}
template <typename T>
int listIter<T>::operator++(int)
{
if (m_currNode != NULL) {
m_currNode = m_currNode->m_next;
}
return(m_currNode != NULL);
}
template <typename T>
int listIter<T>::operator--()
{
if (m_currNode != NULL) {
m_currNode = m_currNode->m_prev;
}
return(m_currNode != NULL);
}
template <typename T>
int listIter<T>::operator--(int)
{
if (m_currNode != NULL) {
m_currNode = m_currNode->m_prev;
}
return(m_currNode != NULL);
}
template <typename T>
int listIter<T>::GetNext()
{
if (m_currNode != NULL) {
m_currNode = m_currNode->m_next;
}
return(m_currNode != NULL);
}
template <typename T>
int listIter<T>::GetPrev()
{
if (m_currNode != NULL) {
m_currNode = m_currNode->m_prev;
}
return(m_currNode != NULL);
}
template <typename T>
void listIter<T>::Iterate(void (*f)(T *ip))
{
if (!GetFirst()) {
return;
}
do {
f(CurrentItem());
} while (GetNext());
}
template <typename T>
T* listIter<T>::CurrentItem()
{
if (m_currNode == NULL) {
return(NULL);
}
else {
return(m_currNode->m_ip);
}
}
#endif // LISTITER_H