아래 링크를 참고했으며, 연결 리스트로 스택을 만듬 

 

https://ehpub.co.kr/c%EC%96%B8%EC%96%B4-%EC%86%8C%EC%8A%A4-%EC%8A%A4%ED%83%9D%EC%9D%84-%EC%97%B0%EA%B2%B0%EB%A6%AC%EC%8A%A4%ED%8A%B8%EB%A1%9C-%EA%B5%AC%ED%98%84/

 

[C언어 소스] 스택을 연결리스트로 구현 – 언제나 휴일

안녕하세요. 언제나 휴일입니다. 이번에는 스택(STACK)을 연결리스트로 구현하는 소스 코드입니다. 스택은 자료를 한쪽으로 보관하고 꺼내는 LIFO(Last In First Out) 방식의 자료구조입니다. 스택에 자

ehpub.co.kr

 

 

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
 
#include <stdio.h>
#include <stdlib.h>
 
 
typedef struct node {
    int data;
    struct node *next;
}Node;
 
typedef struct stack {
    Node *top;    
}Stack;
 
void InitStack(Stack *stack) {
    stack->top = NULL
}
 
int IsEmpty(Stack *stack) {
    return stack->top == NULL;    
}
 
void Push(Stack *stackint data) {
    Node *now = (Node *)malloc(sizeof(Node)); 
    now->data = data;
    now->next = stack->top;
    stack->top = now;   
}
 
int Pop(Stack *stack) {
    Node *now;
    int re;
    if (IsEmpty(stack))
    {
        return 0;
    }
    now = stack->top;
    re = now->data;
 
    stack->top = now->next;
    free(now);
    return re;
}
 
int main(void) {
    int i;
    Stack stack;
 
    InitStack(&stack);
    for (i = 1; i <= 5; i++)     {
        Push(&stack, i);
    }
 
    while (!IsEmpty(&stack))     {
        printf("%d ", Pop(&stack));
    }
    
    printf("\n");
    return 0;
}
 
 
cs

'프로그래밍 > C언어' 카테고리의 다른 글

curl 을 이용한 클라이언트 get , post 요청  (0) 2022.07.11
Json C로 Json <-> string 변환  (0) 2022.07.11
연결 리스트로 큐 생성  (0) 2022.07.10
Msg Queue  (0) 2022.06.14
파일입출력, 라인단위 읽고쓰기  (0) 2019.07.23

+ Recent posts