ABOUT ME

Today
Yesterday
Total
  • [Data Structure] 큐(Queue) & 스택(Stack)
    Data Structure 2024. 5. 16. 23:59

    큐(Queue)와 스택(Stack)자료구조에 대해 정리하였습니다.



    < 큐(Queue) >

    기본적인 자료구조 중 한가지로, 먼저 집어 넣은 데이터가 먼저 나오는 FIFO(First In First Out) 구조로 저장하는 형식을 말합니다.

    일반 큐 (Linear Queue), 원형 큐 (Circular Queue), 우선순위 큐 (Priority Queue), 블로킹 큐 (Blocking Queue), 동기화 큐 (Synchronized Queue) 등 다양한 큐의 종류가 있습니다.

     

     

    Ex)

    - 프린터 대기열

    - 프로세스 관리

    - 운영 체제의 작업 스케줄링

     

    class Queue {
      constructor() {
        this.items = [];
      }
    
      // 큐에 요소 추가
      enqueue(element) {
        this.items.push(element);
      }
    
      // 큐에서 요소 제거 및 반환
      dequeue() {
        if (this.isEmpty()) {
          return "Queue is empty";
        }
        return this.items.shift();
      }
    
      // 큐의 첫 번째 요소 반환 (제거하지 않음)
      front() {
        if (this.isEmpty()) {
          return "Queue is empty";
        }
        return this.items[0];
      }
    
      // 큐가 비어 있는지 확인
      isEmpty() {
        return this.items.length === 0;
      }
    
      // 큐의 크기 반환
      size() {
        return this.items.length;
      }
    
      // 큐 요소들을 문자열로 반환
      print() {
        return this.items.toString();
      }
    }
    
    // 큐 사용 예시
    const queue = new Queue();
    
    console.log(queue.isEmpty()); // true
    
    queue.enqueue(10);
    queue.enqueue(20);
    queue.enqueue(30);
    
    console.log(queue.print()); // 10,20,30
    
    console.log(queue.size()); // 3
    
    console.log(queue.isEmpty()); // false
    
    console.log(queue.dequeue()); // 10
    
    console.log(queue.front()); // 20

     

     


    < 스택(Stack) >

    데이터를 순차적으로 쌓아 올리는 구조로, LIFO(Last In, First Out) 원칙에 따라 가장 마지막에 삽입된 데이터가 가장 먼저 삭제되는 방식입니다.

     

    Ex)

    - 접시 더미에서 가장 위에 있는 접시 꺼내기

    - 페이지 뒤로가기/앞으로가기 스택

     

     

    class Stack {
      constructor() {
        this.items = [];
      }
    
      // 스택에 데이터를 추가합니다.
      push(element) {
        this.items.push(element);
      }
    
      // 스택의 맨 위 데이터를 제거하고 반환합니다.
      pop() {
        if (this.isEmpty()) {
          return "Stack is empty";
        }
        return this.items.pop();
      }
    
      // 스택의 맨 위 데이터를 제거하지 않고 반환합니다.
      peek() {
        if (this.isEmpty()) {
          return "Stack is empty";
        }
        return this.items[this.items.length - 1];
      }
    
      // 스택이 비어 있는지 확인합니다.
      isEmpty() {
        return this.items.length === 0;
      }
    
      // 스택에 있는 데이터의 개수를 반환합니다.
      size() {
        return this.items.length;
      }
    
      // 스택의 모든 요소를 문자열로 반환합니다. (선택사항)
      printStack() {
        return this.items.toString();
      }
    }
    
    // 스택 사용 예제
    const stack = new Stack();
    
    stack.push(1);
    stack.push(2);
    stack.push(3);
    console.log(stack.printStack()); // 출력: 1,2,3
    
    console.log(stack.pop()); // 출력: 3
    console.log(stack.peek()); // 출력: 2
    console.log(stack.isEmpty()); // 출력: false
    console.log(stack.size()); // 출력: 2
    console.log(stack.printStack()); // 출력: 1,2

     

     

     

    'Data Structure' 카테고리의 다른 글

    댓글

Designed by Tistory.