-
Notifications
You must be signed in to change notification settings - Fork 0
/
스택 연결리스트.c
55 lines (51 loc) · 903 Bytes
/
스택 연결리스트.c
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
#include <stdio.h>
#include <stdlib.h>
#define INF 9999999999
typedef struct {
int data;
struct Node *next;
}Node;
typedef struct {
Node *top;
}Stack;
void push(Stack *stack, int data) {
Node *node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = stack->top;
stack->top = node;
}
int pop(Stack *stack) {
if (stack->top == NULL) {
printf("Stack underflow has been ocurred\n");
return -INF;
}
Node *node = stack->top;
int data = node->data;
stack->top = node->next;
free(node);
return data;
}
void show(Stack *stack) {
Node *cur = stack->top;
printf("--스택의 최상단--\n");
while (cur != NULL) {
printf("%d\n", cur->data);
cur = cur->next;
}
printf("--스택의 최하단--\n");
}
int main(void) {
Stack s;
s.top = NULL;
pop(&s);
show(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
push(&s, 4);
pop(&s);
push(&s, 5);
show(&s);
system("pause");
return 0;
}