Link to home
Start Free TrialLog in
Avatar of Tom3333
Tom3333

asked on

queue print function

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???????
Avatar of for_yan
for_yan
Flag of United States of America image



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

No, sorry, that was for binary tree, rather than for queue
Avatar of Tom3333
Tom3333

ASKER

Something else about queue?

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);

}



Avatar of Tom3333

ASKER

Is anyone who know any web site with information about queue with double link list ????
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);

ASKER CERTIFIED SOLUTION
Avatar of joe_maniac
joe_maniac

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial