-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
179 lines (146 loc) · 4.17 KB
/
List.cpp
File metadata and controls
179 lines (146 loc) · 4.17 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
// List.cpp
// Created by Edward J Skrod on 10/9/13. Adapted from Weiss, "Data Structurs
// And Algorithm Analysis in C++", 3rd edition, Section 3.5 "Implementation of a list."
#include <iostream>
#include "List.h"
using namespace std;
List::List()
// Default constructor creates an empty list.
// head and tail are initialized to NULL, size set to 0.
{ init( ); }
List::List(const List & rhs)
{
// Initialize the list
init();
// Copy all of rhs to lhs
for (const_iterator itr = rhs.begin( ); itr != rhs.end( ); ++itr){
this->push_back( *itr );
}
}
List::~List()
// Destructor deletes all items in the list.
{
clear( );
delete head;
delete tail;
} // ~List
void List::init()
// Initializes List to size=0, with head and tail empty
// nodes, pointing to each other
{
theSize = 0;
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
const List& List::operator= ( const List & rhs)
{
if (this == &rhs)
return *this;
clear( );
for (const_iterator itr = rhs.begin( ); itr != rhs.end( ); ++itr){
push_back( *itr );
}
return *this;
}
List::iterator List::insert( iterator itr, const Play &x )
{
Node *p = itr.current;
theSize++;
return iterator( p->prev = p->prev->next = new Node( x, p->prev, p) );
}
List::iterator List::erase ( iterator itr )
{
if (head != tail->prev){
Node *p = itr.current;
iterator retVal( p -> next );
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
theSize--;
return retVal;
}
else { // List is Empty
cout << "Cannot erase from an empty list.\n";
return itr;
}
}
List::iterator List::erase ( iterator start, iterator end )
// Deletes all nodes from start to end - 1, position.
// Returns end
{
for (iterator itr = start; itr != end;)
{
itr = erase( itr );
}
return end;
}
void List::clear( )
{
while ( !empty( ) )
pop_front( );
}
List::iterator List::search(string desc, float rel)
// Uses <cmath>
// search for Node containing desc == description and rel = relevance
// if yes, return the pointer to the listnode in the list
// if not, return NULL
{
// search for matching description and relevance
iterator iter = this->begin(); // nodePtr traverses the list
bool found = false;
//iter.getCurrent()->playObj.getQuarter(),
while (iter != this->end() && (!found)) {
if ((iter.getCurrent()->playObj.getDescription() == desc) &&
(abs((rel - iter.getCurrent()->playObj.getRelevance())) <= .001))
{
found = true;
}
else {
++iter;
}
}
return iter;
}
void List::move_to_front( iterator itr )
{
if (itr == head->next){
// Node is already the first node in the list. No need to move to the front.
} else {
// Remove node from list. Connect Node->prev and Node->next
itr.getCurrent()->prev->next = itr.getCurrent()->next;
itr.getCurrent()->next->prev = itr.getCurrent()->prev;
// Insert Node at head->next
// Connect head->next to itr
head->next->prev = itr.getCurrent();
itr.getCurrent()->next = head->next;
// Connect head to itr
itr.getCurrent()->prev = head;
head->next = itr.getCurrent();
}
}
void List::print( int n )
// printing the content and the list
{
if (n <= 0) {
return;
}
int count = 0;
if (head == tail->prev) {
cout << "list has no item.\n";
return;
}
iterator itr = this->begin();
while (itr != this->end() && (count < n)){
cout << itr.getCurrent()->playObj.getPlayDescription() << " "
<< itr.getCurrent()->playObj.getMin() << " "
<< itr.getCurrent()->playObj.getOffenseName() << " "
<< itr.getCurrent()->playObj.getDefenseName() << " "
<< itr.getCurrent()->playObj.getDown() << " "
<< itr.getCurrent()->playObj.getYardsToGo() << " "
<< itr.getCurrent()->playObj.getStartLocation() << "\n";
++itr;
++count;
}
} // print()