using Queue_3102; /* * Queue for CSC 3102 Data Structures Portfolio * using Qtype */ /* * --REQUIRED METHODS-- * Queue() * Enqueue(Qtype item) * Qtype Dequeue() * Qtype Peek() * int size() * --OPTIONAL METHODS-- * bool IsEmpty() * Qtype PeekAhead(int num) * printArray() */ namespace Queue_3102 { class Queue { internal class QueueNode { public QueueNode? next; public QueueNode? prev; public Q? data; public QueueNode(Q value, QueueNode? prev = null, QueueNode? next = null) { data = value; this.next = next; this.prev = prev; } } private QueueNode? front, back; public int size; //default constuctor public Queue() { front = null; back = null; size = 0; } /* * Adds an item to the back of the Queue * @item - the value of the item you are adding */ public void Enqueue(Qtype item) { var newNode = new QueueNode(item, null, back); back = newNode; if (size == 0) { front = newNode; } if (size != 0) { newNode.next.prev = back; } size++; } public Qtype Dequeue() { if (size == 0) { throw new Exception("The Queue is empty"); } else if (size == 1) { var node = front.data; front = null; back = null; size = 0; return node; } else { var retNode = front.data; front.prev.next = null; front = front.prev; size--; return retNode; } } /* * Returns the value at the front of the queue */ public Qtype Peek() { if (front == null || front.data == null) { throw new Exception("The queue is empty"); } else { return front.data; } } /* * Returns the size of the queue */ public int Size() { return size; } /* * Prints the Queue from back to front */ public void PrintArray() { Console.Write("[ "); var node = back; if (node == null) { Console.Write("'null'"); } else { for (int i = 0; i < size; i++) { Console.Write(node.data + " "); node = node.next; } } Console.WriteLine(" ]"); } } }