Skip to content

Commit 3506cf9

Browse files
committed
Update Code
1 parent c513c97 commit 3506cf9

File tree

4 files changed

+143
-143
lines changed

4 files changed

+143
-143
lines changed

STL_구현/Single_Linked_List.cpp

+67-80
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,109 @@
11
#include <iostream>
22
using namespace std;
3-
43
template <typename T>
5-
struct Node {
4+
class Node {
65
public:
7-
T value;
8-
struct Node<T>* next = nullptr;
6+
T val;
7+
Node<T>* next;
8+
Node() {};
9+
~Node() {};
910
};
10-
1111
template <typename T>
1212
class SLL {
1313
private:
1414
Node<T>* head;
1515
Node<T>* tail;
16-
int size = 0;
16+
T size;
1717
public:
18-
SLL() : head(nullptr), tail(nullptr) {}
19-
~SLL() {}
20-
21-
void addNode(T _value) {
22-
Node<T>* node = new Node<T>;
23-
size++;
24-
node->value = _value;
25-
node->next = nullptr;
26-
18+
SLL() :head(nullptr), tail(nullptr), size(0) {};
19+
~SLL() {};
20+
void AddNode(T _val) {
21+
Node<T>* newNode = new Node<T>;
22+
newNode->val = _val;
23+
newNode->next = nullptr;
2724
if (head == nullptr) {
28-
head = node;
29-
tail = node;
25+
head = tail = newNode;
3026
}
3127
else {
32-
tail->next = node;
28+
tail->next = newNode;
3329
tail = tail->next;
3430
}
31+
size++;
3532
}
36-
void removeNode(T _value) {
33+
bool Find(T _val) {
3734
Node<T>* ptr = head;
38-
Node<T>* tmp = ptr;
35+
if (!size) return false;
3936
while (ptr != nullptr) {
40-
if (ptr->value == _value) {
41-
break;
42-
}
43-
else {
44-
tmp = ptr;
45-
ptr = tmp->next;
46-
}
37+
if (ptr->val == _val) return true;
38+
ptr = ptr->next;
4739
}
48-
if (ptr != nullptr) {
49-
cout << "delete:" << ptr->value << "\n";
50-
tmp->next = ptr->next;
40+
return false;
41+
}
42+
void DeleteNode(T _val) {
43+
Node<T>* ptr = head;
44+
Node<T>* tmp = ptr;
45+
if (!size) return;
46+
if (size == 1) {
5147
delete ptr;
52-
size--;
48+
head = tail = nullptr;
49+
size = 0;
50+
return;
5351
}
54-
else {
55-
cout << "delete fail\n";
52+
while (ptr!= nullptr) {
53+
if (ptr->val == _val) break;
54+
tmp = ptr;
55+
ptr = ptr->next;
5656
}
57+
tmp->next = ptr->next;
58+
delete ptr;
59+
size--;
5760
}
58-
59-
void show() {
61+
void Print() {
6062
Node<T>* ptr = head;
6163
while (ptr != nullptr) {
62-
cout << ptr->value << " ";
64+
cout << ptr->val << " ";
6365
ptr = ptr->next;
6466
}
6567
cout << "\n";
68+
return;
6669
}
67-
void deleteList() {
68-
Node<T>* ptr = head;
69-
while (ptr != nullptr) {
70-
head = ptr->next;
71-
delete ptr;
72-
ptr = head;
73-
}
74-
size = 0;
75-
}
76-
void addPos(int _index, int _value) {
77-
Node<T>* node = new Node<T>;
70+
void AddPos(T _pos, T _val) {
71+
if (_pos > size) return;
72+
Node<T>* newNode = new Node<T>;
73+
newNode->val = _val;
74+
newNode->next = nullptr;
7875
Node<T>* ptr = head;
7976
Node<T>* tmp = ptr;
80-
81-
node->value = _value;
82-
node->next = nullptr;
83-
for (int i = 0; i < _index; i++) {
77+
for (int i = 0; i < _pos; i++) {
8478
tmp = ptr;
8579
ptr = ptr->next;
8680
}
87-
tmp->next = node;
88-
node->next = ptr;
81+
tmp->next = newNode;
82+
newNode->next = ptr;
8983
size++;
9084
}
91-
void search(int _value) {
92-
Node<T>* ptr = head;
93-
int index = 0;
94-
while (ptr != nullptr) {
95-
if (ptr->value == _value) {
96-
cout << index << "¹ø° Á¸Àç\n";
97-
break;
98-
}
99-
ptr = ptr->next;
100-
index++;
101-
}
102-
}
103-
int Size() {
104-
return size;
105-
}
10685
};
10786
int main() {
10887
SLL<int> sll;
109-
sll.addNode(1);
110-
sll.addNode(2);
111-
sll.addNode(3);
112-
sll.addNode(5);
113-
sll.addNode(6);
114-
sll.show();
115-
sll.addPos(3, 4);
116-
sll.show();
117-
sll.removeNode(2);
118-
sll.show();
119-
sll.deleteList();
120-
sll.show();
88+
sll.AddNode(1);
89+
sll.AddNode(2);
90+
sll.AddNode(3);
91+
sll.AddNode(4);
92+
sll.AddNode(5);
93+
sll.AddNode(6);
94+
sll.Print();
95+
sll.DeleteNode(3);
96+
sll.Print();
97+
//cout << (sll.Find(2)) << "\n";
98+
sll.DeleteNode(2);
99+
sll.Print();
100+
sll.DeleteNode(4);
101+
sll.Print();
102+
sll.DeleteNode(5);
103+
sll.Print();
104+
sll.DeleteNode(6);
105+
sll.Print();
106+
sll.DeleteNode(1);
107+
sll.Print();
121108
return 0;
122109
}

STL_구현/queue.cpp

+24-27
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,55 @@
11
#include <iostream>
22
using namespace std;
3+
34
template <typename T>
45
class Node {
56
public:
6-
T value;
7-
Node* next;
8-
9-
Node() : value(0), next(nullptr) {};
7+
T val;
8+
Node<T>* next;
9+
Node() {};
1010
~Node() {};
1111
};
12+
1213
template <typename T>
1314
class Queue {
1415
private:
1516
Node<T>* head;
1617
Node<T>* tail;
17-
int size;
18+
T size;
1819
public:
1920
Queue() :head(nullptr), tail(nullptr), size(0) {};
2021
~Queue() {};
21-
void Push(T _value) {
22+
void Push(T _val) {
2223
Node<T>* newNode = new Node<T>;
23-
newNode->value = _value;
24-
size++;
24+
newNode->val = _val;
2525
if (head == nullptr) {
26-
head = newNode;
27-
tail = newNode;
26+
head = tail = newNode;
2827
}
2928
else {
3029
tail->next = newNode;
3130
tail = tail->next;
3231
}
32+
size += 1;
3333
}
34-
T Pop() {
35-
if (size == 0) return -1;
36-
else {
37-
Node<T>* ptr = head;
38-
T value = head->value;
3934

40-
if (head == tail) {
41-
head = tail = nullptr;
42-
}
43-
else {
44-
head = head->next;
45-
}
46-
delete ptr;
47-
return value;
48-
}
35+
T Pop() {
36+
if (!size) return -1;
37+
Node<T>* ptr = head;
38+
int value = ptr->val;
39+
if (head == tail) head = tail = nullptr;
40+
else head = head->next;
41+
delete ptr;
42+
size -= 1;
43+
return value;
4944
}
45+
5046
bool Empty() {
51-
if (head == nullptr) return true;
47+
if (!size) return true;
5248
else return false;
5349
}
54-
T Front() {
55-
return head->value;
50+
51+
T front() {
52+
return head->val;
5653
}
5754
};
5855
int main() {

STL_구현/stack.cpp

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include<iostream>
1+
#include <iostream>
22
using namespace std;
33
template <typename T>
44
class Node {
55
public:
6-
T value;
7-
Node<T>* next = nullptr;
6+
T val;
7+
Node<T>* next;
88
Node() {};
99
~Node() {};
1010
};
@@ -17,45 +17,47 @@ class Stack {
1717
public:
1818
Stack() : head(nullptr), tail(nullptr), size(0) {};
1919
~Stack() {};
20-
void Push(T _value) {
20+
void Push(T _val) {
2121
Node<T>* newNode = new Node<T>;
22-
newNode->value = _value;
22+
newNode->val = _val;
2323
if (head == nullptr) {
2424
head = tail = newNode;
2525
}
2626
else {
2727
tail->next = newNode;
2828
tail = tail->next;
2929
}
30-
size++;
30+
size += 1;
3131
}
3232
T Pop() {
33-
if (size == 0) return -1;
33+
if (!size) return -1;
34+
Node<T>* ptr = head;
35+
T value = ptr->val;
36+
if (head == tail) {
37+
delete tail;
38+
head = tail = nullptr;
39+
}
3440
else {
35-
Node<T>* ptr = head;
36-
int value = ptr->value;
37-
if (head == tail) {
38-
head = tail = nullptr;
39-
}
40-
else {
41-
while (ptr != nullptr) {
42-
if (ptr->next == tail) break;
43-
ptr = ptr->next;
44-
}
45-
value = ptr->next->value;
41+
while (ptr->next != tail) {
42+
ptr = ptr->next;
4643
}
44+
value = ptr->next->val;
4745
delete tail;
4846
tail = ptr;
49-
size--;
50-
return value;
5147
}
48+
size--;
49+
return value;
5250
}
53-
T Top() {
54-
return tail->value;
51+
bool Empty() {
52+
if (!size) return true;
53+
else return false;
5554
}
5655
T Size() {
5756
return size;
5857
}
58+
T Top() {
59+
return tail->val;
60+
}
5961
};
6062
int main() {
6163
Stack<int> s;
@@ -74,5 +76,4 @@ int main() {
7476
cout << s.Pop() << "\n";
7577
cout << "TOP: " << s.Top() << "\n";
7678
cout << s.Pop() << "\n";
77-
7879
}

0 commit comments

Comments
 (0)