컴퓨터공학 💻 도서관📚

원형연결리스트 C언어 본문

💯🌊자료구조&알고리즘/C언어

원형연결리스트 C언어

들판속초록풀 2025. 4. 8. 21:54

 

 

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

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

// 원형 연결 리스트 구조체
typedef struct {
    Node* head;
} CircularLinkedList;

// 리스트 초기화
void initList(CircularLinkedList* list) {
    list->head = NULL;
}

// 노드 생성
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 끝에 삽입
void insert(CircularLinkedList* list, int data) {
    Node* newNode = createNode(data);

    if (list->head == NULL) {
        list->head = newNode;
        newNode->next = list->head;
    } else {
        Node* temp = list->head;
        while (temp->next != list->head)
            temp = temp->next;
        temp->next = newNode;
        newNode->next = list->head;
    }
}

// 중간 삽입
void insertAt(CircularLinkedList* list, int index, int data) {
    Node* newNode = createNode(data);

    if (index == 0) {
        if (list->head == NULL) {
            list->head = newNode;
            newNode->next = list->head;
        } else {
            Node* temp = list->head;
            while (temp->next != list->head)
                temp = temp->next;
            newNode->next = list->head;
            temp->next = newNode;
            list->head = newNode;
        }
    } else {
        Node* temp = list->head;
        for (int i = 0; i < index - 1; i++) {
            temp = temp->next;
            if (temp == list->head) {
                printf("❌ 위치를 찾을 수 없습니다.\n");
                free(newNode);
                return;
            }
        }
        newNode->next = temp->next;
        temp->next = newNode;
    }
}

// 검색
int search(CircularLinkedList* list, int data) {
    if (list->head == NULL) return -1;

    Node* temp = list->head;
    int index = 0;
    do {
        if (temp->data == data)
            return index;
        temp = temp->next;
        index++;
    } while (temp != list->head);

    return -1;
}

// 삭제
void deleteNode(CircularLinkedList* list, int data) {
    if (list->head == NULL) {
        printf("❌ 삭제할 노드가 없습니다.\n");
        return;
    }

    Node *current = list->head, *prev = NULL;

    // head 삭제
    if (current->data == data) {
        if (current->next == current) {
            free(current);
            list->head = NULL;
        } else {
            Node* last = list->head;
            while (last->next != list->head)
                last = last->next;
            last->next = current->next;
            list->head = current->next;
            free(current);
        }
        return;
    }

    // 그 외 삭제
    do {
        prev = current;
        current = current->next;
        if (current->data == data) {
            prev->next = current->next;
            free(current);
            return;
        }
    } while (current != list->head);

    printf("❌ 값 %d을(를) 찾을 수 없습니다.\n", data);
}

// 출력
void printList(CircularLinkedList* list) {
    if (list->head == NULL) {
        printf("리스트가 비어 있습니다.\n");
        return;
    }

    Node* temp = list->head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != list->head);
    printf("(head)\n");
}

// 메모리 해제
void freeList(CircularLinkedList* list) {
    if (list->head == NULL) return;

    Node* temp = list->head->next;
    while (temp != list->head) {
        Node* next = temp->next;
        free(temp);
        temp = next;
    }
    free(list->head);
    list->head = NULL;
}

// 테스트
int main() {
    CircularLinkedList list;
    initList(&list);

    insert(&list, 10);
    insert(&list, 20);
    insert(&list, 30);
    insert(&list, 40);

    printList(&list);

    insertAt(&list, 2, 25);
    printList(&list);

    int idx = search(&list, 25);
    printf("🔍 25는 %d번째 위치에 있습니다.\n", idx);

    deleteNode(&list, 10);
    printList(&list);

    deleteNode(&list, 30);
    printList(&list);

    deleteNode(&list, 99);
    printList(&list);

    freeList(&list);
    return 0;
}

'💯🌊자료구조&알고리즘 > C언어' 카테고리의 다른 글

집합 자료구조 C언어  (0) 2025.04.13
이중연결리스트 (C언어)  (0) 2025.04.04
단일 연결 리스트  (0) 2025.03.31
하노이 탑 (재귀함수)  (0) 2025.03.25
재귀함수 (C언어)  (0) 2025.02.12
Comments