자료구조_c언어_linked
2021. 5. 18. 10:08ㆍ개발하는중/자료구조
728x90
반응형
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> typedef struct ListNode { char data[10]; struct ListNode *link; }listNode; typedef struct { listNode *head; }linkedList_h; linkedList_h* createLinkedList_h(void); void freeLinkedList(linkedList_h *); void addLastNode(linkedList_h *, char*); void reverse(linkedList_h *); void deleteLastNode(linkedList_h *); void printList(linkedList_h *); linkedList_h* createLinkedList_h(void) { linkedList_h *L; L = (linkedList_h *)malloc(sizeof(linkedList_h)); L->head = NULL; return L; } void addLastNode(linkedList_h *L, char *x) { listNode *newNode; listNode *p; newNode = (listNode*)malloc(sizeof(listNode)); strcpy(newNode->data, x); newNode->link = NULL; if (L->head == NULL) { L->head = newNode; return; } p = L->head; while (p->link != NULL) { p = p->link; } p->link = newNode; } void reverse(linkedList_h *L) { listNode *p; listNode *q; listNode *r; p = L->head; q = NULL; r = NULL; while (p != NULL) { r = q; q = p; p = p->link; q->link = r; } L->head = q; } // temp = a; // a = b; // b = temp; void deleteLastNode(linkedList_h *L) { listNode *previous; listNode *current; if (L->head == NULL) { return; } if (L->head->link == NULL) { // head가 가르키는 링크가 NULL이냐 = head가 하나의 노드 가르키고 있다는 거 free(L->head); // 지움 L->head = NULL; // L이 가르키는 head 주소값 초기화 return; } else { previous = L->head; current = L->head->link; while (current->link != NULL) { previous = current; current = current->link; } free(current); previous->link = NULL; } } void freeLinkedList_h(linkedList_h *L) { listNode *p; while (L->head != NULL) { p = L->head; L->head = L->head->link; free(p); p = NULL; } } void printList(linkedList_h *L) { listNode *p; printf("L=("); p = L->head; while (p != NULL) { printf("%s", p->data); p = p->link; if (p != NULL) { printf(","); } } printf(")\n"); } int main() { linkedList_h *L; L = createLinkedList_h(); printf("(1)공백 리스트 생성하기!\n"); printList(L); getchar(); printf("(2)리스트에 3개의 노드 추가하기!\n"); addLastNode(L, "월"); addLastNode(L, "수"); addLastNode(L, "금"); printList(L); getchar(); printf("(3)리스트 마지막에 노드 한개 추가하기!\n"); addLastNode(L, "일"); printList(L); getchar(); printf("(4)마지막 노드 삭제하기!\n"); deleteLastNode(L); printList(L); getchar(); printf("(5)리스트 원소를 역순으로 변환하기!\n"); reverse(L); printList(L); getchar(); printf("(6)리스트 공간을 해체하여, 공백 리스트 상태로 만들기!\n"); freeLinkedList_h(L); printList(L); getchar(); return 0; } | cs |
728x90
'개발하는중 > 자료구조' 카테고리의 다른 글
자료구조_c언어_linkedList_02 (0) | 2021.05.25 |
---|---|
자료구조_c언어_linkedList (0) | 2021.05.20 |
자료구조_c언어_큐스택_queueStack_01 (0) | 2021.05.17 |
자료구조_c언어_배열스택_arrayStack_05 (0) | 2021.02.24 |
자료구조_c언어_서클 큐_circleQueue_01 (0) | 2021.02.10 |