728x90
728x90
01 λ¬Έμ
02 μκ³ λ¦¬μ¦
- stackμ κΈ°λ³Έ ꡬ쑰λ κ΅¬μ‘°μ²΄λ‘ μ μνμλ€. stackμ μ΄κΈ°ννλ stack_init ν¨μμ λͺ λ Ήμ μν push, pop, size, is_empty, top ν¨μ μμ±
- strcmp ν¨μλ₯Ό μ΄μ©ν΄ λ¬Έμμ΄μ λΉκ΅, λͺ λ Ήμ ꡬλΆνλ€.
- pushμ κ²½μ°μλ μ«μμ ν¨κ» μ λ ₯λλ―λ‘ λμ΄μ°κΈ°λ₯Ό ν ν°μΌλ‘ μΈμνμ¬ push μ nμ ꡬλΆν ν nμ atoi(str -> int) ν¨μλ₯Ό μ΄μ©ν΄ μ μ λ³μμ λμ νλ€.
03 μ½λ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 10000
typedef struct array_stack
{
int arr[LEN];
int top;
}array_stack;
void stack_init(array_stack *pstack);
void push(array_stack *pstack, int x);
void pop(array_stack *pstack);
void size(array_stack *pstack);
void is_empty(array_stack *pstack);
void top(array_stack *pstack);
int main(void)
{
int num;
char inst[15] = { 0, };
array_stack stack;
stack_init(&stack);
scanf("%d", &num);
getchar();
while (num--)
{
gets(inst);
if (strncmp(inst, "push", 4) == 0)
{
int x;
char *pch;
pch = strtok(inst, " ");
pch = strtok(NULL, " ");
x = atoi(pch);
push(&stack, x);
}
else if (strcmp(inst, "pop") == 0) pop(&stack);
else if (strcmp(inst, "size") == 0) size(&stack);
else if (strcmp(inst, "empty") == 0) is_empty(&stack);
else if (strcmp(inst, "top") == 0) top(&stack);
}
}
void stack_init(array_stack *pstack)
{
pstack->top = -1;
}
void push(array_stack *pstack, int x)
{
++pstack->top;
pstack->arr[pstack->top] = x;
}
void pop(array_stack *pstack)
{
if (pstack->top == -1)
printf("-1\n");
else
{
printf("%d\n", pstack->arr[pstack->top]);
pstack->top--;
}
}
void size(array_stack *pstack)
{
printf("%d\n", pstack->top + 1);
}
void is_empty(array_stack *pstack)
{
if (pstack->top == -1)
printf("1\n");
else
printf("0\n");
}
void top(array_stack *pstack)
{
if (pstack->top == -1)
printf("-1\n");
else
printf("%d\n", pstack->arr[pstack->top]);
}
728x90
728x90
'π Algorithm > μκ³ λ¦¬μ¦-Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μκ³ λ¦¬μ¦] Day2 - 2775λ² λΆλ νμ₯μ΄ λ ν μΌ (0) | 2018.08.04 |
---|---|
[μκ³ λ¦¬μ¦] Day1 - κ΄νΈ (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 - νμ (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 - μ ν λλ² (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 νκ· μ λκ² μ§ (0) | 2018.08.04 |