본문 바로가기
PS 문제 풀이/Jungol

[Jungol] 정올 3690 stack api

by 까망 하르방 2021. 3. 18.
반응형

출처: jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=3039&sca=99&page=24

Approach

Stack 자료구조를 구현하는 문제

 

  [스택] Stack 이란? 

 

[스택] Stack 이란?

Stack 이란? 스택 (Stack) 특징을 가장 잘 나타내는 표현은 후입선출(Last In First Out, LIFO) 이다. ▶ 스택(Stack)은 삽입(push)과 삭제(pop)이 한쪽 끝에서만 일어나는 구조 가장 상단에 위치한 원소를 가리.

zoosso.tistory.com


#ifndef NULL
#define NULL 0
#endif
const int SIZE = 100010;
struct Node {
    int num;
    Node* next;
    Node() {
        num = 0, next = NULL;
    }
    Node(int ni, Node* np) {
        num = ni;
        next = np;
    }
    ~Node() {
        delete next;  
    }
} buf[SIZE];
int bcnt;
 
struct Stack {
    Node* head;
    int cnt;
    ~Stack() {
        delete head;
    }
    
    bool empty() { return cnt == 0; }
    int size() { return cnt; }
    int top() { return head->num; }
    void push(int num) {
        cnt++;
        head = new Node(num, head);
    }
    void pop() {
        if (cnt == 0)  return;
        cnt--;
        Node* tmp = head;
        head = tmp -> next;
        tmp -> next = NULL;
        delete tmp;
    }
}stkObject;
 
Stack* newStack() {
    return new Stack();
}
void delStack(Stack* stk) {
    delete stk;
}
bool empty(Stack* stk) {
    return stk->empty();
}
int size(Stack* stk) {
    return stk->size();
}
int top(Stack* stk) {
    return stk->top();
}
void push(Stack* stk, int num) {
    stk->push(num);
}
void pop(Stack* stk) {
    stk->pop();
}

 

반응형

댓글