티스토리 뷰
singleLinkedList.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #ifndef SINGLELINKEDLIST_H #define SINGLELINKEDLIST_H typedef int element; typedef struct ListNode { element data; struct ListNode *link; }Node; Node* create_node(element data, Node* link); void insert_node(Node** phead, Node* p, Node* newNode); void remove_node(Node** phead, Node* p, Node* removed); void display(Node* head); void display_recur(Node* head); Node* reverse(Node* head); void remove_all_nodes(Node** phead); #endif // !SINGLELINKEDLIST_H |
singleLinkedList.h
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 | #include <stdio.h> #include <stdlib.h> #include "singleLinkedList.h" Node* create_node(element data, Node* link) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->link = link; return newNode; } void insert_node(Node** phead, Node* p, Node* newNode) { if (*phead == NULL) { newNode->link = NULL; *phead = newNode; } else if (p == NULL) { newNode->link = *phead; *phead = newNode; } else { newNode->link = p->link; p = newNode; } } void remove_node(Node** phead, Node* p, Node* removed) { if (p == NULL) { *phead = (*phead)->link; } else { p->link = removed->link; } free(removed); } void remove_all_nodes(Node** phead) { Node* p = (*phead); while (p != NULL) { *phead = (*phead)->link; free(p); p = *phead; } } void display(Node* head) { do { printf("%d\n", head->data); head = head->link; } while (head != NULL); puts(""); } void display_recur(Node* head) { if (head == NULL) { puts(""); return; } printf("%d\n", head->data); display_recur(head->link); } Node* reverse(Node* head) { Node *p, *q, *r; p = head; q = NULL; while (p != NULL) { r = q; q = p; p = p->link; q->link = r; } return q; } Node* reverse_recur(Node* p, Node* before) { if (p == NULL) return before; Node* next = p->link; p->link = before; return reverse_recur(next, p); } | cs |
'Coding > dataStucture' 카테고리의 다른 글
스택의 응용(후위 표기법) (3) | 2018.05.10 |
---|---|
Queue.c (0) | 2018.04.17 |
스택의 응용(괄호검사).c (0) | 2018.02.23 |
Stack.c (0) | 2018.02.23 |