-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path기초 Single Linked List 연습.cpp
77 lines (68 loc) · 1.41 KB
/
기초 Single Linked List 연습.cpp
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
#define MAX_NODE 10000
struct Node {
int data;
Node* next;
};
Node node[MAX_NODE];
int nodeCnt;
Node head;
Node* getNode(int data) {
node[nodeCnt].data = data;
node[nodeCnt].next = nullptr;
return &node[nodeCnt++];
}
void init()
{
head.next = nullptr;
}
// 맨 앞에 노드 추가
void addNode2Head(int data)
{
Node *node = getNode(data);
node -> next = head.next;
head.next = node;
}
// 맨 뒤에 노드 추가
void addNode2Tail(int data)
{
Node *node = getNode(data);
Node *pre_ptr = &head;
while(pre_ptr -> next != nullptr) {
pre_ptr = pre_ptr -> next;
}
pre_ptr -> next = node;
}
// 지정된 순서에(num) 노드 추가
void addNode2Num(int data, int num)
{
int cnt = 1;
Node *pre_ptr = &head;
Node *node = getNode(data);
while(cnt < num) {
cnt++;
pre_ptr = pre_ptr -> next;
}
node -> next = pre_ptr -> next;
pre_ptr -> next = node;
}
void removeNode(int data)
{
Node *pre_ptr = &head;
while(pre_ptr -> next != nullptr && pre_ptr -> next -> data != data) {
pre_ptr = pre_ptr -> next;
}
if(pre_ptr -> next != nullptr) {
pre_ptr -> next = pre_ptr -> next -> next;
}
}
int getList(int output[MAX_NODE])
{
int cnt = -1;
Node *pre_ptr = &head;
while(pre_ptr != nullptr) {
output[cnt] = pre_ptr -> data;
pre_ptr = pre_ptr -> next;
cnt++;
}
return cnt;
}