#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ===== Define structure outside function ===== */
/*
Use `typedef` to rename the data type.
`node_t` is equal with `struct node_t`, and
`node` is equal with `struct node_t *`.
*/
typedef struct node_t
{
int data;
struct node_t *next;
} node_t, *node;
/* Define link list
*/
typedef struct list_t
{
node head;
node rear;
int size;
} list_t, *list;
list list_create()
{
list l = NULL;
if ((l = malloc(sizeof(list_t))) == NULL)
{
exit(-1);
}
memset(l, 0, sizeof(list_t));
return l;
}
int list_destroy(list *pl)
{
node cur;
node prev;
if (pl == NULL || *pl == NULL)
return -1;
cur = (*pl)->head;
while (cur != NULL)
{
prev = cur;
cur = cur->next;
free(prev);
}
free(*pl);
*pl = NULL;
return 0;
}
int list_push(list l, int data)
{
node n;
if (l == NULL)
return -1;
if ((n = malloc(sizeof(node_t))) == NULL)
{
exit(-1);
}
n->data = data;
n->next = NULL;
if (l->head == NULL)
{
l->head = n;
}
else
{
l->rear->next = n;
}
l->rear = n;
l->size++;
return 0;
}
int list_pop(list l, int *pdata)
{
if (l == NULL || l->head == NULL)
{
return -1;
}
*pdata = l->head->data;
if (l->head == l->rear)
{
l->rear = NULL;
}
l->head = l->head->next;
l->size--;
return 0;
}
void list_foreach(list l, void (*func)(int data, void *arg), void *arg)
{
node cur = l->head;
while (cur != NULL)
{
func(cur->data, arg);
cur = cur->next;
}
}
static void print_node(int data, void *arg)
{
printf("%d\n", data);
}
int main(int argc, char *argv[])
{
int i;
int d;
list l;
l = list_create();
for (i = 0; i < 5; i++)
{
scanf("%d", &d);
getchar();
list_push(l, d);
}
list_foreach(l, print_node, NULL);
printf("Pop: [status %d], ", list_pop(l, &d));
printf("[value %d]\n", d);
printf("Pop: [status %d], ", list_pop(l, &d));
printf("[value %d]\n", d);
list_foreach(l, print_node, NULL);
list_destroy(&l);
return 0;
}