Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- update
- spring cloud
- Effective Java
- Spring Batch
- Express
- MySQL
- 퀵소트
- log4j2
- REACTJS
- Regular expression
- expire_logs_days
- npm
- 정규표현식
- Node
- java
- REACT
- spring
- migration
- nodejs
- current_date
- Effective Java 3/e
- regex
- upgrade
- Webpack
- JavaScript
- log_bin
- eslint
- Chunk
- mysql 5.5
- git
Archives
- Today
- Total
내 세상
[Algorithms] 오름차순 데이터 저장 기반 double linked list 본문
728x90
head / tail을 사용해서 양쪽 끝단을 시용한다. → Double Linked List의 특징
단, head의 경우 next의 값부터 사용하는 것을 전제로 함.
이유는 head->next를 첫번째 값으로 사용했을 경우, null 상태일때 대처를 손쉽게 할 수 있기 때문임.
const int MAX_N = 30001;
struct node {
int val;
node* next;
node* prev;
node* alloc(int _val, node* _next, node* _prev) {
val = _val;
next = _next;
prev = _prev;
return this;
}
}buf[MAX_N], *head, *tail;
int bCnt;
void init() {
for (int i = 0; i < bCnt; i++) {
buf[i].val = 0;
buf[i].next = buf[i].prev = 0;
}
bCnt = 0;
head = tail = 0;
head = buf[bCnt++].alloc(0, 0, 0);
}
node* find(int data) {
node* ptr = head;
for (; ptr->next; ptr = ptr->next) {
if (ptr->next->val > data) {
return ptr;
}
}
return 0;
}
void insert(int data) {
if (tail == 0) {
head->next = tail = buf[bCnt++].alloc(data, 0, 0);
}
else {
node* ptr = find(data);
if (ptr == 0) {
tail->next = buf[bCnt++].alloc(data, 0, tail);
tail = tail->next;
}
else {
node* temp = buf[bCnt++].alloc(data, ptr->next, ptr);
ptr->next->prev = temp;
ptr->next = temp;
}
}
}
int remove(int index) {
int idx = 0;
int res = -1;
node* ptr = head;
for (; ptr->next; ptr = ptr->next) {
if (idx == index) {
res = ptr->next->val;
if (ptr->next == tail) tail = ptr;
if(ptr->next->next) ptr->next->next->prev = ptr;
ptr->next = ptr->next->next;
break;
}
idx++;
}
return res;
}
int searchByIndex(int index) {
int idx = 0;
int res = -1;
node* ptr = head;
for (; ptr->next; ptr = ptr->next) {
if (idx == index) {
res = ptr->next->val;
break;
}
idx++;
}
return res;
}
728x90
'Coding > Algorithms' 카테고리의 다른 글
[Algorithms] Hashtable / Probing / Hash Chaining / DJB2 (0) | 2021.03.12 |
---|---|
[Algorithms] Double Linked List 구현 (0) | 2021.03.10 |
[Algorithms] Datastructure Queue 구현-Linked List (0) | 2021.03.10 |
[Algorithms] Disjoint sets (0) | 2021.02.26 |
[Algorithms] Merge Sort (0) | 2021.02.19 |