-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop_lab2.cpp
159 lines (123 loc) · 3.19 KB
/
oop_lab2.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
#include <iostream>
#include <cstring>
#include <climits>
using namespace std;
class String
{
char* buffer_;
int capacity_;
int size_;
void allocate () { buffer_ = new char[capacity_ + 1]; }
void from_c_string (const char* c_str)
{
allocate();
for (int i = 0 ; i < capacity_ ; i++, size_ = i) { char c = buffer_[i] = c_str[i]; if (!c) break; }
buffer_[size_] = '\0';
}
public:
String (int capacity__ = 256) : capacity_(capacity__), size_(0) { allocate (); buffer_[size_] = '\0';}
String (const String& str) : capacity_(str.capacity_), size_(str.size_)
{
allocate();
for (int i = 0 ; i <= size_ ; i++) buffer_[i] = str.buffer_[i];
}
String (const String& str, int start, int length) : capacity_(str.capacity_)
{
allocate();
for (int i = start ; i < length && i <= str.size_ ; i++, size_ = i - start)
{ char c = buffer_[i - start] = str.buffer_[i]; if (!c) break; }
buffer_[size_] = '\0';
}
String (const char* c_str, int c_size) : capacity_(c_size) { from_c_string(c_str); }
String (const char* c_str) : capacity_(strlen(c_str)) { from_c_string(c_str); }
~String() { delete[] buffer_; }
int size () const { return size_; }
int capacity () const { return capacity_; }
char at (int pos) const { return pos >= 0 && pos < size_ ? buffer_[pos] : '\0'; }
bool at (int pos, char c) { return pos >= 0 && pos < size_ ? buffer_[pos] = c, true : false; }
bool add (char c) { return size_ < capacity_ ? size_++, buffer_[size_] = c, buffer_[size_+1] = '\0', true : false; }
String substr (int start = 0, int length = INT_MAX) const { return String (*this, start, length); }
int find (const String& str, int start = 0) const
{
for (int i = start ; i < size_ ; i++)
{
if ( buffer_[i] != str.buffer_[0] ) continue;
int j;
for (j = 1 ; j < str.size_ && i + j < size_ ; j++)
if ( buffer_[i+j] != str.buffer_[j] ) break;
if ( j == str.size_ ) return i;
}
return -1;
}
void print () { cout << buffer_; }
};
class List
{
struct Node
{
String& data;
Node* next;
// Initializer is crucial due to &
Node (String& s, Node* n = NULL) : data(s), next(n) {}
};
Node* _start;
int _size;
public:
List () : _start(NULL), _size(0) {}
~List () { while (_size) popAt(0); }
int size () { return _size; }
void pushFront (String& s)
{
_start = new Node (s, _start);
_size++;
}
bool popAt (int pos)
{
if ( pos < 0 || pos >= _size ) return false;
Node* t = _start;
if ( pos == 0 )
{
_start = _start->next;
}
else
{
for (int i = 0 ; i < pos - 1 ; i++) t = t->next;
Node* t2 = t->next;
t->next = t->next->next;
t = t2;
}
delete t;
_size--;
return true;
}
String* at (int pos)
{
if ( pos < 0 || pos >= _size ) return NULL;
Node* t = _start;
for (int i = 0 ; i < pos ; i++) t = t->next;
return &(t->data);
}
};
int main ()
{
String s1("George"), s2("Stathis"), s3("Errikos"), s4("Bill");
List l;
l.pushFront(s1);
l.pushFront(s2);
l.pushFront(s3);
l.pushFront(s4);
cout << l.size() << endl;
l.at(0)->print();
cout << endl;
l.at(1)->print();
cout << endl;
l.at(2)->print();
cout << endl;
l.at(3)->print();
cout << endl;
cout << endl;
l.popAt(1);
l.at(1)->print();
cout << endl;
cout << l.size() << endl;
}