Using a Simple Linked List as Stack in C

U

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;
}
Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags