포인터를 통해 연결되어있는 값들
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
struct _Node* next;
int data;
};
void addFirst(struct Node* target, int data)
{
if (target == NULL)
return;
struct Node* newNode = malloc(sizeof(struct Node));
if (newNode == NULL)
return;
newNode->next = target->next;
newNode->data = data;
target->next = newNode;
}
void removeFirst(struct Node* target)
{
if (target == NULL)
return;
struct Node* removedNode = target->next;
if (removedNode == NULL)
return;
target->next = removedNode->next;
free(removedNode);
}
int main()
{
struct Node* head = malloc(sizeof(struct Node));
head->next = NULL;
addFirst(head, 10);
addFirst(head, 20);
addFirst(head, 30);
removeFirst(head);
// 조회
struct Node* curr = head->next;
while (curr != NULL)
{
printf("%d\n", curr->data);
curr = curr->next;
}
// 메모리 해제
curr = head->next;
while (curr != NULL)
{
struct NODE* next = curr->next;
free(curr);
curr = next;
}
free(head);
return 0;
}