#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void push(struct Node **headRef, int newData)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = (*headRef);
(*headRef) = newNode;
}
void insertAfter(struct Node *prevNode, int newData)
{
if (prevNode == NULL)
{
printf("이전 노드의 값이 NULL이면 안된다.");
return;
}
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = prevNode->next;
prevNode->next = newNode;
}
void append(struct Node **headRef, int newData)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *last = *headRef;
newNode->data = newData;
newNode->next = NULL;
if (*headRef == NULL)
{
*headRef = newNode;
return;
}
while (last->next != NULL)
{
last = last->next;
}
last->next = newNode;
return;
}
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}
int main(void)
{
struct Node *head = NULL;
append(&head, 6);
push(&head, 7);
push(&head, 1);
append(&head, 4);
insertAfter(head->next, 8);
printList(head);
return 0;
}