-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_구현
More file actions
148 lines (135 loc) · 2.48 KB
/
stack_구현
File metadata and controls
148 lines (135 loc) · 2.48 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#define MAX_STACK_SIZE 100
// push data to stack
// data to update : stack, size, top_index
void push( int new_data, int stack[], int *top_index, int *cur_size )
{
// check that stack is full
if (*cur_size == 100)
{
printf( "Stack is full\n" );
return;
}
// push data to stack
// TODO
stack[*cur_size] = new_data;
*cur_size += 1;
*top_index += 1;
}
// pop data from stack
// data to update : stack, size, top_index
void pop( int stack[], int *top_index, int *cur_size )
{
// check that stack is empty
if (*cur_size == 0)
{
printf( "Stack is empty\n" );
return;
}
// pop data from stack
// TODO
*cur_size -= 1;
*top_index -= 1;
}
// return top data
// data to update : none
int top( int stack[], int top_index, int cur_size )
{
if (cur_size == 0)
{
printf( "Stack is empty\n" );
return 0;
}
// TODO
return stack[top_index];
}
// print stack
// data to update : none
void print_stack( int stack[], int cur_size )
{
int i;
printf( "----stack----\n" );
// check that stack is empty
if (cur_size == 0)
{
printf( "Stack is empty\n" );
printf( "-------------\n" );
return;
}
// print all stack data
// ex )
// ----stack----
// 1 <- top
// 3
// 4
// 5
// -------------
// TODO
for (i = 1; i <= cur_size; i++) {
if (i == 1) {
printf("%d <- top\n", stack[cur_size - 1]);
} else {
printf("%d\n", stack[cur_size - i]);
}
}
printf( "-------------\n" );
}
int main( void )
{
int stack[MAX_STACK_SIZE];
int top_index = -1;
int cur_size = 0;
while ( 1 )
{
int select;
printf( "\n0. quit\n" );
printf( "1. push\n" );
printf( "2. pop\n" );
printf( "3. print top\n" );
printf( "4. print size\n" );
printf( "5. print stack\n" );
printf( " > " );
scanf( "%d", &select );
switch( select )
{
case 0: // quit
return 0;
case 1: // push
{
int new_data;
printf( "input new data : " );
scanf( "%d", &new_data );
push( new_data, stack, &top_index, &cur_size );
print_stack( stack, cur_size );
break;
}
case 2: // pop
{
pop( stack, &top_index, &cur_size );
print_stack( stack, cur_size );
break;
}
case 3: // print top
{
printf( "Top : %d\n", top( stack, top_index, cur_size ) );
break;
}
case 4: // print current stack size
{
printf( "Current size : %d\n", cur_size );
break;
}
case 5: // print stack
{
print_stack( stack, cur_size );
break;
}
default :
{
printf( "Wrong input!\n" );
break;
}
}
}
return 0;
}