daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void enqueue(queue *q, treenode *item){
  2.     node *new_node = (node *)malloc(sizeof(node));
  3.    
  4.     if(new_node == NULL){
  5.         printf("memory overflow\n");
  6.         exit(1);
  7.     }
  8.    
  9.     new_node->element = item;
  10.     new_node->next = NULL;
  11.    
  12.     if(is_empty_queue(q)){
  13.         q->front = new_node;
  14.         q->rear = new_node;
  15.     }else{
  16.     //  if(((*new_node).element)<((*new_node->next).element))
  17.        
  18.        
  19.        
  20.         (q->rear)->next = new_node;
  21.         q->rear = new_node;
  22.     }
  23.    
  24.     q->size++;
  25. }
  26.  
  27. treenode *dequeue(queue *q){
  28.     node *temp;                                     /*reference to the to-be dequeued node*/
  29.     treenode *result;
  30.    
  31.     if(is_empty_queue(q)){
  32.         printf("Queue underflow\n");
  33.         exit(1);
  34.     }
  35.    
  36.     temp = q->front;
  37.     q->front = (q->front)->next;
  38.    
  39.     if(q->front == NULL){
  40.         q->rear = NULL;
  41.     }
  42.     q->size--;
  43.    
  44.     result = temp->element;
  45.     free(temp);
  46.    
  47.     return result;
  48. }
  49.  
  50.  
  51. void *construct(struct arrange arr[], int length){
  52.     int i;
  53.     int n;
  54.     queue q;
  55.     treenode *temp;
  56.     treenode *x, *y, *z;
  57.     init_queue(&q);
  58.     printf("\nenqueue ");
  59.     /*wrap the elements to treenodes and insert them to the queue*/
  60.     for(i = 0; i < length; i++){
  61.         temp = (treenode *)malloc(sizeof(treenode));
  62.         if(temp == NULL){
  63.             printf("Memory overflow\n");
  64.             exit(1);
  65.         }
  66.         temp->element = arr[i].freq;
  67.         temp->left = NULL;
  68.         temp->right = NULL;
  69.         printf("%d ", temp->element);
  70.         enqueue(&q,temp);
  71.     }
  72.     printf("\ndequeue ");
  73.     n = q.size;
  74.     for(i = 0; i < n - 1; i++){
  75.         x = dequeue(&q);
  76.         y = dequeue(&q);
  77.         z = (treenode *)malloc(sizeof(node));
  78.         if(z == NULL){
  79.             printf("memory overflow");
  80.             exit(1);
  81.         }
  82.         z->element = x->element + y->element;
  83.         z->left = x;
  84.         z->right = y;
  85.         printf("%d ",z->element);
  86.         enqueue(&q,z);
  87.     /*  enqueue a treenode*/
  88.     }
  89.    
  90.     return dequeue(&q);
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top