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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef char element[100];
typedef struct DListNode {    // ÀÌÁß¿¬°á ³ëµå Å¸ÀÔ
    element data;
    struct DListNode* llink;
    struct DListNode* rlink;
} DListNode;
 
DListNode* current;
 
// ÀÌÁß ¿¬°á ¸®½ºÆ®¸¦ ÃʱâÈ­
void init(DListNode* phead)
{
    phead->llink = phead;
    phead->rlink = phead;
}
 
// ÀÌÁß ¿¬°á ¸®½ºÆ®ÀÇ ³ëµå¸¦ Ãâ·Â
void print_dlist(DListNode* phead)
{
    DListNode* p;
    for (p = phead->rlink; p != phead; p = p->rlink) {
        if (p == current)
            printf("<-| #%s# |-> ", p->data);
        else
            printf("<-| %s |-> ", p->data);
    }
    printf("\n");
}
// ³ëµå newnode¸¦ ³ëµå beforeÀÇ ¿À¸¥ÂÊ¿¡ »ðÀÔÇÑ´Ù.
void dinsert(DListNode *before, element data)
{
    DListNode *newnode = (DListNode *)malloc(sizeof(DListNode));
    strcpy(newnode->data, data);
    newnode->llink = before;
    newnode->rlink = before->rlink;
    before->rlink->llink = newnode;
    before->rlink = newnode;
}
// ³ëµå removed¸¦ »èÁ¦ÇÑ´Ù.
void ddelete(DListNode* head,
    DListNode* removed)
{
    if (removed == head) return;
    removed->llink->rlink = removed->rlink;
    removed->rlink->llink = removed->llink;
    free(removed);
}
 
// ÀÌÁß ¿¬°á ¸®½ºÆ® Å×½ºÆ® ÇÁ·Î±×·¥
int main(void)
{
    char ch;
    DListNode* head = (DListNode *)malloc(sizeof(DListNode));
    init(head);
 
    dinsert(head, "Mamamia");
    dinsert(head, "Dancing Queen");
    dinsert(head, "Fernando");
 
    current = head->rlink;
    print_dlist(head);
 
    do {
        printf("\n¸í·É¾î¸¦ ÀÔ·ÂÇϽÿÀ(<, >, q): ");
        ch = getchar();
        if (ch == '<') {
            current = current->llink;
            if (current == head)
                current = current->llink;
        }
        else if (ch == '>') {
            current = current->rlink;
            if (current == head)
                current = current->rlink;
        }
        print_dlist(head);
        getchar();
    } while (ch != 'q');
    // µ¿Àû ¸Þ¸ð¸® ÇØÁ¦ Äڵ带 ¿©±â¿¡
}
cs