-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCursorList.h
109 lines (88 loc) · 3.51 KB
/
CursorList.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
#ifndef CursorList_H
#define CursorList_H
#define List CursorList
#include "vector.h"
#include "dsexceptions.h"
// LinkedList class using a cursor implementation
//
// CONSTRUCTION: with no initializer
// Access is via LinkedListItr class
//
// ******************PUBLIC OPERATIONS*********************
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ListItr zeroth( ) --> Return position to prior to first
// ListItr first( ) --> Return first position
// void insert( x, p ) --> Insert x after current iterator position p
// void remove( x ) --> Remove x
// ListItr find( x ) --> Return position that views x
// ListItr findPrevious( x )
// --> Return position prior to x
// ******************ERRORS********************************
// No special errors
template <class Object>
class ListItr; // Incomplete declaration.
template <class Object>
class List
{
public:
List( );
List( const List & rhs );
~List( );
bool isEmpty( ) const;
void makeEmpty( );
ListItr<Object> zeroth( ) const;
ListItr<Object> first( ) const;
void insert( const Object & x, const ListItr<Object> & p );
ListItr<Object> find( const Object & x ) const;
ListItr<Object> findPrevious( const Object & x ) const;
void remove( const Object & x );
public:
struct CursorNode
{
CursorNode( ) : next( 0 ) { }
private:
CursorNode( const Object & theElement, int n )
: element( theElement ), next( n ) { }
Object element;
int next;
friend class List<Object>;
friend class ListItr<Object>;
};
const List & operator=( const List & rhs );
private:
int header;
static vector<CursorNode> cursorSpace;
static void initializeCursorSpace( );
static int alloc( );
static void free( int p );
friend class ListItr<Object>;
};
// ListItr class; maintains "current position"
//
// CONSTRUCTION: Package friendly only, with an int
//
// ******************PUBLIC OPERATIONS*********************
// bool isPastEnd( ) --> True if at valid position in list
// void advance( ) --> Advance (if not already null)
// Object retrieve --> Return item in current position
template <class Object>
class ListItr
{
public:
ListItr( ) : current( 0 ) { }
bool isPastEnd( ) const
{ return current == 0; }
void advance( )
{ if( !isPastEnd( ) ) current = List<Object>::cursorSpace[ current ].next; }
const Object & retrieve( ) const
{ if( isPastEnd( ) ) throw BadIterator( );
return List<Object>::cursorSpace[ current ].element; }
private:
int current; // Current position
friend class List<Object>;
ListItr( int theNode )
: current( theNode ) { }
};
#include "CursorList.cpp"
#endif