public void dequeue() { if (isEmpty()) { } else { // Remove the front node. CinderellaNode temp; temp = new CinderellaNode(); temp = front; front = front.Next; temp = null; //<------Come Back to this later // Update numItems. numItems--; if (numItems == 0) { front = null; rear = null; } } }
public void enqueueToFront(CinderellaClass cind) { cind.Priority = 1; if (isEmpty()) { priorityEnqueue(cind); } else { CinderellaNode newFront; newFront = new CinderellaNode(); newFront.Cinderella = cind; newFront.Next = front; front = newFront; numItems++; } }
private CinderellaNode _next; // Pointer to the next node #endregion Fields #region Constructors // Constructor public CinderellaNode() { _cinderella = null; _next = null; }
public bool selectiveDequeue(int CindID) { if (isEmpty()) { return false; } else { // Cursor node to point to node with target ID CinderellaNode cursor = new CinderellaNode(); cursor = front; // Cursor node to points to node in front of the cursor CinderellaNode previousCursor = new CinderellaNode(); previousCursor = null; //Search for a cinderella with the same ID while (cursor.Cinderella.CinderellaID != CindID) { if (cursor == rear) { break; } previousCursor = cursor; cursor = cursor.Next; } //If id is match remove the cinderella from the queue and reasign pointers accordingly if (cursor.Cinderella.CinderellaID == CindID) { //If front cinderella matches ID just call dequeue if (cursor == front) { dequeue(); } else { //If matching cinderella is the last one in the queue reassign the rear pointer if (cursor == rear) { rear = previousCursor; previousCursor.Next = null; } //Otherwise rearrange the pointer of the previous cinderella to the next possible cinderella else { previousCursor.Next = cursor.Next; } //Remove cinderella from queue cursor = null; // Update numItems. numItems--; } return true; } else { return false; } } }
public CinderellaQueue() { front = null; rear = null; numItems = 0; }
public void recalibratePriority() { if (isEmpty()) { //Do Nothing } else { // Cursor node to point to node with target ID CinderellaNode cursor = new CinderellaNode(); cursor = front; for (int i = 0; i < numItems; i++) { //Temp used to check priority CinderellaNode temp; temp = new CinderellaNode(); temp = cursor; if (cursor != rear) { cursor = cursor.Next; } //If cinderella is earlly... if (temp.Cinderella.Priority == 2) { //Time window is subject change double maxTimeWindow = 15.0; //If the cinderella is now within her time window... if (temp.Cinderella.InScheduledTimeWindow(maxTimeWindow)) { //Store the cinderellas' ID before removing them from the queue int targetCinderellaID = temp.Cinderella.CinderellaID; //Selectively dequeue them selectiveDequeue(targetCinderellaID); //Recreate instance of the cinderella that has been removed from the queue CinderellaClass targetedCinderella = new CinderellaClass(targetCinderellaID); //Set the cinderellas' priority to 1 targetedCinderella.Priority = 1; //Insert the cinderella back into the queue priorityEnqueue(targetedCinderella); } } else if (temp.Cinderella.Priority == 3) { double maxWaitTime = 30.0; if (temp.Cinderella.WaitingForXTime(maxWaitTime)) { //Store the cinderellas' ID before removing them from the queue int targetCinderellaID = temp.Cinderella.CinderellaID; //Selectively dequeue them selectiveDequeue(targetCinderellaID); //Recreate instance of the cinderella that has been removed from the queue CinderellaClass targetedCinderella = new CinderellaClass(targetCinderellaID); //Set the cinderellas' priority to 1 targetedCinderella.Priority = 1; //Insert the cinderella back into the queue priorityEnqueue(targetedCinderella); } } } } }
// Queue operations public void priorityEnqueue(CinderellaClass val) { CinderellaNode newNode; // Create a new node and store num there. newNode = new CinderellaNode(); newNode.Cinderella = val; newNode.Next = null; // Setting newNode as front and rear if no nodes are in queue if (isEmpty()) { front = newNode; rear = newNode; } else { // Cursor node to point to node in front of target position CinderellaNode cursor = new CinderellaNode(); cursor = front; // Checking if the new Node should be front if (cursor.Cinderella.Priority > newNode.Cinderella.Priority) { newNode.Next = cursor; front = newNode; } else { // Setting newNode as the rear if cursor was the last node if (cursor == rear) { } else { while (cursor.Next.Cinderella.Priority <= newNode.Cinderella.Priority) { cursor = cursor.Next; // Setting the cursor to its NEXT node or breaking if cursor is rear if (cursor == rear) { break; } } } // Setting newNode as the cursor's next, and newNode's next as cursor's previous next CinderellaNode temp = new CinderellaNode(); temp = cursor.Next; cursor.Next = newNode; newNode.Next = temp; // Setting newNode as the rear if cursor was the last node if (cursor == rear) { rear = newNode; } } } // Update numItems. numItems++; }