스택스택
🗒️

스택

다루고 있는 개념
난이도
Type
자료
file

스택의 정의

  • 스택(stack)은 목록 끝에서만 데이터가 들어오고 나가는 자료구조이다. 사진을 참고하면 좀 더 쉽게 이해할 수 있다. Python으로 구현 할 때에는 listappendpop을 이용하여 구현할 수 있으며, Javascript에서는 Arraypushpop을 이용하여 구현할 수 있다. Java에서는 Stack 클래스를 이용하여 push, pop 을 사용할 수 있다.
  • 스택은 한 쪽 끝에서만 자료를 넣거나 뺄 수 있는 선형 구조(LIFO - Last In First Out)으로 되어 있다. 자료를 넣는 것을 '밀어넣는다' 하여 푸쉬(push)라고 하고 반대로 넣어둔 자료를 꺼내는 것을 (pop)이라고 하는데, 이때 꺼내는 자료는 가장 최근에 푸쉬한 자료부터 나오게 된다. 이처럼 나중에 넣은 값이 먼저 나오는 것을 LIFO 구조라고 한다.
  • 스택이 비었다면 1을 반환하고, 그렇지 않다면 0을 반환한다.

스택의 연산

여기서 사용하는 메서드 이름은 wiki 출처를 따릅니다. 실제 구현되어 있는 언어별 메서드와는 다른 개념, 즉 자료구조의 개념입니다.
  • top() : 스택의 가장 윗(마지막) 데이터를 반환한다.
  • pop() : 스택의 가장 윗(마지막) 데이터를 삭제한다
  • push() : 스택의 가장 윗 데이터로 top이 가리키는 자리 위에(top = top + 1) 메모리를 생성,
    • 데이터 x를 넣는다.
  • 배열로 구현한 스택 (java)
public class StackArray { public static void main(String[] args) { int size = 5; Stack stack = new Stack(size); stack.push(1); stack.printStack(); stack.push(2); stack.printStack(); stack.push(3); stack.printStack(); stack.pop(); stack.printStack(); } } class Stack { private int top; private int[] stackArr; private int size; public Stack(int size) { top = -1; this.stackArr = new int[size]; this.size = size; } public void push(int val) { if (isFull()) { throw new ArrayIndexOutOfBoundsException(); } stackArr[++top] = val; } public int pop() { if (isEmpty()) { throw new ArrayIndexOutOfBoundsException(); } return stackArr[top--]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == size - 1); } public void printStack() { if (isEmpty()) { System.out.println("Stack is empty."); } else { for (int i = 0; i <= top; i++) { System.out.print(stackArr[i] + " "); } } System.out.println(""); } }
// 출력 1 1 2 1 2 3 1 2
 
  • 리스트로 구현한 스택 (java)
import java.util.EmptyStackException; public class StackList { public static void main(String[] args) { Stack stack = new Stack(); stack.push(1); stack.printStack(); stack.push(2); stack.printStack(); stack.push(3); stack.printStack(); stack.pop(); stack.printStack(); } } class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class Stack { private Node top; public void push(int val) { Node node = new Node(val); node.next = top; top = node; } public int pop() { if (isEmpty()) { throw new EmptyStackException(); } Node node = top; int data = node.data; top = top.next; node = null; return data; } public boolean isEmpty() { return (top == null); } public void printStack() { if (isEmpty()) { System.out.println("stack is empty"); } else { Node node = this.top; while (node != null) { System.out.print(node.data + " "); node = node.next; } System.out.println(""); } } }
// 출력 1 2 1 3 2 1 2 1
 
  • Stack 라이브러리 (java)
import java.util.Stack; public class Main { public static void main(String args[]) { Stack<Integer> stack = new Stack<Integer>(); stack.push(1); System.out.println(stack.toString()); stack.push(2); System.out.println(stack.toString()); stack.push(3); System.out.println(stack.toString()); stack.pop(); System.out.println(stack.toString()); } }
// 출력 [1] [1, 2] [1, 2, 3] [1, 2]
 
  • Python 코드
class Stack(list) : push = list.append pop = list.pop def is_empty(self) : if not self : return True else : return False stack = Stack() stack.push(1) print("stack", stack) stack.push(2) print("stack", stack) stack.push(3) print("stack", stack) stack.pop() print("stack", stack)
Out[-] stack [1] stack [1,2] stack [1,2,3] stack [1,2]
 
  • Javascript 코드
class Stack { constructor() { this.arr = []; } push(x) { this.arr.push(x); } pop() { return this.arr.pop(); } empty() { if(arr.length == 0) { return 1; } else { return 0; } } } const stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); console.log(stack.pop()); //3 console.log('stack', stack); //[1, 2]