-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist2_lib.cc
101 lines (87 loc) · 2.03 KB
/
slist2_lib.cc
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
#include "slist2.hh"
#include <cassert>
#include <cstring>
#include <iostream>
using namespace std;
namespace slist {
SingleLinkList::SingleLinkList(const char *s) {
size_t ctr = strlen(s);
while (ctr > 0) {
Prepend(s[ctr - 1]);
ctr--;
}
}
size_t SingleLinkList::Length() const {
size_t ans = 0u;
SingleLinkListElem *elem = First();
while (0 != elem) {
elem = elem->next.get();
ans++;
}
return ans;
}
void SingleLinkList::Prepend(char c) {
std::unique_ptr<SingleLinkListElem> temp(new SingleLinkListElem(c));
temp->next = move(h_);
h_ = move(temp);
}
ostringstream &operator<<(ostringstream &out, const SingleLinkList &sll) {
SingleLinkListElem *temp = sll.First();
while (temp != 0) {
out << temp->data << " -> ";
temp = temp->next.get();
}
out << string("0\n###");
return out;
}
void SingleLinkList::Delete() {
if (0 == h_) {
return;
}
unique_ptr<SingleLinkListElem> temp = move(h_);
h_ = move(temp->next);
temp.reset();
}
unsigned SingleLinkList::Count(char c) const {
unsigned count = 0u;
SingleLinkListElem *elem = First();
while (0 != elem) {
if (c == elem->data) {
count++;
}
elem = elem->next.get();
}
return count;
}
SingleLinkListElem *SingleLinkList::Last() const {
// Without this check, we dereference NULL in the loop's test.
if (0 == h_) {
return nullptr;
}
SingleLinkListElem *elem = h_.get();
while (0 != elem->next) {
elem = elem->next.get();
}
return elem;
}
void SingleLinkList::Append(SingleLinkList &sll) {
SingleLinkListElem *elem = Last();
// If the list to be appended is not empty.
if (0 != sll.First()) {
unique_ptr<SingleLinkListElem> newelem =
make_unique<SingleLinkListElem>(sll.First()->data);
newelem->next = move(sll.First()->next);
// If the appended-to list is not empty.
if (0 != elem) {
elem->next = move(newelem);
} else {
h_ = std::move(newelem);
}
}
}
char SingleLinkList::Pop() {
char c = First()->data;
Delete();
return c;
}
} // namespace slist