June is Combung
단일 연결리스트 본문
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
typedef struct node{
int data;
struct node* next;
} NODE;
void print_all(NODE* head){
NODE* tmp = head;
while(tmp){
printf("%d ", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
NODE* init(int data){
NODE* tmp = (NODE*)malloc(sizeof(NODE));
tmp->data = data;
tmp->next = NULL;
return tmp;
}
void add(NODE** p, int data){
if(!*p) { // *p == NULL
*p = init(data);
return;
}
add(&(*p)->next, data);
}
void main(){
NODE* head = NULL;
int select = 0;
while(1){
print_all(head);
printf("정수(종료 -1) : ");
scanf_s("%d", &select);
if(select == -1){ break; }
add(&head, select); // 리스트의 가장 마지막에 새노드를 추가하는 add() 구현
}
}
'C 자료구조와 알고리즘' 카테고리의 다른 글
이진 탐색 트리 (0) | 2021.06.29 |
---|---|
이중 연결리스트 (0) | 2021.06.29 |
Queue (0) | 2021.06.29 |
연습문제1 (0) | 2021.06.29 |
연결리스트(중간 노드 삽입 삭제) 이중포인터 버전 (0) | 2021.06.29 |
Comments