#BITREE : TREE의 NODE CHILDE가 2개로 이루어져 2진트리라고한다.


이때 한쪽으로 치우쳐진것을 사향트리라고 하는데 밸런스에 맞게 조정한것을 BSTREE라고 한다.



BiTree

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
#include<stdio.h>
 
#include"bitree.h"
 
 
int tree_menu(void) {
 
    int i;
 
    do {
 
        printf("\n<<<Tree Menu>>>>\n");
 
        printf("1. Insert node\n");
 
        printf("2. Print_Preorder\n");
 
        printf("3. Print_Inorder\n");
        
        printf("4. Print_Postorder\n");
        
        printf("5. example - 20, 9, 53, 5, 15, 11, 79\n");
        
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
BiTreeNode * insert_tree(BiTree *tree, BiTreeNode *node, int data)
{
 
    if (bitree_is_eob(node))
    {
        node = (BiTreeNode *)malloc(sizeof(BiTreeNode));
         bitree_left(node)=NULL;
        bitree_right(node)=NULL;
           bitree_data(node) = (void *)data;
            return node;
    }
    
    else
    {
        
    if(bitree_data(node)>data)
        bitree_left(node) = insert_tree(tree,bitree_left(node),data);
 
    else
        bitree_right(node) = insert_tree(tree,bitree_right(node),data);    
    }
     return node;
}
 
void print_preorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
 
    printf("%d ", bitree_data(node));
    print_preorder(bitree_left(node));
    print_preorder(bitree_right(node));
}
 
void print_inorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
        
    print_inorder(bitree_left(node));
    printf("%d ", bitree_data(node));
    print_inorder(bitree_right(node));
}
void print_postorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
 
    print_postorder(bitree_left(node));
    print_postorder(bitree_right(node));
    printf("%d ", bitree_data(node));
}
 
 
void bitree_init(BiTree *tree, void (*destroy)(void *data))
{
    
tree->size = 0;
 
tree->destroy = destroy;
 
tree->root =NULL;
 
return;
    
}
 
int main()
{
    int i;
    BiTree tree;
    bitree_init(&tree,free);
    
    BiTreeNode *root = tree.root;
    
    
    int data;
    
    
        while((i=tree_menu())!=0) {
        switch(i) {
 
            case 1 :
                    printf(">Input data : ");
                    scanf("%d",&data);
                    if(root = insert_tree(&tree,root,data))
                    tree.size++;
            break;
            case 2:
                printf("\nPreeorder>>");
                    print_preorder(root);
                    printf("\n");
                break;
            case 3:
                printf("\ninorder>>");
                    print_inorder(root);
                printf("\n");
                break;
            case 4:
                printf("\npostorder>>");
                    print_postorder(root);
                printf("\n");
                break;
            case 5
                root = insert_tree(&tree,root,20);
                root = insert_tree(&tree,root,9);
                root = insert_tree(&tree,root,53);
                root = insert_tree(&tree,root,5);
                root = insert_tree(&tree,root,15);
                root = insert_tree(&tree,root,11);
                root = insert_tree(&tree,root,79);
                printf("\nPreeorder>>");
                    print_preorder(root);
                    printf("\n");
                    printf("\ninorder>>");
                    print_inorder(root);
                printf("\n");
                    printf("\npostorder>>");
                    print_postorder(root);
                printf("\n");
            break;
        }
    }
    
    system("pause");
    return 0;
}
cs



BsTree
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include<stdio.h>
 
#include"bitree.h"
 
int in_srt_array[100];
int bal_idx=0;
 
