Programming/C

단일 환형 연결리스트 구현

Tribal 2016. 11. 4. 02:18
완전한 형태로 구현하지 않고 일부만 구현해서 사용하였다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* Circle.h */
#pragma once
#ifndef __CIRCLE_H
#define __CIRCLE_H
 
typedef struct _CIRCLE_NODE {
    DWORD index;
    struct _CIRCLE_NODE *next_node;
} CIRCLE_NODE, *PCIRCLE_NODE;
 
typedef struct _CIRCLE {
    PBreak_Table header_table;
    PBreak_Table tailer_table;
} CIRCLE, *PCIRCLE;
 
void Insert_Circle(PCIRCLE pbtc, DWORD value);
DWORD Table_Count(PCIRCLE pbtc);
BOOL Search_Circle(const PBreak_Table_Circle pbtc, const DWORD value);
void Delete_Circle(PCIRCLE pbtc, DWORD offset);
 
#endif
 
cs


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
/* Circle.c */
#include "Circle.h"
 
PCIRCLE gp_circle;
 
void Insert_Circle(PCIRCLE pcir, DWORD value) {
    PCIRCLE_NODE new_table = (PCIRCLE_NODE) malloc(sizeof(CIRCLE_NODE));
 
    if(pcir->header_table == NULL) {
        pcir->header_table = new_table;
        pcir->tailer_table = new_table;
        new_table->index = value;
        new_table->next_table = pcir->tailer_table;
    } else {
        new_table->next_table = pcir->header_table;
        new_table->index = value;
        pcir->tailer_table->next_table = pcir->header_table;
    }
}
 
DWORD Table_Count(PCIRCLE pcir) {
    DWORD count = 0;
    PCIRCLE_NODE temp = pcir->header_table;
 
    if(temp == NULLreturn count;
    do {
        count++;
        temp = temp->next_table;
    }while(temp != pcir->tailer_table);
 
    return count;
}
 
BOOL Search_Circle(const PCIRCLE pbtc, const DWORD value) {
    PCIRCLE_NODE temp = pbtc->header_table;
 
    if(temp == NULLreturn FALSE;
 
    do {
        if(temp->list_offset == value) {
            return TRUE;
        }
        temp = temp->next_table;
    } while(temp != pbtc->tailer_table);
 
    return FALSE;
}
 
void Delete_Circle(PCIRCLE pcir, DWORD offset) {
    PCIRCLE_NODE temp = NULL;                                    // temp table ptr
    PCIRCLE_NODE Del_table = pcir->header_table;        // Delete table ptr
    DWORD i = 0;
 
    for(i=0; i<Table_Count(pcir); i++) {
        if(Del_table->index == offset) break;            // Find offset
        else {
            temp = Del_table;
            Del_table = Del_table->next_table;
        }
    }
 
    if(temp != NULL)
        temp->next_table = Del_table->next_table;    //sorting circle linked list
 
    Del_table->index = NULL;
    Del_table->next_table = NULL;        // Zero Set
    free(Del_table);
}
cs

주의 : 옮기는 도중에 코드를 변형해서 오타가 있을 수 있음...