/********************************************************************
* Peter Klein CM1745 3/18/2010 *
* CSSE 332 - Operating Systems *
* Lab 2: Queue: queue.c *
* *
* Description: *
* Implementation of the queue functions from queue.h *
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
void printNode(Node *Anode);
/****************************************************
* Function: deleteQueue
* Remove all the nodes from the queue, freeing the
* space allocated to each node. The head and tail
* pointers must each be set to NULL.
****************************************************/
void deleteQueue(Queue *Aqueue){
return;
}
/************************************************************
* Function: enqueue
* Add a node to the tail of the queue. The space for the node
* will be allocated on the heap. The tail pointer will point
* to this node and the head pointer will continue to point to
* the node at the start of the queue.
* Return TRUE if successful, FALSE otherwise.
************************************************************/
Bool enqueue(Queue *Aqueue, int processId, int arrivalTime,
int serviceTime, int remainingTime){
Node *new = malloc(sizeof(Node));
new->processId = processId;
new->arrivalTime = arrivalTime;
new->serviceTime = serviceTime;
new->remainingTime = remainingTime;
new->next = NULL;
if(Aqueue->head){
Aqueue->tail->next = new;
Aqueue->tail = new;
} else {
Aqueue->head = new;
Aqueue->tail = new;
}
return TRUE;
}
/***************************************************
* Function: printQueue
* Display the values in each node in the queue.
* Starts at the head.
* Return the number of nodes in the queue.
***************************************************/
int printQueue(Queue Aqueue){
return 0;
}
void printNode(Node *Anode){
printf("%d\t%d\t%d\t%d\n",
Anode->processId,
Anode->arrivalTime,
Anode->serviceTime,
Anode->remainingTime);
}
/****************************************************
* Function: queueSize
* Return the number of nodes in the queue.
***************************************************/
int queueSize(Queue Aqueue){
Node *n = Aqueue.head;
int nodeCount = 0;
while(n){
printNode(n);
nodeCount++;
n = n->next;
}
return nodeCount;
}
/***************************************************
* Function: dequeue
* Remove a node from the head of the queue and return it.
* Free the space at the heap occupied by this node.
* The head pointer is updated to point to the next node.
* If this is the last item in the queue, then set the head
* and tail pointers to NULL.
* If the queue is empty, return an item with process id = -1.
***************************************************/
Node dequeue(Queue *Aqueue){
Node n;
n.processId = -1;
return n;
}