-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path연결리스트.c
More file actions
94 lines (70 loc) · 1.67 KB
/
연결리스트.c
File metadata and controls
94 lines (70 loc) · 1.67 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
#include <stdio.h>
#include <stdlib.h>
#include "연결리스트.h"
void ListInit(List* plist) {
//리스트 초기화, Dummy Node 생성
plist->head = (Node*)malloc(sizeof(Node));
plist->head->next = NULL;
plist->comp = NULL;
plist->numOfData = 0;
}
void FInsert(List* plist, LData data) {
//Front Insert
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = plist->head->next;
//plist->head 더미 노드
plist->head->next = newNode;
(plist->numOfData)++;
}
void SInsert(List* plist, LData data) {
//predecessor, successor
Node* newNode = (Node*)malloc(sizeof(Node));
Node* pred = plist->head;
newNode->data = data;
while (pred->next != NULL &&
plist->comp(data, pred->next->data) != 0) {
//함수포인터 리턴값은 0 또는 1,
pred = pred->next;
}
newNode->next = pred->next;
pred->next = newNode;
(plist->numOfData)++;
}
void LInsert(List* plist, LData data) {
if (plist->comp == NULL)
FInsert(plist, data);
else
SInsert(plist, data);
}
int LFirst(List* plist, LData* pdata) {
if (plist->head->next == NULL)
return FALSE;
plist->before = plist->head;
plist->cur = plist->head->next;
*pdata = plist->cur->data;
return TRUE;
}
int LNext(List* plist, LData* pdata) {
if (plist->cur->next == NULL)
return FALSE;
plist->before = plist->cur;
plist->cur = plist->cur->next;
*pdata = plist->cur->data;
return TRUE;
}
LData LRemove(List* plist){
Node* rpos = plist->cur;
LData rdata = rpos->data;
plist->before->next = plist->cur->next;
plist->cur = plist->before;
free(rpos);
(plist->numOfData)--;
return rdata;
}
int LCount(List* plist){
return plist->numOfData;
}
void SetSortRule(List* plist, int (*comp)(LData d1, LData d2)){
plist->comp = comp;
}