프로그래밍 이야기

C언어 스택

지켜보는비둘기 2017. 2. 22. 17:12
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
 
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#define Max 10
 
int stack[Max];
int top = 0;
 
void push(int data) 
{
    if (top < Max && top >= 0) 
    {
        stack[top] = data;
        top++;
        //return true;
 
    }
    else 
    {
        printf("오버플로우, 배열에 저장되지 않습니다.\n ");
        //return false;
    }
 
}
 
void pop()
{
    top--;
    //return true;
}
 
int main()
    int i = 0;
    while (1) {
        Sleep(1000);
        i++;
        printf("%d\n", i);
            push(i);
        
 
        if (_kbhit())
        {
            getchar();
            for (int j = top-1 ; j >= 0; j-- )
            {
                printf("[%d]", stack[j]);
                pop();
            }
            printf("\n");
 
        }
    }
 
    return 0;
}