How to Make a Queue Function in the C Syntax

Programmers use queues to represent data structures that work by the First In, First Out (FIFO) guideline. This means any data entered into the structure will append to a list of data, and the data that has been in the list the longest will be the first to be removed. In the C programming language, a programmer best implements a basic form of this structure using a linked list and various pointers to maintain the order of the data.

Instructions

    • 1

      Create the data node structure and the list structure. In order for the linked list to function as a queue, the list must contain nodes capable of referencing the next node in the list and two pointers that reference the beginning and the end of the list. These example structures show a basic template for the list Nodes and the Queue structure:

      struct node{

      int data;

      struct * Node next;

      };

      struct queue_list{

      struct * node first; //pointer to the first item

      struct *node last; //pointer to the last item

      }

    • 2

      Implement a data insertion algorithm. In a queue, the program should always append the data to the end of the list. Furthermore, the list must reference the last element, so future insertions know on which end to append. The following example shows a simple algorithm to append a node with data onto a queue:

      void insert(struct * queue q, int value){

      struct *node newnode = malloc(sizeof(struct node));

      newnode->data = value;

      newnode-> next = NULL;

      if(q->first == NULL){

      q->first = q->last = newnode; //if list is empty, first and last = newnode

      }

      else{

      q->last->next = newnode; //append newnode after last element

      q->last = last->next; // point "last" pointer to the new node

      }

      }

    • 3

      Implement a removal algorithm. This algorithm will take the value from the first node in the list. Then, it will point the "first" pointer to the next node and delete the first node. This will make the next node the new first position on the list, ready for removal. The following example shows an algorithm to accomplish this:

      int remove(struct * queue q){

      int value = q->first->data;

      struct * node temp = q->first;

      q->first = q->first->next; // moves the first pointer to the next item

      free(temp); //deletes the old first node

      return value; //returns the first value;

      }

    • 4

      Declare and use a queue. The programmer can declare a queue as variable of type "struct queue" and use the defined functions to add and remove items. The following example shows how to declare and use a queue structure:

      int main(){

      struct queue q;

      insert(&q, 5); //uses a reference to "q" to avoid shallow copies

      insert(&q, 6);

      int x = remove(&q); // x = 5

Tips & Warnings

  • This article details a skeleton of a queue list. It does not contain any safeguards to avoid retrieving items from empty lists or protections to avoid NULL pointers. This is only a barebones example.

Related Searches:

References

Comments

Related Ads

Featured