VioletaBabel

스택 본문

기본개념/알고리즘공부
스택
Beabletoet 2017. 9. 8. 14:41

LIFO에 따라 자료를 배열.

프링글스같은 느낌임(공장에서 가장 마지막에 넣은 과자를 우린 가장 먼저 쳐먹지)



---

배열 이용


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
int main()
{
    int num[100], count = 0;
    char command;
    int comnum;
    while (1)
    {
        printf("입력 - i / 삭제 - d\n : ");
        scanf("%c"&command);
        if (command == 'i')
        {
            printf("입력할 자연수 : ");
            scanf("%d"&comnum);
            num[count++= comnum;
        }
        else if (command == 'd' && count > 0)
            printf("%d\n", num[--count]);
        while (getchar() != '\n');
    }
}
cs


---

연결리스트 이용


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
#include <cstdio>
struct node
{
    node *next;
    node *prev;
    int data;
};
node *head, *foot;
void init();
int main()
{
    init();
    char command;
    int comnum;
    node *newnode;
    while (1)
    {
        printf("입력 - i / 삭제 - d\n : ");
        scanf("%c"&command);
        if (command == 'i')
        {
            printf("입력할 자연수 : ");
            scanf("%d"&comnum);
            newnode = new node;
            newnode->next = foot;
            newnode->prev = foot->prev;
            newnode->prev->next = newnode;
            foot->prev = newnode;
            newnode->data = comnum;
        }
        else if (command == 'd')
        {
            newnode = foot->prev;
            if (newnode->data != -1)
            {
                newnode->prev->next = foot;
                foot->prev = newnode->prev;
                printf("%d\n", newnode->data);
                delete newnode;
            }
        }
        newnode = head->next;
        while (getchar() != '\n');
    }
}
void init()
{
    head = new node;
    foot = new node;
    head->next = foot->next = foot;
    head->prev = foot->prev = head;
    head->data = foot->data = -1;
}
cs


'기본개념 > 알고리즘공부' 카테고리의 다른 글

그래프  (0) 2017.09.09
트리  (0) 2017.09.08
  (0) 2017.09.08
연결리스트  (0) 2017.09.08
해시테이블 (hash table)  (0) 2017.09.05
Comments