별의 공부 블로그 🧑🏻‍💻
728x90
728x170
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
 
#define FALSE 0
#define TRUE 1
 
typedef int element;
typedef struct ListNode {
    element data;
    struct ListNode *link;
} ListNode;
 
typedef struct {
    ListNode *head;        // 헤드 포인터
    int length;                // 노드의 개수
} LinkedListType;
 
void error(char *message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}
 
void init(LinkedListType *list) {
    if (list == NULLreturn;
    list->length = 0;
    list->head = NULL;
}
 
void insert_node(ListNode **phead, ListNode *p, ListNode *new_node) {
    if (*phead == NULL) {
        new_node->link = NULL;
        *phead = new_node;
    }
    else if (p == NULL) {
        new_node->link = *phead;
        *phead = new_node;
    }
    else {
        new_node->link = p->link;
        p->link = new_node;
    }
}
 
void remove_node(ListNode **phead, ListNode *p, ListNode *removed) {
    if (p == NULL) {
        *phead = (*phead)->link;
    }
    else {
        p->link = removed->link;
    }
    free(removed);
}
 
ListNode *get_node_at(LinkedListType *list, int pos) {
    int i;
    ListNode *tmp_node = list->head;
    if (pos < 0return NULL;
    for (i = 0; i < pos; i++) {
        tmp_node = tmp_node->link;
    }
    return tmp_node;
}
 
void add(LinkedListType *list, int position, element data) {
    ListNode *p;
    if ((position >= 0&& (position <= list->length)) {
        ListNode *node = (ListNode *)malloc(sizeof(ListNode));
        if (node == NULL) error("메모리 할당에러");
        node->data = data;
        p = get_node_at(list, position - 1);
        insert_node(&(list->head), p, node);
        list->length++;
    }
}
 
void add_last(LinkedListType *list, element data) {
    add(list, get_length(list), data);
}
 
void add_first(LinkedListType *list, element data) {
    add(list, 0, data);
}
 
int is_empty(LinkedListType *list) {
    if (list->head == NULLreturn 1;
    else return 0;
}
 
int get_length(LinkedListType *list) {
    return list->length;
}
 
void delete(LinkedListType *list, int pos) {
    if (!is_empty(list) && (pos >= 0&& (pos < list->length)) {
        ListNode *= get_node_at(list, pos - 1);
        ListNode *removed = get_node_at(list, pos);
        remove_node(&(list->head), p, removed);
        list->length--;
    }
}
 
element get_entry(LinkedListType *list, int pos) {
    ListNode *p;
    if (pos >= list->length) error("위치 오류");
    p = get_node_at(list, pos);
    return p->data;
}
 
void clear(LinkedListType *list) {
    int i;
    for (i = list->length - 1; i >= 0; i--) {
        delete(list, i);
    }
}
 
void display(LinkedListType *list) {
    int i;
    ListNode *node = list->head;
    printf("( ");
    for (i = 0; i < list->length; i++) {
        printf("%d ", node->data);
        node = node->link;
    }
    printf(" )\n");
}
 
int is_in_list(LinkedListType *list, element item) {
    ListNode *p;
    p = list->head;        // 헤드 포인터에서부터 시작한다.
    while ((p != NULL)) {
        // 노드의 데이터가 item이면
        if (p->data == item) {
            break;
        }
        p = p->link;
    }
    if (p == NULLreturn FALSE;
    else return TRUE;
}
 
int main() {
    LinkedListType list1;
 
    init(&list1);
    add(&list1, 020);
    add_last(&list1, 30);
    add_first(&list1, 10);
    add_last(&list1, 40);
 
    // list1 = (10, 20, 30, 40)
    display(&list1);
 
    // list1 = (10, 20, 30)
    delete(&list1, 3);
    display(&list1);
 
    // list1 = (20, 30)
    delete(&list1, 0);
    display(&list1);
 
    printf("%s\n", is_in_list(&list1, 20== TRUE ? "성공" : "실패");
    printf("%d\n", get_entry(&list1, 0));
}
cs

코드 출처 : C언어로 쉽게 풀어쓴 자료구조 (천인국 외 지음, 생능출판사)


728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