cookchar

Message Queue Insertion Example

Dec 1st, 2019
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NODES 13
  5.  
  6. struct node{
  7.     struct node *next;
  8.     int ts;
  9.     int nid;
  10. };
  11.  
  12. typedef struct node ncmsg;
  13.  
  14. void printList(ncmsg *head) {
  15.     ncmsg *temp = head;
  16.     int p = 0;
  17.     while (temp != NULL) {
  18.         printf("(%d, %d)\t", temp -> nid, temp -> ts);
  19.         p++;
  20.         if (p % 8 == 0) printf("\n");
  21.         temp = temp -> next;
  22.     }
  23.     printf("\nThe list is shown above.\n");
  24. }
  25.  
  26. void insertMessage(ncmsg **head, ncmsg *newMsg) {
  27.     if (*head == NULL) {
  28.         printf("A.\n");
  29.         *head = newMsg;
  30.     } else {
  31.         ncmsg *temp = *head;
  32.         if (
  33.             (newMsg -> ts) < (temp -> ts) || (
  34.                 (newMsg -> ts) == (temp -> ts) &&
  35.                 (newMsg -> nid) < (temp -> nid)
  36.             )
  37.         ) { // if the new message has to become the head
  38.             printf("B.\n");
  39.             newMsg -> next = temp;
  40.             *head = newMsg;
  41.         } else { // if the new message goes after the head somewhere
  42.             printf("C.\n");
  43.             while (
  44.                 (temp -> next) != NULL && (
  45.                     (newMsg -> ts) > (temp -> next -> ts) || (
  46.                         (newMsg -> ts) == (temp -> next -> ts) &&
  47.                         (newMsg -> nid) > (temp -> next -> nid)
  48.                     )
  49.                 )
  50.             ) {
  51.                 temp = temp -> next;
  52.             }
  53.             newMsg -> next = temp -> next;
  54.             temp -> next = newMsg;
  55.         }
  56.     }
  57. }
  58.  
  59. void popHead(ncmsg **head) {
  60.     if ((*head) -> next != NULL) {
  61.         *head = ((*head) -> next);
  62.     }
  63.     else {
  64.         perror("Head is the sole element of the list, popping it will destroy the list!\nAborting!\n");
  65.     }
  66. }
  67.  
  68. int main() {
  69.     srand(11091996);
  70.     int i, j;
  71.     int tsArr[NODES];
  72.  
  73.     for (i = 0; i < NODES; i++) tsArr[i] = 1;
  74.  
  75.     ncmsg *temp, *head;
  76.     /*
  77.     head = (ncmsg *) malloc(sizeof(ncmsg));
  78.     head -> ts = 0;
  79.     head -> nid = 0;
  80.     head -> next = NULL;
  81.     */
  82.     head = NULL;
  83.  
  84.     for (i = 0; i < 64; i++) {
  85.         j = (rand() % NODES) + 1;
  86.        
  87.         temp = (ncmsg *) malloc(sizeof(ncmsg));
  88.        
  89.         temp -> ts = tsArr[j - 1];
  90.         tsArr[j - 1]++;
  91.         temp -> nid = j;
  92.         temp -> next = NULL;
  93.        
  94.         printf("New message: (%d, %d). ", temp -> nid, temp -> ts);
  95.         insertMessage(&head, temp);
  96.         //printList(head);
  97.     }
  98.  
  99.     printList(head);
  100.     printf("Now popping off the head node...\n");
  101.     popHead(&head);
  102.     printList(head);
  103.     printf("Done.\n");
  104.  
  105.     return 0;
  106. }
RAW Paste Data