-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.c
More file actions
142 lines (124 loc) · 2.43 KB
/
LinkedList.c
File metadata and controls
142 lines (124 loc) · 2.43 KB
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
#include <stdio.h>
#include <stdlib.h>
#define EMPTY 0
typedef struct node {
int data;
struct node* link;
}LinkedList;
LinkedList* GetNode()
{
LinkedList* tmp;
tmp = (LinkedList*)malloc(sizeof(LinkedList));
tmp->link = EMPTY;
return tmp;
}
void Insert(LinkedList** head, int data)
{
if (*head == EMPTY)
{
*head = GetNode();
(*head)->data = data;
}
else
{
Insert(&(*head)->link, data); //Recursive 활용
}
}
void Output(LinkedList** head)
{
//해제 x
if (*head)
{
printf("%d\n", (*head)->data);
Output(&(*head)->link);
}
}
void Search(LinkedList** head, int data)
{
if (*head == EMPTY)
{
printf("%d not exist!!\n", data);
return;
}
else if ((*head)->data == data)
{
printf("%d Search!!\n", data);
}
else
Search(&(*head)->link, data);
}
void Delete(LinkedList** head, int data)
{
LinkedList* tmp;
if (*head == EMPTY)
{
printf("%d not exist!!\n", data);
return;
}
else if ((*head)->data == data)
{
tmp = *head;
*head = (*head)->link;
free(tmp);
}
else
Delete(&(*head)->link, data);
}
void Add(LinkedList** head, int data1, int data2)
{
LinkedList* newNode = EMPTY;
if (*head == EMPTY)
{
printf("%d not exist!!\n", data1);
}
else if ((*head)->data == data1)
{
Insert(&newNode, data2);
newNode->link = (*head)->link;
(*head)->link = newNode;
}
else
Add(&(*head)->link, data1, data2);
}
void Sort(LinkedList** head)
{
LinkedList* tmp;
int tmp_int;
//반복문 대체
if (*head)
{
tmp = (*head)->link;
while (tmp)
{
if ((*head)->data < tmp->data)
{
tmp_int = (*head)->data;
(*head)->data = tmp->data;
tmp->data = tmp_int;
}
tmp = tmp->link;
}
Sort(&(*head)->link);
}
}
int main()
{
//head 선언
LinkedList* head = EMPTY;
Insert(&head, 10);
Insert(&head, 20);
Insert(&head, 30);
//(&head, 40);
//Search(&head, 30);
//Search(&head, 50);
//Search(&head, 40);
//Delete(&head, 20);
//Delete(&head, 30);
Delete(&head, 45);
Add(&head, 20, 25);
Add(&head, 30, 35);
//Add(&head, 50, 55);
Sort(&head);
Output(&head);
return 0;
}