본문 바로가기

Data Structure/Sample Code

LinkedList

LinkedList

 

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
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define EMPTY 0
 
struct node{
    int data;
    struct node *link;
};
typedef struct node linked;
 
linked * get_node()
{
    linked *tail;
    tail = (linked *)malloc(sizeof(linked));
    tail->link = EMPTY;
    return tail;
}
 
void insert_data(linked **head, int data)
{
    if (*head == NULL)
    {
        *head = get_node();
        (*head)->data = data;
    }
    else
    {
        insert_data(&(*head)->link, data);//재귀함수 사용(재귀함수 미사용시 반복문사용)
    }
}
 
void add(linked **head, int find_data, int add_data)
{
    if (*head == NULL)
    {
        printf("This data don't Exist");
    }
    else if ((*head)->data == find_data)
    {
        linked *add_node = EMPTY;
        add_node = get_node();
        add_node->data = add_data;
        add_node->link = (*head)->link;
        (*head)->link = add_node;
    }
    else
    {
        add(&(*head)->link, find_data, add_data);
    }
}
 
void delete_node(linked **head, int find_data)
{
    if (*head == NULL)
    {
        printf("This data don't Exist");
    }
 
    else if ((*head)->data == find_data)
    {
        linked * tmp;
        tmp = *head;
        //(*head)->link = (*head)->link;
        free(tmp);
    }
 
    else
    {
        delete_node(&(*head)->link, find_data);
    }
}
 
void print(linked *head)
{
    if (head == NULL)
    {
        printf("\n[Print Complete]\n");
    }
    else
    {
        printf("%d\t", head->data);
        print(head->link);
    }
}
 
void search()
{
}
 
void main()
{
    linked * head = EMPTY;
 
    insert_data(&head, 10);
    insert_data(&head, 20);
    insert_data(&head, 30);
 
    add(&head, 1015);
    add(&head, 2225);
 
    printf("--Linked List data print--\n");
    print(head);
 
}
cs


'Data Structure > Sample Code' 카테고리의 다른 글

Tree  (0) 2015.08.19
Queue 응용  (0) 2015.08.19
Queue  (0) 2015.08.19
Stack  (0) 2015.08.19