Using a Simple Linked List as Queue in C

U

Simple data structures such as linked lists are almost always required in programming. The following code snippets provide the most basic functions for for using simple linked list as a queue in C programming language:

  • Insert Item
  • Remove Item

(Please check also: Using a Simple Linked List as Stack in C)

Insert Function (Example 1)
struct node_t * insert_1(struct node_t * head, struct node_t * node)
{
   struct node_t * temp = head;	
   if(head == NULL)
      return node;	
   while(temp->next!=NULL)
      temp=temp->next;	
   temp->next = node;	
   return head;
}
Insert Function (Example 2)
void insert_2(struct node_t ** head, struct node_t * node)
{
   struct node_t * temp = *head;	
   if(*head == NULL)
   {
      *head = node;
      return;
   }	
   while(temp->next!=NULL)
      temp=temp->next;	
   temp->next = node;	
}
Remove Function
struct node_t * remove_node(struct node_t ** head)
{
   struct node_t * node = *head;
   *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