-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursorList.cpp
More file actions
197 lines (170 loc) · 5.25 KB
/
CursorList.cpp
File metadata and controls
197 lines (170 loc) · 5.25 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "CursorList.h"
/**
* Routine to initialize the cursorSpace.
*/
template <class Object>
void List<Object>::initializeCursorSpace( )
{
static int cursorSpaceIsInitialized = false;
if( !cursorSpaceIsInitialized )
{
cursorSpace.resize( 100 );
for( int i = 0; i < cursorSpace.size( ); i++ )
cursorSpace[ i ].next = i + 1;
cursorSpace[ cursorSpace.size( ) - 1 ].next = 0;
cursorSpaceIsInitialized = true;
}
}
/**
* Allocate a CursorNode
*/
template <class Object>
int List<Object>::alloc( )
{
int p = cursorSpace[ 0 ].next;
cursorSpace[ 0 ].next = cursorSpace[ p ].next;
return p;
}
/**
* Free a CursorNode
*/
template <class Object>
void List<Object>::free( int p )
{
cursorSpace[ p ].next = cursorSpace[ 0 ].next;
cursorSpace[ 0 ].next = p;
}
/**
* Construct the list
*/
template <class Object>
List<Object>::List( )
{
initializeCursorSpace( );
header = alloc( );
cursorSpace[ header ].next = 0;
}
/**
* Copy constructor
*/
template <class Object>
List<Object>::List( const List<Object> & rhs )
{
initializeCursorSpace( );
header = alloc( );
cursorSpace[ header ].next = 0;
*this = rhs;
}
/**
* Destroy the list
*/
template <class Object>
List<Object>::~List( )
{
makeEmpty( );
free( header );
}
/**
* Test if the list is logically empty.
* return true if empty, false otherwise.
*/
template <class Object>
bool List<Object>::isEmpty( ) const
{
return cursorSpace[ header ].next == 0;
}
/**
* Make the list logically empty.
*/
template <class Object>
void List<Object>::makeEmpty( )
{
while( !isEmpty( ) )
remove( first( ).retrieve( ) );
}
/**
* Return an iterator representing the header node.
*/
template <class Object>
ListItr<Object> List<Object>::zeroth( ) const
{
return ListItr<Object>( header );
}
/**
* Return an iterator representing the first node in the list.
* This operation is valid for empty lists.
*/
template <class Object>
ListItr<Object> List<Object>::first( ) const
{
return ListItr<Object>( cursorSpace[ header ].next );
}
/**
* Insert item x after p.
*/
template <class Object>
void List<Object>::insert( const Object & x, const ListItr<Object> & p )
{
if( p.current != 0 )
{
int pos = p.current;
int tmp = alloc( );
cursorSpace[ tmp ] = CursorNode( x, cursorSpace[ pos ].next );
cursorSpace[ pos ].next = tmp;
}
}
/**
* Return iterator corresponding to the first node containing an item x.
* Iterator isPastEnd if item is not found.
*/
template <class Object>
ListItr<Object> List<Object>::find( const Object & x ) const
{
/* 1*/ int itr = cursorSpace[ header ].next;
/* 2*/ while( itr != 0 && cursorSpace[ itr ].element != x )
/* 3*/ itr = cursorSpace[ itr ].next;
/* 4*/ return ListItr<Object>( itr );
}
/**
* Return iterator prior to the first node containing an item x.
*/
template <class Object>
ListItr<Object> List<Object>::findPrevious( const Object & x ) const
{
/* 1*/ int itr = header;
/* 2*/ while( cursorSpace[ itr ].next != 0 &&
cursorSpace[ cursorSpace[ itr ].next ].element != x )
/* 3*/ itr = cursorSpace[ itr ].next;
/* 4*/ return ListItr<Object>( itr );
}
/**
* Remove the first occurrence of an item x.
*/
template <class Object>
void List<Object>::remove( const Object & x )
{
ListItr<Object> p = findPrevious( x );
int pos = p.current;
if( cursorSpace[ pos ].next != 0 )
{
int tmp = cursorSpace[ pos ].next;
cursorSpace[ pos ].next = cursorSpace[ tmp ].next;
free ( tmp );
}
}
/**
* Deep copy of linked lists.
*/
template <class Object>
const List<Object> & List<Object>::operator=( const List<Object> & rhs )
{
ListItr<Object> ritr = rhs.first( );
ListItr<Object> itr = zeroth( );
if( this != &rhs )
{
makeEmpty( );
for( ; !ritr.isPastEnd( ); ritr.advance( ), itr.advance( ) )
insert( ritr.retrieve( ), itr );
}
return *this;
}