Last active
June 3, 2020 04:58
-
-
Save dalcon10028/d2bff7a66bade3c4b3593c58bb0225f4 to your computer and use it in GitHub Desktop.
연결 리스트 실습 2
This 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> | |
| // 노드의 정의 | |
| struct _node { | |
| int data; | |
| struct _node *link; | |
| }; | |
| typedef struct _node node; | |
| int main(void) | |
| { | |
| node *current, *temp, *head; | |
| int num; | |
| /* 연결 리스트에서 첫 번째 노드를 예외 처리 */ | |
| /* head는 첫 번째 노드를 가리키는 포인터 */ | |
| /* temp는 새로운 노드를 연결시켜주기 위한 포인터 */ | |
| current = (node*) malloc(sizeof(node)); | |
| current->data = 1; | |
| current->link = NULL; | |
| head = temp = current; | |
| /* 두번째 노드부터 열번째 노드까지 반복 처리 */ | |
| /* temp는 증가하는 연결 리스트의 마지막 노를 가리키다 포인터 current에 의해 | |
| 생성되는 새로운 노드와 연결할 때 사용한다 */ | |
| num = 2; | |
| /* 2~10까지 노드를 새로 생성하고 마지막 노드와 연결하기를 반복합니다. */ | |
| while ( num <= 10 ) { | |
| current = (node*) malloc(sizeof(node)); | |
| if ( current == NULL ) { | |
| printf("Memory Allocation Failed!\n"); | |
| exit(0); | |
| } | |
| current->data = num; | |
| current->link = NULL; | |
| temp->link = current; | |
| temp = current; | |
| num++; | |
| } | |
| /* 생성을 마쳤으면 temp를 head(맨 처음 노드)로 이동 */ | |
| temp = head; | |
| /* 마지막 노드의 주소(NULL)에 도달할 때까지 각 노드의 data 출력 */ | |
| while ( temp->link != NULL ) { | |
| printf("%d -> ",temp->data); | |
| temp = temp->link; | |
| } | |
| // 마지막 노드 data만 따로 처리 | |
| printf("%d\n",temp->data); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment