[자료구조] 덱(Dequeue, 데크)

2024. 3. 27. 01:36·Computer Science/Data Structure

덱(Dequeue)


덱이란?

  • Dequeue : Double-ended Queue

    • 여기선 큐의 출력을 의미하는 Dequeue와 다른 의미로 사용됨

    • a.k.a 데크

  • 전단, 후단 양쪽에서 push, pop이 가능함

  • 스택과 큐의 장점을 모아둔 자료구조

덱의 종류

  • 스크롤 : 입력이 한쪽 끝으로만 가능하도록 제한, 입력 제한 덱이라고도 함

  • 셀프(Shelf) : 출력이 한쪽 끝으로만 가능하도록 제한, 출력 제한 덱이라고도 함

  • 보통 양쪽에서 입력/출력을 모두 하는 경우는 거의 없어 스크롤이나 셀프를 사용한다

덱의 용도

  • 스케줄링

  • 우선순위 조절

덱의 동작

  • 입력과 출력이 양방향에서 가능하고, 입력과 출력의 순서를 맘대로 정할 수 있음

  • 추가 및 삭제의 실행속도는 O(1)

이중 연결 리스트로 덱 구현

#include <stdio.h>
#include <stdlib.h>

// 노드 정의
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

// Dequeue 정의
typedef struct {
    Node* front;
    Node* rear;
} Dequeue;

// Dequeue 초기화
void initDequeue(Dequeue* q) {
    q->front = q->rear = NULL;
}

// 노드 생성 함수
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        exit(1); // 메모리 할당 실패
    }
    newNode->data = data;
    newNode->prev = newNode->next = NULL;
    return newNode;
}

// 앞쪽에 추가
void insertFront(Dequeue* q, int data) {
    Node* newNode = createNode(data);
    if (q->front == NULL) {
        q->front = q->rear = newNode;
    } else {
        newNode->next = q->front;
        q->front->prev = newNode;
        q->front = newNode;
    }
}

// 뒤쪽에 추가
void insertRear(Dequeue* q, int data) {
    Node* newNode = createNode(data);
    if (q->rear == NULL) {
        q->front = q->rear = newNode;
    } else {
        newNode->prev = q->rear;
        q->rear->next = newNode;
        q->rear = newNode;
    }
}

// 앞쪽에서 삭제
int deleteFront(Dequeue* q) {
    if (q->front == NULL) {
        printf("Dequeue is empty\n");
        return -1;
    }
    Node* temp = q->front;
    int data = temp->data;
    q->front = q->front->next;
    if (q->front == NULL) {
        q->rear = NULL;
    } else {
        q->front->prev = NULL;
    }
    free(temp);
    return data;
}

// 뒤쪽에서 삭제
int deleteRear(Dequeue* q) {
    if (q->rear == NULL) {
        printf("Dequeue is empty\n");
        return -1;
    }
    Node* temp = q->rear;
    int data = temp->data;
    q->rear = q->rear->prev;
    if (q->rear == NULL) {
        q->front = NULL;
    } else {
        q->rear->next = NULL;
    }
    free(temp);
    return data;
}

int main() {
    Dequeue q;
    initDequeue(&q);

    insertFront(&q, 10);
    insertRear(&q, 20);
    insertFront(&q, 5);
    insertRear(&q, 25);

    printf("Front: %d\n", deleteFront(&q));
    printf("Rear: %d\n", deleteRear(&q));

    return 0;
}

'Computer Science > Data Structure' 카테고리의 다른 글

[자료구조] 이진 트리  (0) 2024.06.15
[자료구조] 트리 ADT  (0) 2024.06.15
[자료구조] 링크드 큐(Linked Queue)  (0) 2024.03.27
[자료구조] 큐와 원형 큐(순환 큐)  (0) 2024.03.27
[자료구조] 연결 리스트(linked list) 기반 스택 구현  (0) 2024.03.26
'Computer Science/Data Structure' 카테고리의 다른 글
  • [자료구조] 이진 트리
  • [자료구조] 트리 ADT
  • [자료구조] 링크드 큐(Linked Queue)
  • [자료구조] 큐와 원형 큐(순환 큐)
lumana
lumana
배움을 나누는 공간 https://github.com/bebeis
  • lumana
    Brute force Study
    lumana
  • 전체
    오늘
    어제
    • 분류 전체보기 (457)
      • Software Development (27)
        • Performance (0)
        • TroubleShooting (1)
        • Refactoring (0)
        • Test (8)
        • Code Style, Convetion (0)
        • DDD (0)
        • Software Engineering (18)
      • Java (71)
        • Basic (5)
        • Core (21)
        • Collection (7)
        • 멀티스레드&동시성 (13)
        • IO, Network (8)
        • Reflection, Annotation (3)
        • Modern Java(8~) (12)
        • JVM (2)
      • Spring (53)
        • Framework (12)
        • MVC (23)
        • Transaction (3)
        • AOP (11)
        • Boot (0)
        • AI (0)
      • DB Access (1)
        • Jdbc (1)
        • JdbcTemplate (0)
        • JPA (14)
        • Spring Data JPA (0)
        • QueryDSL (0)
      • Computer Science (130)
        • Data Structure (27)
        • OS (14)
        • Database (10)
        • Network (21)
        • 컴퓨터구조 (6)
        • 시스템 프로그래밍 (23)
        • Algorithm (29)
      • HTTP (8)
      • Infra (1)
        • Docker (1)
      • 프로그래밍언어론 (15)
      • Programming Language(Sub) (77)
        • Kotlin (1)
        • Python (25)
        • C++ (51)
        • JavaScript (0)
      • FE (11)
        • HTML (1)
        • CSS (9)
        • React (0)
        • Application (1)
      • Unix_Linux (0)
        • Common (0)
      • PS (13)
        • BOJ (7)
        • Tip (3)
        • 프로그래머스 (0)
        • CodeForce (0)
      • Book Review (4)
        • Clean Code (4)
      • Math (3)
        • Linear Algebra (3)
      • AI (7)
        • DL (0)
        • ML (0)
        • DA (0)
        • Concepts (7)
      • 프리코스 (4)
      • Project Review (6)
      • LegacyPosts (11)
      • 모니터 (0)
      • Diary (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
lumana
[자료구조] 덱(Dequeue, 데크)
상단으로

티스토리툴바