Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion caucsejunseo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@
| 6์ฐจ์‹œ | 2025.04.06 | ํ | [์•ต๋ฌด์ƒˆ](https://www.acmicpc.net/problem/14713)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/23|
| 7์ฐจ์‹œ | 2025.04.10 | ๊ทธ๋ฆฌ๋”” | [ํด๋ฆฌ์˜ค๋ฏธ๋…ธ](https://www.acmicpc.net/problem/1343)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/28|
| 8์ฐจ์‹œ | 2025.04.11 | ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ| [ํ‚ค๋กœ๊ฑฐ](https://www.acmicpc.net/problem/5397)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/30|

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 1000001

// ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋…ธ๋“œ ๊ตฌ์กฐ์ฒด ์ •์˜
typedef struct Node {
char data;
struct Node* prev;
struct Node* next;
} Node;

// ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘๊ณผ ๋์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ตฌ์กฐ์ฒด
typedef struct {
Node* head;
Node* tail;
} List;

// ๋ฆฌ์ŠคํŠธ ์ดˆ๊ธฐํ™”
void init_list(List* list) {
list->head = NULL;
list->tail = NULL;
}

// ์ƒˆ ๋…ธ๋“œ ์‚ฝ์ž… (์ปค์„œ ๋ฐ”๋กœ ์•ž ์œ„์น˜)
void insert(List* list, Node** cursor, char ch) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = ch;
new_node->prev = NULL;
new_node->next = NULL;

// ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ๊ฒฝ์šฐ
if (list->head == NULL) {
list->head = list->tail = new_node;
*cursor = new_node;
return;
}

// ์ปค์„œ๊ฐ€ NULL์ด๋ฉด ๋งจ ์•ž ์‚ฝ์ž…
if (*cursor == NULL) {
new_node->next = list->head;
list->head->prev = new_node;
list->head = new_node;
*cursor = new_node;
return;
}

// ์ผ๋ฐ˜์ ์ธ ์œ„์น˜ ์‚ฝ์ž…
new_node->prev = *cursor;
new_node->next = (*cursor)->next;

if ((*cursor)->next != NULL)
(*cursor)->next->prev = new_node;
else
list->tail = new_node;

(*cursor)->next = new_node;
*cursor = new_node;
}

// ๋…ธ๋“œ ์‚ญ์ œ (์ปค์„œ ์•ž ๋…ธ๋“œ ์‚ญ์ œ)
void delete(List* list, Node** cursor) {
if (*cursor == NULL) return;

Node* to_delete = *cursor;

if (to_delete->prev != NULL)
to_delete->prev->next = to_delete->next;
else
list->head = to_delete->next;

if (to_delete->next != NULL)
to_delete->next->prev = to_delete->prev;
else
list->tail = to_delete->prev;

*cursor = to_delete->prev;
free(to_delete);
}

// ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ
void print_list(List* list) {
Node* cur = list->head;
while (cur != NULL) {
printf("%c", cur->data);
cur = cur->next;
}
printf("\n");
}

// ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ
void free_list(List* list) {
Node* cur = list->head;
while (cur != NULL) {
Node* temp = cur;
cur = cur->next;
free(temp);
}
list->head = list->tail = NULL;
}

int main() {
int T;
scanf("%d", &T);
while (T--) {
char input[MAX];
scanf("%s", input);

List list;
init_list(&list);
Node* cursor = NULL; // ์ปค์„œ ์ดˆ๊ธฐ ์œ„์น˜๋Š” ๊ฐ€์žฅ ์•ž

for (int i = 0; input[i]; i++) {
char ch = input[i];
if (ch == '<') {
if (cursor != NULL) cursor = cursor->prev;
}
else if (ch == '>') {
if (cursor == NULL) cursor = list.head;
else if (cursor->next != NULL) cursor = cursor->next;
}
else if (ch == '-') {
delete(&list, &cursor);
}
else {
insert(&list, &cursor, ch);
}
}

print_list(&list);
free_list(&list);
}
return 0;
}