C_C++

(C언어) 연결 리스트(linked list)를 이용한 스택(stack) 구현하기

고니자니 2023. 11. 4. 10:33
반응형

C 언어에서 연결 리스트를 이용해서 스택을 표현해 봅니다.

스택은 후입선출(LIFO) 데이터 구조로 마지막에 입력된 자료가 가장 먼저 제거되는 자료구입니다.

여기서는 스택에 요소를 삽입(push)하고 제거(pop)하는 기능이 포함되어 있습니다.

연결 리스트를 사용하면 스택의 크기가 동적으로 변경될 수 있습니다.

먼저, 스택의 각 노드를 나타내는 구조체를 정의합니다.

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

// 스택의 노드를 나타내는 구조체
struct Node {
    int data;
    struct Node* next;
};

 

다음으로, 스택을 나타내는 구조체를 정의합니다.

struct Stack {
    struct Node* top;
};

 

스택의 요소를 생성하는 함수를 정의합니다.

struct Stack* createStack() {
    struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
    stack->top = NULL;
    return stack;
}

 

이제 스택에 요소를 추가하는 push 함수를 정의합니다.

void push(struct Stack* stack, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = stack->top;
    stack->top = newNode;
}

 

스택에서 요소를 제거하고 제거된 값을 반환하는 pop 함수를 정의합니다.

스택이 비어 있으면 -1을 반환하도록 합니다.

int pop(struct Stack* stack) {
    if (stack->top == NULL) {
        printf("스택이 비어있습니다.\n");
        return -1; // 오류를 나타내는 값을 반환
    }
    struct Node* temp = stack->top;
    int data = temp->data;
    stack->top = temp->next;
    free(temp);
    return data;
}

 

스택의 top 요소를 반환하는 함수를 정의합니다.

int top(struct Stack* stack) {
    if (stack->top == NULL) {
        printf("스택이 비어있습니다.\n");
        return -1; // 오류를 나타내는 값을 반환
    }
    return stack->top->data;
}

 

main() 함수를 포함한 전체 코드는 다음과 같습니다.

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

// 스택의 노드를 나타내는 구조체
struct Node {
    int data;
    struct Node* next;
};

struct Stack {
    struct Node* top;
};

struct Stack* createStack() {
    struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
    stack->top = NULL;
    return stack;
}

void push(struct Stack* stack, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = stack->top;
    stack->top = newNode;
}

int pop(struct Stack* stack) {
    if (stack->top == NULL) {
        printf("스택이 비어있습니다.\n");
        return -1; // 오류를 나타내는 값을 반환
    }
    struct Node* temp = stack->top;
    int data = temp->data;
    stack->top = temp->next;
    free(temp);
    return data;
}

int top(struct Stack* stack) {
    if (stack->top == NULL) {
        printf("스택이 비어있습니다.\n");
        return -1; // 스택이 비어있으면 -1을 반환
    }
    return stack->top->data;
}

int main() {
    struct Stack* stack = createStack();

    push(stack, 100);
    push(stack, 200);
    push(stack, 300);
    push(stack, 400);

    printf("스택의 top 요소: %d\n\n", top(stack));

    printf("스택에서 pop(): %d\n", pop(stack));
    printf("스택에서 pop(): %d\n", pop(stack));
    printf("스택에서 pop(): %d\n", pop(stack));
    printf("스택에서 pop(): %d\n", pop(stack));
    printf("스택에서 pop(): %d\n", pop(stack));

    return 0;
}

(Output)

스택의 top 요소: 400

스택에서 pop(): 400
스택에서 pop(): 300
스택에서 pop(): 200
스택에서 pop(): 100
스택이 비어있습니다.
스택에서 pop(): -1

(C언어) 연결 리스트(linked list)를 이용한 스택(stack) 구현하기

728x90
반응형