반응형
SMALL
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 | #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; struct node *prev; }; struct node *head; void addToDLL(int n) { struct node *cur; cur = (struct node *)malloc(sizeof(struct node)); cur->next = cur->prev = 0; cur->data = n; // check if there is no node if (head == 0) { head = cur; return; } // find the last node { struct node *temp = head; while (temp->next != 0) { temp = temp->next; } temp->next = cur; cur->prev = temp; return; } } void showDLL() { struct node *cur = head; while (cur != 0) { printf("%d ", cur->data); cur = cur->next; } } // find the node with data n // delete it from DLL void delFromDLL(int n) { // locate the node with data n struct node *cur = head; while (cur != 0) { if (cur->data == n) { // found the node with data n break; } cur = cur->next; } if (cur == 0) // failed to find the node with n { return; } // now, cur is pointing to the node with n // case 1. cur is the first node of DLL if (cur == head) { head = head->next; free(cur); if (head != 0) { head->prev = 0; } } else if (cur->next == 0) { // case 2.cur is the last node of DLL cur->prev->next = 0; free(cur); } else // case3. cur is a middle node { // connect the previous one with the next one cur->prev->next = cur->next; // connect the next one with the previous one cur->next->prev = cur->prev; free(cur); } } // locate the node with n // add a new with with m // if direction == 1, add m mode to the next location // direction == -1, add m node to the previous location void insertToDLL(int n, int m, int direction) { // locate the node with n struct node *cur = head; while (cur != 0) { if (cur->data == n) { // found the node with n break; } cur = cur->next; } if (cur == 0) // failed to find the node with n { // thank you return; } else { // create a new node having m struct node *newbie; newbie = (struct node *)malloc(sizeof(struct node)); newbie->next = newbie->prev = 0; newbie->data = m; // case 1: if n node is the head node if (cur == head) { if (direction == -1) { // add the newbie to the previous location head = newbie; newbie->next = cur; cur->prev = newbie; return; } else { // add the newbie to the next location // case 1. cur has a next node if (cur->next != 0) { newbie->next = cur->next; newbie->prev = cur; cur->next->prev = newbie; cur->next = newbie; return; } else { // case 2. cur has no next node newbie->prev = cur; cur->next = newbie; return; } } } else if (cur->next != 0) { // case 2: cur is neither the first nor the last node. if (direction == 1) // next location { newbie->next = cur->next; newbie->prev = cur; cur->next = newbie; newbie->next->prev = newbie; return; } else // previous location { newbie->next = cur; newbie->prev = cur->prev; cur->prev = newbie; newbie->prev->next = newbie; return; } } else { // case 3. cur is the last node if (direction == 1) { // it is the same with the addtion cur->next = newbie; newbie->prev = cur; return; } else // add to the previous location { newbie->next = cur; newbie->prev = cur->prev; cur->prev = newbie; newbie->prev->next = newbie; return; } } } } int main(void) { addToDLL(1); addToDLL(2); addToDLL(3); addToDLL(4); insertToDLL(4, 10, -1); showDLL(); return 0; } |
반응형
LIST
'데이터구조 > 소스코드' 카테고리의 다른 글
OJ 1132번 해답 (0) | 2016.04.11 |
---|---|
Online Judge 1117 문제의 해답 (0) | 2016.04.04 |
Queue 구현코드 (큐) (0) | 2016.04.04 |
Stack 구현 소스 (0) | 2016.04.04 |
Singly Linked List (SLL) 소스코드 (Source code) (0) | 2016.03.24 |