Queue arranges element in such a way that new element always gets added in the back and also when comes the removal it removes the last or oldest element or which is the first element in the queue. It is also known as FIFO or First In First Out.

Let us take a situation of a long row of queue of people at the entrance. Now when the entrance will be open the first person who was waiting will be removed. Then evey person will be shifted one position in the front. If some new person comes he/she has to stand at the back of the last person.

Queue of People

A queue can easily be visualized with the help of a linked list or collection where nodes are added at tail and deleted from head or from the front.

Queue

// queue.cpp : Queue demo program.
//

#include<malloc.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
struct node
{
  int value;
  struct node * next;
};

struct node * queue_add(struct node *queue, int value)
{
  struct node * new_node = NULL;
  new_node = (struct node *)malloc(sizeof(struct node));
  if(new_node == NULL)
  {
    printf("Terminating: no more memory left!\n");
    exit(-1);
    return NULL;
  }
  new_node->value = value;
  new_node->next = NULL;
  if(queue == NULL)
  {
    queue = new_node;
  }
  else
  {
    while(queue->next != NULL)
    {
        queue = queue->next;
    }
      queue->next = new_node;
  }
  return new_node;

}
struct node * queue_remove(struct node *queue)
{
  struct node * temp;
  if(queue == NULL)
  {
    printf("Queue is empty!\n");
    return NULL;
  }
  else
  {
    printf("One element deleted\n");
    temp = queue->next;
    free(queue);
    return temp;
  }


}
void queue_printall(struct node *queue)
{
  struct node * l_node = queue;
  while(l_node != NULL)
  {
    printf("node(0x%X) value = %d\n", l_node, l_node->value);
    l_node = l_node->next;
  }

}
void queue_removeall(struct node *queue)
{
  struct node * l_temp = NULL;
  struct node * l_node = queue;
  while(l_node != NULL)
  {
    l_temp = l_node->next;
    printf("deleting node(0x%X) value = %d\n",
            l_node,
            l_node->value);
    free(l_node);
    l_node = l_temp;
  }

}
int main(int argc, char* argv[])
{
  struct node *queue  = NULL;
  struct node *temp  = NULL;
    char input;
  int node_val;
  while(1)
  {
    printf("Add a new element [a]\n"
           "Delete a new element [d]\n"
           "Exit [ESC]\nEnter :");
    input = getch();
    if(input == 'a')
    {
        printf("\nEnter a number :");
        scanf("%d", &node_val);
        temp = queue_add(queue, node_val);
        if(temp)
        {
          printf("One element has been added\n");
          if(queue == NULL)
          {
            queue = temp;
          }
        }
    }
    else if(input == 'd')
    {
        queue = queue_remove(queue);
    }
    else
    {
         break;
    }
      
    printf("Current queue elements\n");    
    queue_printall(queue);

  }
  queue_removeall(queue);
  return 0;
}



You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

Linked List, Single Linked list, Doubly Linked list, Circular Linked list, Doubly Circular Linked list, Queue, reverse single linked list, delete single linked list, Stack,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan