자료구조_c언어_circularLinkedList_01

2021. 8. 25. 10:11개발하는중/자료구조

728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct ListNode {
    char data[4];
    struct ListNode *link;
}listNode;
 
typedef struct {
    listNode *head;
}linkedList_h;
 
linkedList_h* createLinkedList_h() {
    linkedList_h *CL;
    CL = (linkedList_h*)malloc(sizeof(linkedList_h));
    CL->head = NULL;
 
    return CL;
}
 
void printList(linkedList_h *CL) {
    listNode *p;
    printf("CL=(");
    p = CL->head;
    do {
        printf("%s", p->data);
        p = p->link;
        if (p != CL->head) {
            printf(",");
        }
    } while (p != CL->head);
    printf(")\n");
}
 
void insertFirstNode(linkedList_h *CL, char *x) {
    listNode *newNode, *temp;
    newNode = (listNode*)malloc(sizeof(listNode));
    strcpy(newNode->data, x);
    if (CL->head == NULL) {
        CL->head = newNode;
        newNode->link = newNode;
    }
    else {
        temp = CL->head;
        while (temp->link != CL->head) {
            temp = temp->link;
        }
        newNode->link = temp->link;
        temp->link = newNode;
        CL->head = newNode;
    }
}
 
void insertMiddleNode(linkedList_h *CL, listNode *pre, char *x) {
    listNode *newNode;
    newNode = (listNode*)malloc(sizeof(listNode));
    strcpy(newNode->data, x);
    if (pre == NULL) {
        CL->head = newNode;
        newNode->link = newNode;
    }
    else {
        newNode->link = pre->link;
        pre->link = newNode;
    }
}
 
void deleteNode(linkedList_h *CL, listNode *old) {
    listNode *pre;
    if (CL->head == NULL) {
        return;
    }
    if (CL->head->link == CL->head) {
        free(CL->head);
        CL->head = NULL;
        return;
    }
    else if (old == NULL) {
        return;
    }
    else {
        pre = CL->head;
        while (pre->link != old) {
            pre = pre->link;
        }
        pre->link = old->link;
        if (old == CL->head) {
            CL->head = old->link;
        }
        free(old);
    }
}
cs
728x90

'개발하는중 > 자료구조' 카테고리의 다른 글

자료구조 개념  (0) 2023.09.09
자료구조_c언어_tree  (0) 2021.08.25
자료구조_c언어_서클 큐_circleQueue_02  (0) 2021.07.19
자료구조_c언어_linkedList_02  (0) 2021.05.25
자료구조_c언어_linkedList  (0) 2021.05.20