-
Notifications
You must be signed in to change notification settings - Fork 0
/
ordered_string_list.cpp
160 lines (149 loc) · 2.67 KB
/
ordered_string_list.cpp
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
#include <sstream>
#include <iostream>
#include <ostream>
#include <istream>
#include "ordered_string_list.hpp"
ordered_string_list::~ordered_string_list()
{
delete first;
delete last;
count = 0;
}
int ordered_string_list::size()
{
return count;
}
ordered_string_list::operator std::string()
{
node* curr(first);
std::stringstream text;
while (curr != 0)
{
text << curr->val << std::endl;
curr = curr->next;
}
return text.str();
}
ordered_string_list::node* ordered_string_list::find(std::string s) const
{
node* curr(first);
while (curr != 0 and curr->val < s)
{
curr = curr->next;
}
return curr;
}
bool ordered_string_list::member(std::string s) const
{
node* curr(first);
while (curr != 0 and curr->val != s)
curr = curr->next;
if(curr != 0)
return curr->val == s;
else
return false;
}
//(std::string s, node* p, node* n)
//ordered_string_list() : count(0), first(0), last(0)
void ordered_string_list::insert(std::string s)
{
ordered_string_list::node* curr(first);
if (curr == NULL) // List is empty, check
{
node* inserter = new node(s, 0, 0);
first = inserter;
last = inserter;
++count;
return;
}
if (first == last) // Only 1 node,
{
node* inserter = new node(s, curr, 0);
first->next = inserter;
last = inserter;
++count;
return;
}
while (curr->val < s and curr != last)
curr = curr->next;
if (curr == last)
{
if(curr->val > s)
{
node* inserter = new node(s, curr->prev, curr);
curr->prev->next = inserter;
curr->prev = inserter;
++count;
return;
}
else
{
node* inserter = new node(s, curr, 0);
curr->next = inserter;
last = inserter;
++count;
return;
}
}
if (curr == first)
{
node* inserter = new node(s, 0, curr);
curr->prev = inserter;
first = inserter;
++count;
return;
}
node* inserter = new node(s, curr->prev, curr);
curr->prev->next = inserter;
curr->prev = inserter;
++count;
}
bool ordered_string_list::remove(std::string s)
{
ordered_string_list::node* current(first);
if(current == 0) //No Nodes
{
return false;
}
if(first == last) //Only 1 Node
{
if(first->val == s)
{
first = 0;
last = 0;
count = 0;
return true;
}
else
return false;
}
while(current != last and current->val != s)
current = current->next;
if(current == last)
{
if(current->val == s)
{
current->prev->next = 0;
--count;
last = current->prev;
return true;
}
else
return false;
}
//Special cases taken care of
else
{
if(current == first) //this can happen!
{
first = current->next;
current->next->prev = 0;
--count;
return true;
}
current->prev->next = current->next;
current->next->prev = current->prev;
--count;
return true;
}
}