스택

https://www.acmicpc.net/problem/10828

#include <iostream>
#include <vector>
using namespace std;
class stack {
private:
int a[10000];
int i = -1;
public:
void push(int x);
int pop();
int size();
int empty();
int top();
};
void stack::push(int x) { a[++i] = x; }
int stack::pop() {
if (empty()) return -1;
else return a[i--];
}
int stack::size() { return (empty()) ? 0 : i + 1; }
int stack::empty() { return (i == -1) ? 1 : 0; }
int stack::top() {
if (empty()) return -1;
else return a[i];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
stack stk;
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (s == "push") {
int t;
cin >> t;
stk.push(t);
}
else if (s == "pop") cout << stk.pop() << '\n';
else if (s == "size") cout << stk.size() << '\n';
else if (s == "empty") cout << stk.empty() << '\n';
else if (s == "top") cout << stk.top() << '\n';
}
return 0;
}
view raw b10828.cpp hosted with ❤ by GitHub
using System;
using System.Text;
class stack {
private int[] a = new int[10000];
private int i = -1;
public void push(int x) { a[++i] = x; }
public int pop() {
if (empty() == 1) return -1;
else return a[i--];
}
public int size() { return i + 1; }
public int empty() { return (i == -1) ? 1 : 0; }
public int top() {
if (empty() == 1) return -1;
else return a[i];
}
}
class Program {
public static void Main() {
StringBuilder sb = new StringBuilder();
stack stk = new stack();
int n = int.Parse(Console.ReadLine());
while (n-- > 0) {
string[] s = Console.ReadLine().Split(' ');
if (s[0] == "push") stk.push(int.Parse(s[1]));
else if (s[0] == "pop") sb.Append(stk.pop().ToString() + '\n');
else if (s[0] == "size") sb.Append(stk.size().ToString() + '\n');
else if (s[0] == "empty") sb.Append(stk.empty().ToString() + '\n');
else if (s[0] == "top") sb.Append(stk.top().ToString() + '\n');
}
Console.Write(sb.ToString());
}
}
view raw b10828.cs hosted with ❤ by GitHub
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
class stack {
private int[] a = new int[10000];
private int i = -1;
public void push(int x) { a[++i] = x; }
public int pop() {
if (empty() == 1) return -1;
else return a[i--];
}
public int size() { return i + 1; }
public int empty() { return (i == -1) ? 1 : 0; }
public int top() {
if (empty() == 1) return -1;
else return a[i];
}
}
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
stack stk = new stack();
int n = Integer.parseInt(br.readLine());
while (n-- > 0) {
String[] s = br.readLine().split(" ");
if (s[0].compareTo("push") == 0) {
int t = Integer.parseInt(s[1]);
stk.push(t);
}
else if (s[0].compareTo("pop") == 0) bw.write(stk.pop() + "\n");
else if (s[0].compareTo("size") == 0) bw.write(stk.size() + "\n");
else if (s[0].compareTo("empty") == 0) bw.write(stk.empty() + "\n");
else if (s[0].compareTo("top") == 0) bw.write(stk.top() + "\n");
}
bw.close();
}
}
view raw b10828.java hosted with ❤ by GitHub
import sys
input = sys.stdin.readline
print = sys.stdout.write
class stack:
a = [0 for _ in range(10000)]
i = -1
def push(self, x):
self.i += 1
self.a[self.i] = x
def pop(self):
if self.empty() == 1: return -1
else:
self.i -= 1
return self.a[self.i + 1]
def size(self): return self.i + 1
def empty(self): return 1 if self.i == -1 else 0
def top(self):
if self.empty() == 1: return -1
else: return self.a[self.i]
stk = stack()
n = int(input())
for _ in range(n):
s = input().split()
if s[0] == "push": stk.push(int(s[1]))
elif s[0] == "pop": print(str(stk.pop()) + '\n')
elif s[0] == "size": print(str(stk.size()) + '\n')
elif s[0] == "empty": print(str(stk.empty()) + '\n')
elif s[0] == "top": print(str(stk.top()) + '\n')
view raw b10828.py hosted with ❤ by GitHub
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
class stack{
private var a = Array<Int>(10000) { 0 }
private var i: Int = -1
public fun push(x: Int) { a[++i] = x }
public fun pop(): Int {
if (empty() == 1) return -1
else return a[i--]
}
public fun size(): Int { return i + 1 }
public fun empty(): Int { return if (i == -1) 1 else 0 }
public fun top(): Int {
if (empty() == 1) return -1
else return a[i]
}
}
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.`out`));
var stk = stack()
var n = Integer.parseInt(br.readLine());
while (n-- > 0) {
val s = br.readLine().split(" ")
if (s[0].compareTo("push") == 0) {
val t = Integer.parseInt(s[1])
stk.push(t)
}
else if (s[0].compareTo("pop") == 0) bw.write(stk.pop().toString() + "\n")
else if (s[0].compareTo("size") == 0) bw.write(stk.size().toString() + "\n")
else if (s[0].compareTo("empty") == 0) bw.write(stk.empty().toString() + "\n")
else if (s[0].compareTo("top") == 0) bw.write(stk.top().toString() + "\n")
}
bw.close()
}
view raw b10828.kt hosted with ❤ by GitHub
class stack {
constructor() {
this.a = Array(10000)
this.i = -1
}
push(x) { this.a[++this.i] = x }
pop() {
if (this.empty() == 1) return -1
else return this.a[this.i--]
}
size() { return this.i + 1 }
empty() { return (this.i == - 1) ? 1 : 0 }
top() {
if (this.empty() == 1) return -1
else return this.a[this.i]
}
}
let stk = new stack()
let ip = require('fs').readFileSync(0).toString().split('\n')
let n = parseInt(ip[0]), rst = ""
for (let i = 1; i <= n; i++) {
let s = ip[i].split(' ')
if (s[0] == "push") stk.push(parseInt(s[1]))
else if (s[0] == "pop") rst += stk.pop() + '\n'
else if (s[0] == "size") rst += stk.size() + '\n'
else if (s[0] == "empty") rst += stk.empty() + '\n'
else if (s[0] == "top") rst += stk.top() + '\n'
}
console.log(rst)
view raw b10828.js hosted with ❤ by GitHub
import 'dart:io';
class stack {
List<int> a = List<int>.filled(10000, 0);
int i = -1;
void push(int x) { a[++i] = x; }
int pop() {
if (empty() == 1) return -1;
else return a[i--];
}
int size() { return i + 1; }
int empty() { return (i == -1) ? 1 : 0; }
int top() {
if (empty() == 1) return -1;
else return a[i];
}
}
void main() {
stack stk = new stack();
String rst = "";
int n = int.parse(stdin.readLineSync()!);
while (n-- > 0) {
List<String> s = stdin.readLineSync()!.split(' ');
if (s[0] == "push") stk.push(int.parse(s[1]));
else if (s[0] == "pop") rst += stk.pop().toString() + '\n';
else if (s[0] == "size") rst += stk.size().toString() + '\n';
else if (s[0] == "empty") rst += stk.empty().toString() + '\n';
else if (s[0] == "top") rst += stk.top().toString() + '\n';
}
print(rst);
}
view raw b10828.dart hosted with ❤ by GitHub

풀이

문제 제목 그대로 자료구조 스택을 구현하면 된다.