카테고리 없음 / / 2015. 10. 15. 15:19

[자료구조] C언어 스택 Stack

반응형
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
int *Stack;
int size;
int top;
 
void InitStack(int _size){
    size = _size;
    Stack = (int*)malloc(sizeof(int)*_size);
    top = 0;
}
 
bool Push(int data){
    if (top >= size){
        return false;
    }
    Stack[top] = data;
    top++;
    return true;
}
 
int Pop(){
    if (top == 0){
        printf("empty\n");
        return -1;
    }
    int temp;
    temp = Stack[top-1];
    top--;
    printf("%d\n", temp);
    return temp;    
}
 
int main(void) {
 
    InitStack(4);
    Push(1);
    Push(5);
    Push(2);
    Push(3);
 
    Pop();
    Pop();
    Pop();
    Pop();
    Pop();
 
    return 0;    
}
cs






반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유