#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAX 5001
#define MALLOC(p,s)\
	if( ! ( (p) = malloc(s) ) ) { \
		fprintf(stderr, "Insufficient memory!"); \
		exit(EXIT_FAILURE); \
	}


typedef struct node* nodePointer;
typedef struct node {
	int data;
	nodePointer next;
} node;

void initLinkedList(nodePointer first) {
	first->next = NULL;
}

void addList(nodePointer first, nodePointer now) {
	if (first->next == NULL) { // 첫 번째 노드가 NULL 이면
		now->next = first->next;
		first->next = now;
	}
	else { // 첫 번째 노드 앞에 삽입
		now->next = NULL;
		first->next = now->next;
		now->next = first;
	}
}
nodePointer findLastNode(nodePointer first) {
	nodePointer curr = first->next; //순회용 노드 생성
	while (curr->next != NULL) {
		curr = curr->next;
	}
	return curr;
}

nodePointer printLinkedList(nodePointer first) {
	nodePointer curr = first->next; //순회용 노드 생성
	while (curr->next != NULL) {
		printf("%d\n", curr->data);
		curr = curr->next;
	}
	printf("%d\n", curr->data);

}

int main(void) {
	nodePointer head2, nodeA, nodeB, nodeC;
	MALLOC(head2, sizeof(node));
	MALLOC(nodeA, sizeof(node));
	MALLOC(nodeB, sizeof(node));
	MALLOC(nodeC, sizeof(node));
    
	nodeA->data = 111;
	nodeB->data = 222;
	nodeC->data = 333;

	initLinkedList(head2); // 연결리스트 초기화
	addList(head2, nodeA);
	addList(nodeA, nodeB);
	addList(nodeB, nodeC);

	nodeA->next = nodeB->next;
	free(nodeB);
	printLinkedList(head2);


	free(head);
	for (i = 0; i < N; i++) {
		free(nodes[i+1]);
	}

	return 0;
}

+ Recent posts