์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋?
๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์์ ๋์ด ์ฒ์๊ณผ ์ฐ๊ฒฐ๋์ด ์๋ ํํ๋ฅผ ์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ผ๊ณ ํฉ๋๋ค.
STL์๋ ๊ตฌํ๋์ด์์ง ์๊ณ ์์ง ํ์ ์์ค์ด์ง๋ง, ์ค์ ๋ก ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ๋ ๋ณธ ์ ์ด ๋ง์ด ์์ต๋๋ค.
๊ทธ๋์ ๋จ์ํ๊ฒ๋ง ๋ง๋ค์ด๋ณด๋๋ก ํ ๊ฒ์ธ๋ฐ, ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ ํํ๋ก ๋ง๋ค๋ ค๊ณ ํฉ๋๋ค.
(์ค์ ๋ก ์ํ์ ์ธ ์์๋ ํ ๋๋ ์ฌ๊ท๋ฅผ ๋ง์ด ์ฌ์ฉํ๋๋ฏ)
์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Circular Linked List) ๊ตฌํํ๊ธฐ
๋ง์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ง๋ ์๊ฒ ์ต๋๋ค.
๋ ธ๋
#pragma once
template <typename T>
class Node {
public:
Node(T data);
T getData() const;
Node* getNext() const;
Node* getPrev() const;
void setNext(Node* node);
void setPrev(Node* node);
private:
T data;
Node* next;
Node* prev;
};
template <typename T>
Node<T>::Node(T data) : data(data), next(nullptr), prev(nullptr) {}
template <typename T>
T Node<T>::getData() const {
return data;
}
template <typename T>
Node<T>* Node<T>::getNext() const {
return next;
}
template <typename T>
Node<T>* Node<T>::getPrev() const {
return prev;
}
template <typename T>
void Node<T>::setNext(Node* node) {
next = node;
}
template <typename T>
void Node<T>::setPrev(Node* node) {
prev = node;
}
์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ ธ๋๋ ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ตฌ์กฐ์ผ ๊ฒฝ์ฐ ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ํฌ์คํธ์์ ์ฌ์ฉ๋ ๋ ธ๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
ํ์ฌ๋ ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ตฌ์กฐ์์ ์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ๊ฐ๋ฐํ๊ธฐ๋ก ์ค๊ณํ์ต๋๋ค.
์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ํด๋์ค
#pragma once
#include "CircularLinkedListNode.h"
#include <iostream>
// Prototype
template <typename T>
class CircularLinkedList {
public:
CircularLinkedList();
void push_front(const T& value);
void push_back(const T& value);
void pop_front();
void pop_back();
void travel();
private:
Node<T>* head;
Node<T>* tail;
};
template <typename T>
CircularLinkedList<T>::CircularLinkedList() : head(nullptr), tail(nullptr) {}
template <typename T>
void CircularLinkedList<T>::push_front(const T& value) {
Node<T>* newNode = new Node<T>(value);
if (head == nullptr) {
head = newNode;
tail = newNode;
head -> setNext(newNode);
head -> setPrev(newNode);
tail -> setNext(newNode);
tail -> setPrev(newNode);
} else {
head -> setPrev(newNode);
tail -> setNext(newNode);
newNode -> setNext(head);
newNode -> setPrev(tail);
head = newNode;
}
}
template <typename T>
void CircularLinkedList<T>::push_back(const T& value) {
if (head == nullptr) {
push_front(value);
} else {
Node<T>* newNode = new Node<T>(value);
head -> setPrev(newNode);
tail -> setNext(newNode);
newNode -> setNext(head);
newNode -> setPrev(tail);
tail = newNode;
}
}
template <typename T>
void CircularLinkedList<T>::pop_front() {
if(head == tail) {
delete head;
delete tail;
head == nullptr;
tail == nullptr;
} else {
Node<T>* newHead = head -> getNext();
tail -> setNext(newHead);
newHead -> setPrev(newHead);
delete head;
head = newHead;
}
}
template <typename T>
void CircularLinkedList<T>::pop_back() {
if(head == tail) {
delete head;
delete tail;
head == nullptr;
tail == nullptr;
} else {
Node<T>* newTail = tail -> getPrev();
newTail -> setNext(head);
head -> setPrev(newTail);
delete tail;
tail = newTail;
}
}
template <typename T>
void CircularLinkedList<T>::travel() {
Node<T>* temp = head;
do {
std::cout << (temp -> getData()) << ' ';
temp = temp -> getNext();
}
while (temp != head);
std::cout << '\n';
}
์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ์์ iterator๋ฅผ ์ ๊ฑฐํ๊ณ ๊ฐ๋จํ๊ฒ๋ง ์์ฑํ์ต๋๋ค.
๋งจ ์ ๋ค์ ์ถ๊ฐ/์ญ์ ํ๋ ์ฐ์ฐ๊ณผ ๋ชจ๋ ์์๋ฅผ ํ์ํ๋ ๊ณผ์ ๋ง ์์ฑํ์ต๋๋ค.

