12-1연결리스트(재귀)

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <stdlib.h> // malloc함수(동적할당), free함수(동적해제) #define EMPTY 0 typedef struct node { int data; struct node* link; }General; General* GetNode() { //노드를 힙 지역에 만들어주는 함수 General* newNode = (General *)malloc(sizeof(General)); newNode->link = EMPTY; return newNode; } void Insert(General** head, int data) { if (*head == EMPTY) { General* newNode = GetNode(); newNode->data = data; *head = newNode; return; } General* tmp = *head; if ((*tmp).link == EMPTY) { General* newNode = GetNode(); newNode->data = data; (*tmp).link = newNode; } else { tmp = (*tmp).link; Insert(head, data); } } void main() { General* head = EMPTY; Insert(&head, 10); Insert(&head, 20); Insert(&head, 30); }
Editor Settings
Theme
Key bindings
Full width
Lines