-
문제 04-2 더미 노드Data Structure/윤성우의 열혈 자료구조 2019. 9. 20. 18:34This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include <stdio.h> #include <stdlib.h> #pragma warning(disable:4996) typedef struct _node { int data; struct _node * next; } Node; int main(void) { Node * head = NULL; Node * tail = NULL; Node * cur = NULL; Node * newNode = NULL; int readData; head = (Node*)malloc(sizeof(Node)); // 추가 된 문장, 더미 노드 추가 tail = head; /**** 데이터를 입력 받는 과정 ****/ while (1) { printf("자연수 입력: "); scanf("%d", &readData); if (readData < 1) break; /*** 노드의 추가과정 ***/ newNode = (Node*)malloc(sizeof(Node)); newNode->data = readData; newNode->next = NULL; /* if(head == NULL) head = newNode; else tail->next = newNode; */ tail->next = newNode; tail = newNode; } printf("\n"); /**** 입력 받은 데이터의 출력과정 ****/ printf("입력 받은 데이터의 전체출력! \n"); if (head == NULL) { printf("저장된 자연수가 존재하지 않습니다. \n"); } else { cur = head; // printf("%d ", cur->data); // 첫 번째 데이터 출력 while (cur->next != NULL) // 두 번째 이후의 데이터 출력 { cur = cur->next; printf("%d ", cur->data); } } printf("\n\n"); /**** 메모리의 해제과정 ****/ if (head == NULL) { return 0; // 해제할 노드가 존재하지 않는다. } else { Node * delNode = head; Node * delNextNode = head->next; // printf("%d을(를) 삭제합니다. \n", head->data); // free(delNode); // 첫 번째 노드의 삭제 while (delNextNode != NULL) // 두 번째 이후의 노드 삭제 위한 반복문 { delNode = delNextNode; delNextNode = delNextNode->next; printf("%d을(를) 삭제합니다. \n", delNode->data); free(delNode); // 두 번째 이후의 노드 삭제 } } return 0; } 'Data Structure > 윤성우의 열혈 자료구조' 카테고리의 다른 글
문제05-2 더미 노드 기반 양방향 연결 리스트 (31) 2019.09.20 문제04-4 정렬의 기준으로 활용되는 함수의 정의 (31) 2019.09.20 문제 04-1 연결 리스트 관련 코드에 익숙해지기 (0) 2019.09.05 문제 03 -1 리스트 라이브러리의 활용 (0) 2019.09.04 문제 11-1 이진 탐색 트리의 조건 (0) 2019.08.16