공부/자료구조

C로 리스트 구현하기~

choryDev 2019. 5. 3. 15:34

수업시간에 배운 리스트 내용을 정리한 것 입니다.

 

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
#include  
#include  
 
#define MAX 30 
 
typedef char element; 
typedef struct { 
    char list[MAX];    // 배열 정의
    int length;    // 현재 배열에 저장된 항목들의 개수
} ArrayListType; 
 
// 리스트 초기화
void init(ArrayListType *L) 
    L->length = 0
 
// 리스트가 비어 있으면 1을 반환
// 그렇지 않으면 0을 반환
int is_empty(ArrayListType *L) 
    return L->length == 0
 
// 리스트가 가득 차 있으면 1을 반환
// 그렇지 많으면 1을 반환
int is_full(ArrayListType *L) 
return L->length == MAX; 
 
void display(ArrayListType *L) { 
    int i = 0
    for (i = 0; i < L->length; i++) { 
    printf("[%d] %c\n", i + 1, L->list[i]); 
 
// position: 삽입하고자 하는 위치
// item: 삽입하고자 하는 자료
void add(ArrayListType *L, int position, element item) 
    if (!is_full(L) && (position >= 0&& 
    (position <= L->length)) { 
        int i; 
        for (i = (L->length - 1); i >= position; i--
            L->list[i + 1= L->list[i]; 
            L->list[position] = item; 
            L->length++
    } 
 
// position: 삭제하고자 하는 위치
// 반환값: 삭제되는 자료
element delete(ArrayListType *L, int position) 
    int i; 
    element item; 
 
    if (position < 0 || position >= L->length) 
        return -1
    item = L->list[position]; 
 
    for (i = position; i < (L->length - 1); i++
        L->list[i] = L->list[i + 1]; 
        L->length--
 
return item; 
 
void add_last(ArrayListType *L, element item) { 
 
    L->list[L->length] = item; 
    L->length++
//add(L, L->length, item); 
 
void add_first(ArrayListType *L, element item) { 
 
if (!is_full(L)) { 
    int i; 
 
    for (i = (L->length - 1); i >= 0; i--
        L->list[i + 1= L->list[i]; 
        L->list[0= item; 
        L->length++
    } 
//add(L, 0, item); 
 
void replace(ArrayListType *L, int pos, element item) { 
    L->list[pos] = item; 
 
int is_in_list(ArrayListType *L, element item) { 
    int i = 0
    for (i = 0; i < L->length; i++) { 
        if (L->list[i] == item) 
            return 0
        } 
    return 1
 
int get_entry(ArrayListType *L, element item) { 
//item의 위치를 반환
    int i = 0
 
    for (i = 0; i < L->length; i++) { 
        if (L->list[i] == item) 
            return i; 
    } 
    return -1
 
int get_length(ArrayListType *L) { 
 
    return L->length; 
 
void main() { 
    ArrayListType L; 
    
    init(&L); 
    
    add(&L, 0'a'); 
    add(&L, 1'b'); 
    add(&L, 2'c'); 
    add(&L, 3'd'); 
    add(&L, 1'e'); //1 
    
    replace(&L, get_entry(&L, 'b'), 'f');//2 
    
    add_first(&L, 'g');//3 
    
    add_last(&L, 'h');//4 
    
    delete(&L, get_entry(&L, 'c'));//5 
    
    display(&L);//6 
}
cs

 

'공부 > 자료구조' 카테고리의 다른 글

큐~  (0) 2019.05.31
스택 괄호검사  (0) 2019.05.24
더블링크드 리스트 c 언어  (0) 2019.05.17
링크드 리스트 2개 오름차순으로 병합하기  (0) 2019.05.10