int tree_menu(void) {
 
    int i;
 
    do {
 
        printf("\n<<<Tree Menu>>>>\n");
 
        printf("1. Insert node\n");
 
        printf("2. Print_Preorder\n");
 
        printf("3. Print_Inorder\n");
        
        printf("4. Print_Postorder\n");
        
        printf("5. Print_Balance tree\n");
        
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
BiTreeNode * insert_tree(BiTree *tree, BiTreeNode *node, int data)
{
    in_srt_array[tree->size]=data;
    
    if (bitree_is_eob(node))
    {
        node = (BiTreeNode *)malloc(sizeof(BiTreeNode));
         bitree_left(node)=NULL;
        bitree_right(node)=NULL;
           bitree_data(node) = (void *)data;
            return node;
    }
    
    else
    {    
    if(bitree_data(node)>data)
        bitree_left(node) = insert_tree(tree,bitree_left(node),data);
 
    else
        bitree_right(node) = insert_tree(tree,bitree_right(node),data);    
    }
 
     return node;
}
 
 
 
 
 
void print_preorder(BiTreeNode *node)
{
 
    if(bitree_is_eob(node))
        return;
    printf("%d ", bitree_data(node));
    print_preorder(bitree_left(node));
    print_preorder(bitree_right(node));
    
}
 
void print_inorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
        
    print_inorder(bitree_left(node));
    printf("%d ", bitree_data(node));
    print_inorder(bitree_right(node));
}
void print_postorder(BiTreeNode *node)
{
    if(bitree_is_eob(node))
        return;
 
    print_postorder(bitree_left(node));
    print_postorder(bitree_right(node));
    printf("%d ", bitree_data(node));
}
 
 
 
void bitree_init(BiTree *tree, void (*destroy)(void *data))
{
    
tree->size = 0;
 
tree->destroy = destroy;
 
tree->root =NULL;
 
return;
    
}
 
BiTreeNode * balance(BiTree *tree,int n)
{
    int ln, rn;
    BiTreeNode *r;
    
    if(n>0)
    {
        ln=(n-1)/2;
        rn=n-ln-1;
        r = (BiTreeNode *)malloc(sizeof(BiTreeNode));
        r->left = balance(&tree,ln);
        r->data=in_srt_array[(bal_idx++)];
        r->right = balance(&tree,rn);
        return r;
    }
 
    return NULL;
}
int main()
{
    int i;
    BiTree tree;
    BiTree tree2;
    bitree_init(&tree,free);
    bitree_init(&tree2,free);
    
    BiTreeNode *root = tree.root;    
    
    int data;
    
    
        while((i=tree_menu())!=0) {
        switch(i) {
 
            case 1 :
                    printf(">Input data : ");
                    scanf("%d",&data);
                    if(root = insert_tree(&tree,root,data))
                    tree.size++;
            break;
            case 2:
                printf("\nPreeorder>>");
                    print_preorder(root);
                    printf("\n");
                break;
            case 3:
                printf("\ninorder>>");
                    print_inorder(root);
                printf("\n");
                break;
            case 4:
                printf("\npostorder>>");
                    print_postorder(root);
                printf("\n");
                break;
 
            
            case 5:
 
            tree2.root=balance(&tree2,tree.size);
            print_preorder(tree2.root);
            bal_idx=0;
            
            break;
 
        }
    }
    
    system("pause");
    return 0;
}
cs


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

[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28

Chained Hash Tables

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <stdlib.h>
#include"list.h"
#include<string.h>
#include<stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
List * make_bucket(int *size);
void find_node(List *list,int size);
void auto_data(List* list,int size);
int list_menu(void) {
 
    int i;
 
    do {
 
        printf("\n\nLinked List Menu\n");
 
        printf("1. Insert node\n");
 
        printf("2. Display all node\n");
        
        printf("3. find node\n");
 
        printf("4. auto data\n");
    
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
void start(List* hash,int size) {
 
    int i;
 
    void *data;
 
 
    while((i=list_menu())!=0) {
 
        switch(i) {
 
 
            case 1 :
                isert_node(hash,size);
            break;
 
            case 2 :
                for(i=0;i<size;i++)
                {
                    printf("\n>>hash[%d]>> ",i);
                    if((hash+i)->head==NULL)
                    {
                        printf("- empty",i);
                        continue;
                    }
                
                    print_list(hash+i);    
                }
            break;
            case 3 :
                find_node(hash,size);
            break;
            case 4:
                auto_data(hash,size);
                break;
        }
 
    }
    exit(0);
 
}
 
 
void print_list(List *list) {
    
    ListElmt *element;
    element=list_head(list);
 
    while(element){
        
        printf("%s  ",list_data(element));
 
        element=element->next;
    }
}
 
void main(void) {
 
 
    List *hash;
    int size=0;
    
    hash=make_bucket(&size);
    start(hash,size);
 
}
List * make_bucket(int *size)
{
    int i;
    printf("#버킷 크기 입력 :");
    fflush(stdin);
    scanf("%d",size);
    
    List * n_hash=(List *)malloc(sizeof(List)*(*size));
    
    for(i=0;i<*size;i++)
    {
        list_init(n_hash+i,free);
    }
    printf("\n#Bucket[%d]\n",*size);
    return n_hash;
    
}
 
void isert_node(List* list,int size)
{
    char temp[100];
    char * str;
 
        printf(">Input data : ");
        fflush(stdin);
        gets(temp); 
        str = (char *)malloc(sizeof(temp)+1);
        strcpy(str,temp);
    
    
    int num = rand()%size;
    
    if(list[num].tail==NULL)
    {
        if(list_ins_next(list+num, NULL, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    else
    {
            if(list_ins_next(list+num, list[num].tail, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    return;    
}
 
void auto_data(List* list,int size)
{
    char temp[100];
    char * str;
    int input,i;
    int num;
        printf(">Input data 개수 : ");
        fflush(stdin);
        scanf("%d",&input);
 
 
        for(i=0;i<input;i++)
        { 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,itoa(rand()%1000,temp,10));
        
        num = atoi(str)%size;
 
    
    if(list[num].tail==NULL)
    {
        if(list_ins_next(list+num, NULL, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    else
    {
            if(list_ins_next(list+num, list[num].tail, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
}
    return;    
}
 
void find_node(List *list,int size){
    int key,count=1;
    
    char temp[100];
    char * str;
 
        printf(">Input data : ");
        fflush(stdin);
        gets(temp); 
        str = (char *)malloc(sizeof(temp)+1);
        strcpy(str,temp);
        
    key=str[0]%size;
    ListElmt *element=list[key].head;
    
    while(1){
        if(!strcmp((char*)element->data,str)){
            printf("# data는 hash[%d]의 %d번째 노드에 있습니다.\n\n",key,count);
            return ;    
        }
        if(element->next==NULL){
            printf("# not find...\n");
            return ;
        }
        element=list_next(element);
        count++;
    }
}
 
cs


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

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28

■Stack■

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*****************************************************************************
*                                                                            *
*  ------------------------------- stack.c --------------------------------  *
*                                                                            *
*****************************************************************************/
 
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
 
#include "list.h"
#include "stack.h"
 
/*****************************************************************************
*                                                                            *
*  ------------------------------ stack_push ------------------------------  *
*                                                                            *
*****************************************************************************/
 
int stack_push(Stack *stackconst void *data) {
 
/*****************************************************************************
*                                                                            *
*  Push the data onto the stack.                                             *
*                                                                            *
*****************************************************************************/
 
 
return list_ins_next(stackNULL, data);
 
}
 
/*****************************************************************************
*                                                                            *
*  ------------------------------ stack_pop -------------------------------  *
*                                                                            *
*****************************************************************************/
 
int stack_pop(Stack *stackvoid **data) {
 
/*****************************************************************************
*                                                                            *
*  Pop the data off the stack.                                               *
*                                                                            *
*****************************************************************************/
 
return list_rem_next(stackNULL, data);
 
}
 
 
 
void main(void) {
 
 
    Stack stack;
    stack_init(&stackfree);
    
    
    start(&stack);
 
}
 
int list_menu(void) {
 
    int i;
 
    do {
 
        printf("\nStackt Menu\n");
 
        printf("1. stack push\n");
 
        printf("2. stack pop\n");
        
        printf("3. Display stack\n");
 
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
 
void start(Stack* stack) {
 
    int i;
 
    void *data;
    char temp[100];
    char * str,*str2;
 
 
    while((i=list_menu())!=0) {
 
        switch(i) {
 
 
            case 1 :
 
            printf(">Input data : ");
            fflush(stdin);
            gets(temp); 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,temp);
            stack_push(&stack, str);
            break;
            
            case 2 :
 
            if(list_rem_next(&stackNULL, (void **)&data)!=0)
 
            printf("The data is not exist\n");
 
            break;
            
            case 3 :
 
                print_stack(&stack);  
 
                break;
 
        }
 
    }
 
 
    exit(0);
 
}
 
void print_stack(Stack *stack)
{
    void *data;
    char temp[100];
    
    ListElmt *ST;
 
    ST=stack->head;
    
    
    if(ST==NULL) {
    printf("There is not data\n");
    return;
    }
        
    while(1) {
        
            strcpy(temp,(char *)(ST->data));
            
            if(is_check(temp))
            printf("%s    ", (char *)(ST->data) );
            else
            printf("%d    ",atoi((char *)(ST->data) ));
            
        if(ST->next==NULL)
            break;
 
        else
            ST=(ST->next);
    }
 
}
int is_check(char* temp)
{
    int i;
    for(i=0;i<strlen(temp);i++)
            if(temp[i]<'0' ||temp[i]>'9')
            {
                return 1;
            }
        return 0;
}
cs


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

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28
■SET■

# 합집합, 차집합, 교집합을 링크리스트를 이용하여 구현

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <stdio.h>
#include <stdlib.h>
#include"list.h"
#include<string.h>
 
 
void print_list(List *list);
void doUnion(List* listA,List* listB);
void deDuplicate(List *list);
void doIntersection(List* listA,List* listB);
void doDiffrence(List* listA,List* listB);
int find_list(List *list,void *data);
 
int list_menu(void) {
 
    int i;
 
    do {
 
        printf("\n=======Menu=======\n");
 
        printf("1. Insert nodeA\n");
 
        printf("2. Insert nodeB\n");
        
        printf("3. print nodeA\n");
        
        printf("4. print nodeB\n");
        
        printf("5. doUnion\n");
            
        printf("6. doIntersection\n");
                
        printf("7. doDiffrence\n");
            
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>8);
 
    return i;
 
}
 
void start(List* listA,List* listB) {
 
    int i;
 
    void *data;
    char temp[100];
    char * str;
 
 
    while((i=list_menu())!=0) {
 
        switch(i) {
 
 
            case 1 :
 
            printf(">Input data : ");
            fflush(stdin);
            gets(temp); 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,temp);
            list_ins_next(listA, NULL, str);
 
            break;
            
            case 2 :
 
            printf(">Input data : ");
            fflush(stdin);
            gets(temp); 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,temp);
            list_ins_next(listB, NULL, str);
 
            break;
 
            case 3 :
 
                print_list(listA);  
 
            break;
            
            case 4 :
 
                print_list(listB);  
                
            break;
            
            case 5:
                doUnion(listA,listB);
 
            break;
            
            case 6:
                
                doIntersection(listA,listB);
 
            break;
            
            case 7:
                
                doDiffrence(listA,listB);
 
            break;
 
        }
 
    }
 
    exit(0);
 
}
 
 
int is_check(char* temp)
{
    int i;
    for(i=0;i<strlen(temp);i++)
            if(temp[i]<'0' ||temp[i]>'9')
            {
                return 1;
            }
        return 0;
}
 
 
void print_list(List *list)
{
    void *data;
    char temp[100];
    
    ListElmt *element;
 
    element=list->head;
    
    
    if(element==NULL) {
    printf("There is not data\n");
    return;
    }
    
 
    while(1) {
        
            strcpy(temp,(char *)(element->data));
            
            if(is_check(temp))
            printf("%s    ", (char *)(element->data) );
            else
            printf("%d    ",atoi((char *)(element->data) ));
            
        if(list_is_tail(element))
            break;
 
        else
            element=list_next(element);
    }
 
}
 
 
void doUnion(List* listA,List* listB)
{
    List listC;
    list_init(&listC, free);
    
    char tempA[100],tempB[100];
    
    ListElmt *elementA,*elementB;
 
    elementA=listA->head;
    elementB=listB->head;
 
        
    if(elementA==NULL) {
    printf("A is not data\n");
    return;
    }
    if(elementB==NULL) {
    printf("B is not data\n");
    return;
    }
    
    while(1) {
        
        strcpy(tempA,(char *)(elementA->data));
            
        if(!find_list(&listC,tempA)||(find_list(&listC,tempA)==-1))
            list_ins_next(&listC, NULL, elementA->data);
            
        while(1) {
        
        strcpy(tempB,(char *)(elementB->data));
                
        if(strcmp(tempA,tempB))
        if(!find_list(&listC,tempB))
        list_ins_next(&listC, NULL, elementB->data);
 
 
        if(list_is_tail(elementB))
            break;
 
        else
            elementB=list_next(elementB);
    }
            
            elementB=listB->head;
            
        if(list_is_tail(elementA))
            break;
 
        else
            elementA=list_next(elementA);
    }
        printf("\nUnion >> ");
        print_list(&listC);
        printf("\n");
}
 
void doIntersection(List* listA,List* listB)
{
    List listC;
    list_init(&listC, free);
    
    char tempA[100],tempB[100];
 
    ListElmt *elementA,*elementB;
 
    elementA=listA->head;
    elementB=listB->head;
 
        
    if(elementA==NULL) {
    printf("A is not data\n");
    return;
    }
    if(elementB==NULL) {
    printf("B is not data\n");
    return;
    }
 
    
    while(1) {
        
            strcpy(tempA,(char *)(elementA->data));
            
            
 
        while(1) {
        
            strcpy(tempB,(char *)(elementB->data));
                
            if(!strcmp(tempA,tempB))
            if(!find_list(&listC,tempB)||(find_list(&listC,tempB)==-1))
            list_ins_next(&listC, NULL, elementB->data);
 
        if(list_is_tail(elementB))
            break;
 
        else
            elementB=list_next(elementB);
    }
            
            elementB=listB->head;
            
        if(list_is_tail(elementA))
            break;
 
        else
            elementA=list_next(elementA);
    }
        
        printf("\nIntersection >> ");
        print_list(&listC);
        printf("\n");
}
 
void doDiffrence(List* listA,List* listB)
{
    List listC;
    list_init(&listC, free);
    
    int flag=0;
    char tempA[100],tempB[100];
    
    ListElmt *elementA,*elementB;
 
    elementA=listA->head;
    elementB=listB->head;
 
        
    if(elementA==NULL) {
    printf("A is not data\n");
    return;
    }
    if(elementB==NULL) {
    printf("B is not data\n");
    return;
    }
 
    
    while(1) {
        
        strcpy(tempA,(char *)(elementA->data));
                    
        while(1) {
        
            strcpy(tempB,(char *)(elementB->data));
                
        if(!strcmp(tempA,tempB))
            {
            flag=1;
            break;
            }
 
        if(list_is_tail(elementB))
            break;
 
        else
            elementB=list_next(elementB);
    }
    
    if(flag==0)
        if(!find_list(&listC,tempA)||(find_list(&listC,tempA)==-1))
            list_ins_next(&listC, NULL, elementA->data);            
    flag=0;    
    
        elementB=listB->head;
            
        if(list_is_tail(elementA))
            break;
 
        else
            elementA=list_next(elementA);
    }
        
        printf("\nDiffrence >> ");
        print_list(&listC);
        printf("\n");
}
 
 
int find_list(List *list,void *data){//있을시 return 1, 없을시 return 0 
 
    char temp[100];
 
    ListElmt *element;
 
    element=list->head;
    
    
    if(element==NULL)
    return -1;
    
    while(1) {
 
    strcpy(temp,(char *)(data));
    
    if(is_check(temp))
    {
 
    if(strcmp((char *)(data),(char *)(element->data))==0 )
    {
    return 1;
    }
    
    else
    {
        if(list_is_tail(element))
        {
            return 0;
        }
    element=list_next(element);
    }
}
    else
    
    {
 
    if(atoi((char *)(data))==atoi((char *)(element->data)) )
    {
 
    return 1;
    }
    
    else
    {
        if(list_is_tail(element))
        {
 
            return 0;
        }
    element=list_next(element);
    }
}
    
    }
 
 }
int main(void) {
 
 
    List listA,listB;
    list_init(&listA, free);
    list_init(&listB, free);
 
    start(&listA,&listB);
    
    system("pause");
 
}
 
cs


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

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28

■Double Circular Linked List■

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
void initDCLL(Node **H) {
    *= (Node*)malloc(sizeof(Node));
    (*H)->next = *H;
    (*H)->prev = *H;
}
 
Node * createNode( T_DATA data ){
    Node *= (Node*)malloc(sizeof(Node));
    t->data = data;
    t->next = NULL;
    t->prev = NULL;
    return t;
}
 
void _insertNode(Node *newnode, Node *P, Node *N) {
    newnode->prev = P;
    newnode->next = N;
    P->next = newnode;
    N->prev = newnode;
}
 
typedef struct {
        char *Name;
        char *PhoneNo;
} Phonebook;
 
typedef struct _Node {   
    T_DATA data;            
    struct _Node *prev;   
    struct _Node *next;  
} Node;
 
#define insertAfterNode(newnode, T) _insertNode(newnode, T, T->next)
 
void AddPhoneBook(void)
{
Phonebook newpb;
Node *temp = NULL;

if(Head==NULL)    
     {
        temp=createNode(newpb);
         Head->next=temp;
         Head->prev=temp;
     }
    else
    {
        temp=createNode(newpb);
        insertAfterNode(temp, Head->next);
    }
}
cs

이중 환형 리스트는 이중 연결 리스트와 비슷하지만 차이점은 tail이 없다는 것입니다.


사용 예 : <재시도 페이지 교체>

-사용 가능한 페이지를 재할당하는 등의 페이지 할당/교체 방식

-Swap disk

-Page replacement algorithm

-메모리 공간의 제약으로 인해 동시에 모든 자료를 메모리에 올려 놓을 수가 없다.

(-> 빈번한 교체가 이루어져야 한다.)

-LRU페이지 교체 방식



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

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day1]Double Linked List  (0) 2018.10.28


■Double Linked List■

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
#ifndef DLIST_H
#define DLIST_H
 
#include <stdlib.h>
 
typedef struct DListElmt_ {
 
void               *data;
struct DListElmt_  *prev;
struct DListElmt_  *next;
 
} DListElmt;
 
 
typedef struct DList_ {
 
int                size;
 
int                (*match)(const void *key1, const void *key2);
void               (*destroy)(void *data);
 
DListElmt          *head;
DListElmt          *tail;
 
} DList;
cs
#Double Linked List는 Single Linked List와 비교하면 prev라는 포인터가 하나더 붙어 있으며 역방향으로의 순회가 가능하다.



대표적으로 insert를 구현하면서 이해를 해보면 delete,find등 다른 구현은 쉽게할 수있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void dlist_init(DList *list, void (*destroy)(void *data)) {
 
/*****************************************************************************
*                                                                            *
*  Initialize the list.                                                      *
*                                                                            *
*****************************************************************************/
 
list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
 
return;
 
}

cs

List의 초기화 부분이며 head와 tail은 리스트의 첫과 끝을 가리키며 size는 할당된 노드의 수를 말한다.


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
int dlist_ins_next(DList *list, DListElmt *element, const void *data) {
{
DListElmt           *new_element;
    
    if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)
   return -1;
    new_element->data = (void *)data;
    
    if (element == NULL) {
   if (dlist_size(list) == 0)
{
       list->head = new_element;
       list->head->prev = NULL;
       list->head->next = NULL;
       list->tail = new_element;
}
 
else 
{
    new_element->next=list->tail->next;
    list->tail->next=new_element;
    list->tail = new_element;
    list->tail->prev=list->head;
}
}
else {
      if (dlist_size(list) == 0)
{
       list->head = new_element;
       list->head->prev = NULL;
       list->head->next = NULL;
       list->tail = new_element;
}
 
else 
{
    new_element->next=element->next;
    element->next=new_element;
    element = new_element;
    new_element->prev=element;
}
    
}
list->size++;
return 0;
}
}
cs

1. 전달된 노드의 값이 NULL인지 존재하는지를 구분한다. (노드가 존재하면 그 노드를 기준으로 insert 한다.)
2. size가 0인지를 즉, List안에 처음으로 들어갈 노드인지 아닌지를 체크한다.
3.
  if (dlist_size(list) == 0)
{
       list->head = new_element;
       list->head->prev = NULL;
       list->head->next = NULL;
       list->tail = new_element;
}
size가 0, List안에 처음으로 들어갈 때의 노드를 A라고 하자, 하나 밖에 없으므로 head와 tail은 A_node를 가리키고,
A_node의 앞,뒤에는 아무것도 없으므로 NULL값을 넣어준다. 
list->head(list의 head=new_element head가 new_element를 가리킨다.)
list->head->prev = NULL(head가 가리키는 노드의 prev에 NULL을 넣는다.)
4.
else 
{
    new_element->next=list->tail->next;
    list->tail->next=new_element;
    list->tail = new_element;
    list->tail->prev=list->head;
}

기존에 A_node가 있을 때 B_node가 들어간다고 가정한다면

먼저  B_node의 next에 list->tail->next(즉, A_node의 next값을 넣어준다(현재 A_node의 next값은 NULL이다)

list->tail->next(A_node의 next)는 B_node를 가리킨다.

list->tail(List의 끝은)B_node를 가리키고

list->tail->prev(B_node의 prev)는 list->head(A_node)를 가리킨다.


*전달된 노드가 있을 시 list->tail를 element로 바꿔주면 되고 현재는 insert_next로 구현하였고 insert_prev로 구현할 수도 있다.

예) insert_next : insert- 1 2 3 4    insert된 node -4 3 2 1 


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

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day4]Chained Hash Tables  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28

+ Recent posts