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 | #include <stdio.h> #include <malloc.h> typedef struct Node { int Data; struct Node* NextNode; }Node; /* 노드 생성 */ Node* SLL_CreateNode(int NewData) { Node* NewNode = (Node*)malloc(sizeof(Node)); NewNode->Data = NewData; /* 데이터를 저장한다. */ NewNode->NextNode = NULL; /* 다음 노드에 대한 포인터는 NULL로 초기화 한다. */ return NewNode;/* 노드의 주소를 반환한다. */ } /* 노드 소멸 */ void SLL_DestroyNode(Node* Node) { free(Node); } /* 노드 추가 */ void SLL_AppendNode(Node** Head, Node* NewNode) { /* 헤드 노드가 NULL이라면 새로운 노드가 Head */ if ((*Head) == NULL) { *Head = NewNode; } else { /* 테일을 찾아 NewNode를 연결한다. */ Node* Tail = (*Head); while (Tail->NextNode != NULL) { Tail = Tail->NextNode; } Tail->NextNode = NewNode; } } /* 노드 삽입 */ void SLL_InsertAfter(Node* Current, Node* NewNode) { NewNode->NextNode = Current->NextNode; Current->NextNode = NewNode; } void SLL_InsertNewHead(Node** Head, Node* NewHead) { if (Head == NULL) { (*Head) = NewHead; } else { NewHead->NextNode = (*Head); (*Head) = NewHead; } } void SLL_InsertBefore(Node** Head, Node* Current, Node* NewHead) { if ((*Head) == Current) { SLL_InsertNewHead(Head, NewHead); } else { Node *BeforeCurrent = *Head; while (BeforeCurrent != NULL && BeforeCurrent->NextNode != Current) { BeforeCurrent = BeforeCurrent->NextNode; } if (BeforeCurrent != NULL) { NewHead->NextNode = Current; BeforeCurrent->NextNode = NewHead; } } } /* 노드 제거 */ void SLL_RemoveNode(Node** Head, Node* Remove) { if (*Head == Remove) { *Head = Remove->NextNode; } else { Node* Current = *Head; while (Current != NULL && Current->NextNode != Remove) { Current = Current->NextNode; } if (Current != NULL) Current->NextNode = Remove->NextNode; } //SLL_DestroyNode(Remove); } /* 노드 탐색 */ Node* SLL_GetNodeAt(Node* Head, int Location) { Node* Current = Head; while (Current != NULL && (--Location) >= 0) { Current = Current->NextNode; } return Current; } /* 노드 수 세기 */ int SLL_GetNodeCount(Node* Head) { int Count = 0; Node* Current = Head; while (Current != NULL) { Current = Current->NextNode; Count++; } return Count; } void SLL_DestroyAllNodes(Node** List) { } int main() { return 0; } | cs |
Singly Linked List
2015. 10. 12. 01:59