Coding/Algorithms
[Algorithms] Double Linked List 구현
sga8
2021. 3. 10. 20:16
728x90
/// === main.cpp ===
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
const int MAX_LEN = 1100000;
int N, bn;
struct Node {
char val;
Node* prev;
Node* next;
Node() : prev(NULL), next(NULL), val(0) {}
Node* alloc(char _val, Node *_prev, Node *_next){
val = _val;
prev = _prev;
next = _next;
return this;
}
}buf[MAX_LEN];
Node* Head;
Node* cursor;
void insert(char ch){
Node* temp = buf[bn++].alloc(ch, NULL, NULL);
temp->val = ch;
temp->next = cursor->next;
temp->prev = cursor;
if (cursor->next != NULL) cursor->next->prev = temp;
cursor->next = temp;
cursor = temp;
}
void leftMove(){
if (cursor->prev != NULL){
cursor = cursor->prev;
}
}
void rightMove(){
if (cursor->next != NULL){
cursor = cursor->next;
}
}
void remove(){
if (cursor->prev != NULL){
cursor->prev->next = cursor->next;
if (cursor->next != NULL) cursor->next->prev = cursor->prev;
cursor = cursor->prev;
}
}
char arr[MAX_LEN];
int main(){
//freopen("input.txt", "r", stdin);
scanf("%d", &N);
char t;
for (int T = 0; T < N; T++){
bn = 0;
cursor = buf[bn++].alloc(0, NULL, NULL);
Head = cursor;
for (int i = 0; i < MAX_LEN; i++) arr[i] = 0;
scanf("%s", &arr);
for (int i = 0; t = arr[i]; i++){
if (t == '<'){
leftMove();
}
else if (t == '>'){
rightMove();
}
else if (t == '-'){
remove();
}
else {
insert(t);
}
}
while (Head->next != NULL){
printf("%c", Head->next->val);
Head = Head->next;
}
printf("\n");
}
return 0;
}
728x90