티스토리 뷰

백준 온라인 저지

190224 BOJ 1406번 에디터

Stolen Moments 2019. 2. 25. 00:06

이중 연결 리스트


- 이중 연결 리스트를 처음으로 직접 구현해서 풀었다.

- 삼성 B형을 대비해서 구현 연습을 계속 해야겠다.

- 끝에 널문자를 출력해서 계속 오답이였다.....




풀이


1. 커서를 좌, 우로 이동하므로 이중 연결 리스트를 구현한다.

2. head와 tail에 유의하여 문제 조건에 맞게 함수를 구현한다.

3. 널문자를 출력하지 않도록 조심한다.



소스 코드



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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#define sws ios::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
struct node{
    char data;
    node *next, *prev;
};
node *head = (node*)malloc(sizeof(node));
node *tail = (node*)malloc(sizeof(node));
void init() {
    head->data = NULL;
    head->prev = NULL;
    head->next = tail;
    tail->data = NULL;
    tail->prev = head;
    tail->next = NULL;
}
void add(node *cur, char data) {
    node *newNode = (node*)malloc(sizeof(node));
    cur->prev->next = newNode;
    newNode->data = data;
    newNode->next = cur;
    newNode->prev = cur->prev;
    cur->prev = newNode;
}
void del(node *cur) {
    cur->prev->prev->next = cur;
    node *tmp = cur->prev;
    cur->prev = cur->prev->prev;
    free(tmp);
}
int main() {
    sws;
    init();
    int N;
    char cmd, x;
    string s;
    cin >> s;
    for (char c : s) add(tail, c);
    cin >> N;
    node *cur = tail;
    while (N--) {
        cin >> cmd;
        switch (cmd)
        {
        case 'L':
            if(cur != head->next) cur = cur->prev;
            break;
        case 'D':
            if (cur != tail) cur = cur->next;
            break;
        case 'B':
            if (cur != head->next) del(cur);
            break;
        case 'P':
            cin >> x;
            add(cur, x);
            break;
        default:
            break;
        }
    }
    cur = head->next;
    while (cur != tail) {
        cout << cur->data;
        cur = cur->next;
    }
}
cs


반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/09   »
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
글 보관함