โป ์ถ๊ฐ ์ฐ์ฐ ์ ์ฃผ์ํด์ผ ํ ์ .
๋ง์ฝ, ์ฒ์์ผ๋ก ์ถ๊ฐํ๋ ๊ฒฝ์ฐ ๋ฆฌ์คํธ๋ ๋น์ด์์ต๋๋ค.
์ด ๋, ์ํ ์ฐ๊ฒฐ๋ฆฌ์คํธ์์ ์ฒซ ์์๋ ๋ณธ์ธ ์ค์ค๋ก๋ฅผ ๊ฐ๋ฅดํค๊ฒ ๋์ด์์ต๋๋ค.
์์๊ฐ ๋ค์ ์ถ๊ฐ ๋๋ ๊ฒฝ์ฐ, ์๋ก ์ถ๊ฐ๋๋ ์์๋ฅผ Now(ํ์ฌ ์์)๋ผ๊ณ ํ๊ฒ ์ต๋๋ค.
1. Tail(๋ง์ง๋ง ์์)์ ๋ค์ ๊ฐ์ผ๋ก Now๊ฐ ์์ผํ๊ณ Head(์ฒซ ์์)์ ์ด์ ๊ฐ์ผ๋ก Now๊ฐ ์์ผํฉ๋๋ค.
2. Now์ ๋ค์ ์์๋ Tail์ด ๋์ด์ผํ๊ณ Now์ ์ด์ ์์๋ Head๊ฐ ๋์ด์ผ ํฉ๋๋ค.
์์๊ฐ ์์ ์ถ๊ฐ ๋๋ ๊ฒฝ์ฐ๋ ๋ฐ๋ ์ผ์ด์ค๋ผ์ ์ค๋ช ํ์ง ์๊ฒ ์ต๋๋ค.
์์๊ฐ ์ญ์ ๋๋ ๊ฒฝ์ฐ๋, ์์๊ฐ ํ๋๋ง ์์ ๊ฒฝ์ฐ head์ tail์ด ๊ฐ์ ๊ฒฝ์ฐ (์ฃผ์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๋น๊ตํ ์ ์์)
1. ์ด ๋, head์ tail์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ญ์ ํ์ฌ ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ๋ฐฉ์งํ๊ณ ๋ชจ๋ null๊ฐ์ผ๋ก ๋ฐ๊พธ์ด์ผ ํฉ๋๋ค.
๋งจ ๋ค ๋ฐ์ดํฐ๊ฐ ์ญ์ ๊ฐ ๋๋ ๊ฒฝ์ฐ
1. Prev(Tail์ ์ด์ ๊ฐ)๊ฐ Tail์ด ๋์ด์ผํฉ๋๋ค.
2. Prev์ ๋ค์ ๊ฐ์ Next(Tail์ ๋ค์ ๊ฐ)์ผ๋ก ์ค์ ํฉ๋๋ค.
3. Next์ ์ด์ ๊ฐ์ Prev๋ก ์ค์ ํฉ๋๋ค.
4. ๊ธฐ์กด์ Tail์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํด์ ํด์ฃผ๊ณ Prev๋ฅผ Tail๋ก ์ ์ํฉ๋๋ค.
์์ ์์๊ฐ ์ญ์ ๋๋ ๊ฒฝ์ฐ๋ ๋ฐ๋ ์ผ์ด์ค๋ผ์ ์ค๋ช ํ์ง ์๊ฒ ์ต๋๋ค.
๋ง์ง๋ง์ผ๋ก ํ์ํ๋ ๊ฒฝ์ฐ์ ๋๋ค.
(head == tail) != null ์ด๋ฏ๋ก do์์ ๋จผ์ ์ถ๋ ฅํ ๋ค while๋ฌธ์์ ์กฐ๊ฑด ๊ฒ์ฌ๋ฅผ ํด์ผํฉ๋๋ค.
์คํ
#include <iostream>
#include "CircularLinkedList.h"
using namespace std;
int main() {
CircularLinkedList<int> customList;
cout << "===== push_back & travel =====\n";
for (int i = 10; i <= 50; i += 10) {
customList.push_back(i);
}
customList.travel();
cout << "\n===== push_front & travel =====\n";
for (int i = 1; i <= 5; i++) {
customList.push_front(i);
}
customList.travel();
cout << "\npop_front & pop_back & travel \n";
customList.pop_front();
customList.pop_back();
customList.travel();
return 0;
}
๊ฒฐ๊ณผ
GitHub ๋งํฌ URL
https://github.com/jihwankim128/datastructure
GitHub - jihwankim128/datastructure
Contribute to jihwankim128/datastructure development by creating an account on GitHub.
github.com
'์๊ณ ๋ฆฌ์ฆ > ์๋ฃ๊ตฌ์กฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Tree (0) | 2024.07.07 |
---|---|
6. STL ์ฌ์ฉํ๊ธฐ - std::list (0) | 2024.02.11 |
4. STL ๋ฐ๋ผ์ก๊ธฐ - ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Doubly Linked List) ๊ตฌํ (0) | 2024.02.07 |
3. STL ๋ฐ๋ผ์ก๊ธฐ - ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Singly Linked List) ๊ตฌํ (0) | 2024.02.04 |
2. ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Linked List) (1) | 2024.02.04 |