#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode {
element data;
struct ListNode* link;
}ListNode;
void error(char* message) {
fprintf(stderr, "%s\n", message);
exit(1);
}
ListNode* insertFirst(ListNode* head, int value) {
ListNode* p = (ListNode*)malloc(sizeof(ListNode));
p->data = value;
p->link = head;
head = p;
return head;
}
ListNode* insert(ListNode* head, ListNode* pre, element value) {
ListNode* p = (ListNode*)malloc(sizeof(ListNode));
p->data = value;
p->link = pre->link;
pre->link = p;
return head;
}
ListNode* deleteFirst(ListNode* head) {
ListNode* removed;
if (head == NULL) return NULL;
removed = head;
head = removed->link;
free(removed);
return head;
}
ListNode* delete(ListNode* head, ListNode* pre) {
ListNode* removed;
removed = pre->link;
pre->link = removed->link;
free(removed);
return head;
}
void printList(ListNode* head) {
for (ListNode* p = head;p != NULL; p = p->link) {
printf("%d->", p->data);
}
printf("NULL\n");
}
int main(void) {
ListNode* head = NULL;
for (int i = 0;i < 5;i++) {
head = insertFirst(head, i);
printList(head);
}
for (int i = 0;i < 5;i++) {
head = deleteFirst(head);
printList(head);
}
return 0;
}
설명 : c언어를 활용한 단순 연결 리스트