티스토리 뷰
C, C++/데이터구조 & 자료구조
C언어 단순 연결 리스트(singly linked list ) 마지막 삽입, 삭제, 검색, 출력
j0n9m1n1 2018. 4. 8. 23:37반응형
마지막 삽입
삭제
삭제
검색
출력
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 | //2014244057 이종민 //함수명 앞에 *는 리턴하는 값이 포인터라는 거 //cls //삽입은 그냥 맨 뒤로 삽입// 헤드만 있을때, 헤드가 없을때, 노드들이 많을때 //삭제 // 지정 삭제 // 삭제할 데이터가 있을때(맨 앞 값일때, 맨 뒷값일때), 없을때 //출력 입력한 값이 있나 없나 // #include "stdafx.h" typedef struct ListNode { int data; struct ListNode *next; }node; typedef struct { node *head; }list_h; list_h *create_head(void) { list_h *L; L = (list_h*)malloc(sizeof(list_h)); L->head = NULL; return L; } void node_insert(list_h *L, int input) { node *newNode; node *temp; newNode = (node*)malloc(sizeof(node)); newNode->data = input; newNode->next = NULL; if (L->head == NULL) { L->head = newNode; return; } if (L->head == NULL) { L->head = newNode; return; } temp = L->head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; } void node_search(list_h *L, int input) { node *temp; temp = L->head; while (temp != NULL) { if (temp->data == input) { printf("\n노드에 %d이(가) 있습니다.\n\n", input); return; } else temp = temp->next; } printf("\n노드에 %d이(가) 없습니다.\n\n", input); } void node_delete(list_h *L, int input) { node *p = L->head; node *q = L->head->next; if (L->head == NULL) { printf("\n노드가 없습니다.\n\n", input); return; } if (L->head->data == input) { L->head = L->head->next; free(p); printf("\ndata가 %d인 노드를 삭제했습니다.\n\n", input); return; } while (p != 0 && p->data != input) { q = p; p = p->next; } if (p != NULL) { q->next = p->next; printf("\ndata가 %d인 노드를 삭제했습니다.\n\n", input); free(p); } else printf("\n%d이(가) 노드에 없습니다\n\n", input); } void node_print(list_h *L) { node *temp; temp = L->head; printf("\n리스트 출력: "); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n\n"); } void introduction() { printf("1. 연결리스트의 제일 뒤에 삽입\n"); printf("2. 연결리스트에서 검색\n"); printf("3. 연결리스트에서 노드 하나 삭제\n"); printf("4. 연결리스트 출력\n"); printf("5. 종료\n"); } int main() { list_h *L; L = create_head(); int menu = 10; int input = 0; printf("이 프로그램은 단순 연결 리스트 삽입, 검색, 삭제, 출력하는 프로그램입니다.\n"); while (menu != 5) { //system("cls"); introduction(); printf("실행하실 메뉴를 입력하세요: "); scanf("%d", &menu); if (menu == 1) { printf("맨 뒤에 삽입 할 값을 입력하세요: "); scanf("%d", &input); node_insert(L, input); system("cls"); //puts(""); } if (menu == 2) { printf("검색할 값을 입력하세요: "); scanf("%d", &input); system("cls"); node_search(L, input); } if (menu == 3) { printf("삭제할 값을 입력하세요: "); scanf("%d", &input); system("cls"); node_delete(L, input); } if (menu == 4) { system("cls"); node_print(L); //printf("연결리스트를 출력합니다.\n"); } if (menu == 5) printf("종료합니다.\n"); } } | cs |
'C, C++ > 데이터구조 & 자료구조' 카테고리의 다른 글
C언어 순차리스트 삽입, 삭제, 탐색, 출력 예제 (0) | 2018.03.22 |
---|---|
C언어 선택 정렬(Selection sort) (0) | 2016.06.06 |
C언어 버블정렬 알고리즘(Bubble sorting algorithm) (0) | 2016.06.06 |
C언어 하노이 타워 알고리즘(Hanoi Tower Algorithm) (0) | 2016.06.06 |
C언어 재귀(Recursive) 피보나치 수열 (0) | 2016.06.06 |
댓글
티스토리 방명록
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
Blog is powered by
Tistory / Designed by
Tistory
Contact: j0n9m1n1@gmail.com
Contact: j0n9m1n1@gmail.com