노드 구조체
1 2 3 4 5 6 | typedef struct _node { int data; struct _node* left; struct _node* right; } Node; | cs |
리스트 구조체
1 2 3 4 5 6 | typedef struct _list { Node* head; Node* tail; int size; } List; | cs |
List* create_list() : 리스트 생성 및 초기화
1 2 3 4 5 6 7 8 9 | List* create_list() { List* new_list = (List*)malloc(sizeof(List)); new_list->head = NULL; new_list->tail = NULL; new_list->size = 0; return new_list; } | cs |
bool list_empty(List* L) : 리스트가 비어있으면 1 아니면 0 리턴
1 2 3 4 5 | bool list_empty(List* L) { if(L->size) return 0; else return 1; } | cs |
Node* create_node(int data) : 노드 생성 및 초기화
1 2 3 4 5 6 7 8 9 | Node* create_node(int data) { Node* new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->left = NULL; new_node->right = NULL; return new_node; } | cs |
void push_back(List *L, int data) : 리스트 맨 뒤에 푸시
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | void push_back(List *L, int data) { Node* new_node = create_node(data); if(list_empty(L)) { L->head = new_node; L->tail = new_node; } else { L->tail->right = new_node; new_node->left = L->tail; L->tail = new_node; } L->size++; } | cs |
void push_front(List *L, int data) : 리스트 맨 앞에 푸시
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | void push_front(List *L, int data) { Node* new_node = create_node(data); if(list_empty(L)) { L->head = new_node; L->tail = new_node; } else { L->head->left = new_node; new_node->right = L->head; L->head = new_node; } L->size++; } | cs |
void node_insert(List* L, int idx, int data) : 해당 인덱스에 데이터 삽입
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void node_insert(List* L, int idx, int data) { if(idx == 0) push_front(L, data); else if(idx >= L->size - 1) push_back(L, data); else { Node* new_node = create_node(data); Node* now = L->head; for(int i = 0; i < idx; i++) now = now->right; new_node->left = now->left; new_node->right = now; now->left = new_node; new_node->left->right = new_node; L->size++; } } | cs |
void pop_back(List* L) : 리스트 맨 뒤에 삭제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | void pop_back(List* L) { if(!list_empty(L)) { if(L->size == 1) { free(L->head); L->head = NULL; L->tail = NULL; } else { Node* prev = L->tail->left; free(L->tail); prev->right = NULL; L->tail = prev; } L->size--; } } | cs |
void pop_front(List* L) : 리스트 맨 앞에 삭제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | void pop_front(List* L) { if(!list_empty(L)) { if(L->size == 1) { free(L->head); L->head = NULL; L->tail = NULL; } else { Node* next = L->head->right; free(L->head); next->left = NULL; L->head = next; } L->size--; } } | cs |
void node_delete(List* L, int idx) : 입력된 인덱스의 노드 삭제(인덱스가 벗어나면 맨 앞 혹은 맨 뒤 삭제)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void node_delete(List* L, int idx) { if(!list_empty(L)) { if(idx >= L->size - 1) pop_back(L); else if(idx <= 0) pop_front(L); else { Node* now = L->head; for(int i = 0; i < idx; i++) now = now->right; now->left->right = now->right; now->right->left = now->left; free(now); L->size--; } } } | cs |
실행 환경 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 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | #include <cstdio> #include <cstdlib> typedef struct _node { int data; struct _node* left; struct _node* right; } Node; typedef struct _list { Node* head; Node* tail; int size; } List; List* create_list() { List* new_list = (List*)malloc(sizeof(List)); new_list->head = NULL; new_list->tail = NULL; new_list->size = 0; return new_list; } Node* create_node(int data) { Node* new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->left = NULL; new_node->right = NULL; return new_node; } bool list_empty(List* L) { if(L->size) return 0; else return 1; } void push_back(List *L, int data) { Node* new_node = create_node(data); if(list_empty(L)) { L->head = new_node; L->tail = new_node; } else { L->tail->right = new_node; new_node->left = L->tail; L->tail = new_node; } L->size++; } void push_front(List *L, int data) { Node* new_node = create_node(data); if(list_empty(L)) { L->head = new_node; L->tail = new_node; } else { L->head->left = new_node; new_node->right = L->head; L->head = new_node; } L->size++; } void list_print(List* L) { Node* tmp = L->head; while(tmp) { printf("%d ", tmp->data); tmp = tmp->right; } puts(""); } void pop_back(List* L) { if(!list_empty(L)) { if(L->size == 1) { free(L->head); L->head = NULL; L->tail = NULL; } else { Node* prev = L->tail->left; free(L->tail); prev->right = NULL; L->tail = prev; } L->size--; } } void pop_front(List* L) { if(!list_empty(L)) { if(L->size == 1) { free(L->head); L->head = NULL; L->tail = NULL; } else { Node* next = L->head->right; free(L->head); next->left = NULL; L->head = next; } L->size--; } } void node_delete(List* L, int idx) { if(!list_empty(L)) { if(idx >= L->size - 1) pop_back(L); else if(idx <= 0) pop_front(L); else { Node* now = L->head; for(int i = 0; i < idx; i++) now = now->right; now->left->right = now->right; now->right->left = now->left; free(now); L->size--; } } } void node_insert(List* L, int idx, int data) { if(idx == 0) push_front(L, data); else if(idx >= L->size - 1) push_back(L, data); else { Node* new_node = create_node(data); Node* now = L->head; for(int i = 0; i < idx; i++) now = now->right; new_node->left = now->left; new_node->right = now; now->left = new_node; new_node->left->right = new_node; L->size++; } } int main() { List* new_list = create_list(); for(int i = 1; i < 10; i++) { push_back(new_list, i); list_print(new_list); } for(int i = -10; i < 0; i++) { push_front(new_list, i); list_print(new_list); } for(int i = 0; i < 20; i++) { pop_front(new_list); list_print(new_list); } for(int i = 1; i < 10; i++) { push_back(new_list, i); list_print(new_list); } for(int i = 0; i < 10; i++) { node_delete(new_list, i); list_print(new_list); } for(int i = 1; i < 10; i++) { push_back(new_list, i); list_print(new_list); } puts(""); for(int i = 2; i < 5; i++){ node_insert(new_list, i, -1 - i); list_print(new_list); } return 0; } | cs |
'CS기본지식 > 자료구조' 카테고리의 다른 글
[C언어] 우선순위 큐(Priority Queue) (3) | 2017.09.07 |
---|---|
[2263] 트리의 순회 (1) | 2017.08.27 |
스택 계산기 (0) | 2017.06.13 |
[1918] 후위표기식 (2) | 2017.06.13 |
단순 연결 리스트 (0) | 2017.04.22 |