Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C | Size: 3.30 KB | Hits: 53 | Expires: Never
Copy text to clipboard
  1. /********************************************************************
  2.  * Peter Klein CM1745                                     3/18/2010 *
  3.  * CSSE 332 - Operating Systems                                     *
  4.  * Lab 2: Queue: queue.c                                            *
  5.  *                                                                  *
  6.  * Description:                                                     *
  7.  *     Implementation of the queue functions from queue.h           *
  8.  ********************************************************************/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "queue.h"
  13.  
  14. void printNode(Node *Anode);
  15.  
  16. /****************************************************
  17.  * Function: deleteQueue
  18.  * Remove all the nodes from the queue, freeing the
  19.  * space allocated to each node. The head and tail
  20.  * pointers must each be set to NULL.
  21.  ****************************************************/
  22. void deleteQueue(Queue *Aqueue){
  23.     return;
  24. }
  25.  
  26. /************************************************************
  27.  * Function: enqueue
  28.  * Add a node to the tail of the queue. The space for the node
  29.  * will be allocated on the heap. The tail pointer will point
  30.  * to this node and the head pointer will continue to point to
  31.  * the node at the start of the queue.
  32.  * Return TRUE if successful, FALSE otherwise.
  33.  ************************************************************/
  34. Bool enqueue(Queue *Aqueue, int processId, int arrivalTime,
  35.              int serviceTime, int remainingTime){
  36.  
  37.   Node *new = malloc(sizeof(Node));
  38.   new->processId = processId;
  39.   new->arrivalTime = arrivalTime;
  40.   new->serviceTime = serviceTime;
  41.   new->remainingTime = remainingTime;
  42.   new->next = NULL;
  43.  
  44.     if(Aqueue->head){
  45.         Aqueue->tail->next = new;
  46.         Aqueue->tail = new;
  47.     } else {
  48.         Aqueue->head = new;
  49.         Aqueue->tail = new;
  50.     }
  51.     return TRUE;
  52. }
  53.  
  54.  
  55.  
  56. /***************************************************
  57.  * Function: printQueue
  58.  * Display the values in each node in the queue.
  59.  * Starts at the head.
  60.  * Return the number of nodes in the queue.
  61.  ***************************************************/
  62. int printQueue(Queue Aqueue){
  63.     return 0;
  64. }
  65. void printNode(Node *Anode){
  66.     printf("%d\t%d\t%d\t%d\n",
  67.             Anode->processId,
  68.             Anode->arrivalTime,
  69.             Anode->serviceTime,
  70.             Anode->remainingTime);
  71. }
  72.  
  73. /****************************************************
  74.  * Function: queueSize
  75.  * Return the number of nodes in the queue.
  76.  ***************************************************/
  77. int queueSize(Queue Aqueue){
  78.     Node *n = Aqueue.head;
  79.     int nodeCount = 0;
  80.     while(n){
  81.         printNode(n);
  82.         nodeCount++;
  83.         n = n->next;
  84.     }
  85.     return nodeCount;
  86. }
  87.  
  88.  
  89.  
  90. /***************************************************
  91.  * Function: dequeue
  92.  * Remove a node from the head of the queue and return it.
  93.  * Free the space at the heap occupied by this node.
  94.  * The head pointer is updated to point to the next node.
  95.  * If this is the last item in the queue, then set the head
  96.  * and tail pointers to NULL.
  97.  * If the queue is empty, return an item with process id = -1.
  98.  ***************************************************/
  99. Node dequeue(Queue *Aqueue){
  100.     Node n;
  101.     n.processId = -1;
  102.     return n;
  103. }