SHyeonL / Future_Internet_LAB_Algorithm

미래 인터넷 연구실 알고리즘 스터디
0 stars 0 forks source link

10828번 - 스택 #3

Open SHyeonL opened 1 year ago

SHyeonL commented 1 year ago
import sys
input = sys.stdin.readline

n = int(input())
stk = []
for _ in range(n):
    x = list(map(str, input().split()))
    if x[0] == 'push':
        stk.append(int(x[1]))
    elif x[0] == 'pop':
        if stk:
            print(stk.pop())
        else:
            print(-1)
    elif x[0] == 'size':
        print(len(stk))
    elif x[0] == 'empty':
        if stk:
            print(0)
        else:
            print(1)
    elif x[0] == 'top':
        if stk:
            print(stk[-1])
        else:
            print(-1)
rhkrdkdms123 commented 1 year ago
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STACK_SIZE 10000

typedef int element;

typedef struct {
    element data[MAX_STACK_SIZE];
    int top;
} StackType;

void init_stack(StackType* s) {
    s->top = -1; //스택이 공백상태면 top은 -1
}

int is_empty(StackType* s) {
    return (s->top == -1);
}

int is_full(StackType* s) {
    return (s->top == (MAX_STACK_SIZE - 1));
}

void push(StackType* s, element item) {
    if (is_full(s)) {
        fprintf(stderr, "스택 포화 에러\n");
        return;
    }
    else s->data[++(s->top)] = item;
}

element pop(StackType* s) {
    if (is_empty(&s)) {
        fprintf(stderr, "스택 공백 에러\n");
        exit(1);
    }
    else return s->data[(s->top)--];
}

int main(void) {
    int count;
    char cmd[20];
    int num;

    StackType s;
    init_stack(&s);

    scanf("%d", &count);
    getchar();

    for (int i = 0; i < count; i++) {
        scanf("%s", cmd);
        //명령어 구별
        if (!strcmp(cmd, "push")) {
            scanf("%d", &num);
            push(&s, num);
        }
        else if (!strcmp(cmd, "pop")) {
            printf("%d\n", is_empty(&s) ? -1 : pop(&s));
        }
        else if (!strcmp(cmd, "size")) {
            printf("%d\n", s.top + 1);
        }
        else if (!strcmp(cmd, "empty")) {
            printf("%d\n", is_empty(&s));
        }
        else if (!strcmp(cmd, "top")) {
            printf("%d\n", is_empty(&s) ? -1 : s.data[s.top]);
        }
    }

    return 0;
}
s5830360 commented 1 year ago

import sys

n = int(sys.stdin.readline()) #명령의 수를 문자열로 받아 int형으로 변환

stack = [] #리스트로 스택 만들기

for i in range(n): #명령의 수만큼 반복 command = sys.stdin.readline().split() #명령을 문자열로 받은 다음 split함수를 이용해 공백을 기준으로 문자열을 나눠 리스트로 만들기

if command[0] == 'push': #command 리스트의 0번째 원소가 push라면 stack.append(command[1]) #1번째 원소를 stack에 추가

elif command[0] == 'pop': #command 리스트의 0번째 원소가 pop이라면 if(len(stack)==0): print(-1) #stack이 비어있다면 -1 출력 else: print(stack.pop()) #비어있지 않다면 pop()

elif command[0] == 'size': #command 리스트의 0번째 원소가 size라면 print(len(stack)) #stack의 길이 출력

elif command[0] == 'empty': #command 리스트의 0번째 원소가 empty라면 if(len(stack) == 0): print(1) #stack이 비어있다면 1 출력 else: print(0) #비어있지 않다면 0 출력

elif command[0] == 'top': #command 리스트의 0번째 원소가 top이라면 if(len(stack)==0): print(-1) #stack이 비어있다면 -1 출력 else: print(stack[-1]) #비어있지 않다면 stack의 가장 마지막 원소 출력

yuneojin commented 1 year ago
import sys
T = int(sys.stdin.readline())
stack = []

def push(n):
    stack.append(n)

def top():
    if empty() == 0:
        return stack[-1]
    else:
        return -1

def pop():
    if empty() == 0:
        return stack.pop()
    else:
        return -1

def size():
    return len(stack)

def empty():
    if len(stack)==0:
        return 1
    else:
        return 0

for i in range(T):
    command = sys.stdin.readline().split()
    if command[0] == "top":
        print(top())
    elif command[0] == "size":
        print(size())
    elif command[0] == "pop":
        print(pop())
    elif command[0] == "empty":
        print(empty())
    elif command[0] == "push":
        push(command[1])