-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_STL_ADT_Implementation
More file actions
177 lines (147 loc) · 4.05 KB
/
list_STL_ADT_Implementation
File metadata and controls
177 lines (147 loc) · 4.05 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
#pragma once
template <typename T>
class node
{
public:
T val;
node<T>* next;
node <T>* prev;
};
#pragma once
#include "node.h"
#include <iostream>
namespace cs211 {
template <typename T>
class list {
private:
node<T>* H;
int n;
public:
// Iterator class
class iterator {
private:
node<T>* ptr;
public:
iterator(node<T>* p = nullptr) : ptr(p) {} // Constructor for the iterator
T& operator*() {
return this->ptr->val;
}
T* operator->() {
return &(this->ptr->val);
}
iterator& operator--() {
this->ptr = this->ptr->prev;
return *this;
}
iterator operator--(int) {
iterator temp = *this;
this->ptr = this->ptr->prev;
return temp;
}
iterator& operator++() {
this->ptr = this->ptr->next;
return *this;
}
iterator operator++(int) {
iterator temp = *this;
this->ptr = this->ptr->next;
return temp;
}
bool operator==(const iterator& other) const {
return this->ptr == other.ptr;
}
bool operator!=(const iterator& other) const {
return this->ptr != other.ptr;
}
friend class list; // Allow list to access private members of iterator
};
list() {
this->H = new node<T>;
this->H->next = this->H->prev = this->H;
this->n = 0;
}
iterator begin() {
return iterator(this->H->next);
}
iterator rbegin() {
return iterator(this->H->prev);
}
iterator end() {
return iterator(this->H);
}
iterator rend() {
return iterator(this->H);
}
void push_front(const T& val) {
node<T>* ptr = new node<T>;
ptr->val = val;
ptr->next = this->H->next;
ptr->prev = this->H;
this->H->next->prev = ptr;
this->H->next = ptr;
this->n++;
}
void push_back(const T& val) {
node<T>* ptr = new node<T>;
ptr->val = val;
ptr->next = this->H;
ptr->prev = this->H->prev;
this->H->prev->next = ptr;
this->H->prev = ptr;
this->n++;
}
void pop_back() {
if (this->H->prev == this->H) {
std::cout << "list underflow!";
return;
}
node<T>* ptr = this->H->prev;
ptr->prev->next = this->H;
this->H->prev = ptr->prev;
delete ptr;
this->n--;
}
T front() const {
if (this->H->next == this->H) {
throw std::runtime_error("list underflow!");
}
return this->H->next->val;
}
T back() const {
if (this->H->prev == this->H) {
std::cout<<"list underflow!";
}
return this->H->prev->val;
}
bool empty() const {
return this->H->next == this->H;
}
void clear() {
node<T>* ptr;
while (this->H->next != this->H) {
ptr = this->H->next;
this->H->next = ptr->next;
delete ptr;
}
this->H->prev = this->H;
this->n = 0;
}
void erase(const iterator& it) {
node<T>* ptr = it.ptr;
if (ptr == this->H) {
std::cout<<"list Underflow!";
}
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
delete ptr;
this->n--;
}
int size() const {
return this->n;
}
~list() {
this->clear();
delete this->H;
}
};
}