The following code snippets are some useful functions for using a simple linked list as a stack in C programming language.
(Please check also: Using a Simple Linked List as Queue in C)
struct node_t * push_1(struct node_t * head, struct node_t * node)
{
node->next = head;
return node;
}
// Example : 2
void push_2(struct node_t ** head, struct node_t * node)
{
node->next = *head;
*head = node;
}
struct node_t * pop(struct node_t ** head)
{
struct node_t * node = *head;
node->next = NULL;
*head = (*head)->next;
return node;
}