?
Solved

queue print function

Posted on 2011-10-09
7
Medium Priority
?
274 Views
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;
} NODE;

typedef struct
{
      NODE *head;
      NODE *tail;
      int size;
} QUEUE;


How to create the function for print all the nodes of the queue???????
0
Comment
Question by:Tom3333
  • 3
  • 2
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36939676


I think thos one from
http://www.geeksforgeeks.org/archives/2686
shows how to do it:

METHOD 2 (Use Queue)

Algorithm:
For each node, first the node is visited and then it’s child nodes are put in a FIFO queue.

printLevelorder(tree)
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

Implementation:
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;
 
  while(temp_node)
  {
    printf("%d ", temp_node->data);
 
    /*Enqueue left child */
    if(temp_node->left)
      enQueue(queue, &rear, temp_node->left);
 
    /*Enqueue right child */
    if(temp_node->right)
      enQueue(queue, &rear, temp_node->right);
 
    /*Dequeue node and make it temp_node*/
    temp_node = deQueue(queue, &front);
  }
}
 
/*UTILITY FUNCTIONS*/
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;
  (*rear)++;
}    
 
struct node *deQueue(struct node **queue, int *front)
{
  (*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;
 
  return(node);
}
 
/* 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");
  printLevelOrder(root);
 
  getchar();
  return 0;
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36939686
No, sorry, that was for binary tree, rather than for queue
0
 

Author Comment

by:Tom3333
ID: 36939792
Something else about queue?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36940012

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);
print(n);

}



0
 

Author Comment

by:Tom3333
ID: 36952421
Is anyone who know any web site with information about queue with double link list ????
0
 
LVL 1

Expert Comment

by:joe_maniac
ID: 37225674
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);

0
 
LVL 1

Accepted Solution

by:
joe_maniac earned 200 total points
ID: 37225676
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

862 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