typedef int element;
typedef struct ListNode {
element data;
struct ListNode* link;
}ListNode;
ListNode* insert_first(ListNode* head, int value) {
ListNode* p;
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;
p = (ListNode*)malloc(sizeof(ListNode));
p->data = value;
p->link = pre->link;
return head;
}
void print_list(ListNode* head) {
ListNode* p;
p = head;
while (p != NULL) {
printf("%d\n", p->data);
p = p->link;
}
}
ListNode* delete_first(ListNode* head) {
ListNode* removed;
if (head != 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;
}
int main(void) {
ListNode* head = NULL;
for (int i = 0; i < 5; i++) {
head = insert_first(head, i);
print_list(head);
}
for (int i = 0; i < 5; i++) {
head = delete_first(head);
print_list(head);
}
return 0;
}