queue print function

Posted on 2011-10-09
Last Modified: 2012-08-13
I would like to create a function which print all the nodes of the queue (i have already create all the other functions (for example  add new node etc))

The structure which i used :
typedef struct node
      char *name;
      int Identity;
      int time;
      struct node *next;

typedef struct
      NODE *head;
      NODE *tail;
      int size;

How to create the function for print all the nodes of the queue???????
Question by:Tom3333
    LVL 47

    Expert Comment


    I think thos one from
    shows how to do it:

    METHOD 2 (Use Queue)
    For each node, first the node is visited and then it’s child nodes are put in a FIFO queue.
    1) Create an empty queue q
    2) temp_node = root /*start from root*/
    3) Loop while temp_node is not NULL
        a) print temp_node->data.
        b) Enqueue temp_node’s children (first left then right children) to q
        c) Dequeue a node from q and assign it’s value to temp_node
    Here is a simple implementation of the above algorithm. Queue is implemented using an array with maximum size of 500. We can implement queue as linked list also.
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_Q_SIZE 500
    /* A binary tree node has data, pointer to left child
       and a pointer to right child */
    struct node
        int data;
        struct node* left;
        struct node* right;
    /* frunction prototypes */
    struct node** createQueue(int *, int *);
    void enQueue(struct node **, int *, struct node *);
    struct node *deQueue(struct node **, int *);
    /* Given a binary tree, print its nodes in level order
       using array for implementing queue */
    void printLevelOrder(struct node* root)
      int rear, front;
      struct node **queue = createQueue(&front, &rear);
      struct node *temp_node = root;
        printf("%d ", temp_node->data);
        /*Enqueue left child */
          enQueue(queue, &rear, temp_node->left);
        /*Enqueue right child */
          enQueue(queue, &rear, temp_node->right);
        /*Dequeue node and make it temp_node*/
        temp_node = deQueue(queue, &front);
    struct node** createQueue(int *front, int *rear)
      struct node **queue =
       (struct node **)malloc(sizeof(struct node*)*MAX_Q_SIZE); 
      *front = *rear = 0;
      return queue;
    void enQueue(struct node **queue, int *rear, struct node *new_node)
      queue[*rear] = new_node;
    struct node *deQueue(struct node **queue, int *front)
      return queue[*front - 1];
    /* Helper function that allocates a new node with the
       given data and NULL left and right pointers. */
    struct node* newNode(int data)
      struct node* node = (struct node*)
                           malloc(sizeof(struct node));
      node->data = data;
      node->left = NULL;
      node->right = NULL;
    /* Driver program to test above functions*/
    int main()
      struct node *root = newNode(1);
      root->left        = newNode(2);
      root->right       = newNode(3);
      root->left->left  = newNode(4);
      root->left->right = newNode(5);
      printf("Level Order traversal of binary tree is \n");
      return 0;

    Open in new window

    LVL 47

    Expert Comment

    No, sorry, that was for binary tree, rather than for queue

    Author Comment

    Something else about queue?
    LVL 47

    Expert Comment


    Have you created funtion pop() which would get the highest element of the queue and remove it form the queue ?
    and function empty() which will check id the queue is empty? and function print() for printing the node?

    with all these functions, I think,  something like that should work

    while (!empty(q)){
    NODE n = pop(q);



    Author Comment

    Is anyone who know any web site with information about queue with double link list ????
    LVL 1

    Expert Comment

    Easy way to print all nodes from your queue is:
    QUEUE q;
    NODE n;
    for (n = q->head;
           n != NULL;
           n = n->next)
      printf("%s %d %d\n", n->name, n->Identity, n->time);

    LVL 1

    Accepted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now