Created
March 13, 2012 01:20
-
-
Save dislogical/2026003 to your computer and use it in GitHub Desktop.
Queue Data Structure: FILO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace YOUR_PROJECT_NAMESPACE | |
| { | |
| /// <summary> | |
| /// Queue class</summary> | |
| /// <typeparam name="T"> | |
| /// Type of object stored in queue</typeparam> | |
| /// <author>Colden Cullen</author> | |
| class Queue<T> : QueueSaver<T> | |
| { | |
| #region Constructor | |
| /// <summary> | |
| /// Queue constructor | |
| /// </summary> | |
| public Queue() | |
| { | |
| count = 0; | |
| head = null; | |
| tail = null; | |
| } | |
| #endregion | |
| #region Enqueue Dequeue | |
| /// <summary> | |
| /// Adds an element to the back of the queue | |
| /// </summary> | |
| /// <param name="data"> | |
| /// The element to be added</param> | |
| public void Enqueue( T data ) | |
| { | |
| Node<T> toBeAdded = new Node<T>( data, count ); | |
| if( head == null ) | |
| { | |
| head = toBeAdded; | |
| tail = toBeAdded; | |
| } | |
| else | |
| { | |
| tail.Next = toBeAdded; | |
| tail = toBeAdded; | |
| } | |
| count++; | |
| } | |
| /// <summary> | |
| /// Removes the front element from the queue | |
| /// </summary> | |
| /// <returns>The front element</returns> | |
| public T Dequeue() | |
| { | |
| Node<T> temp; | |
| if( head == null ) | |
| { | |
| throw new Exception( "Queue is empty!" ); | |
| } | |
| else | |
| { | |
| temp = head; | |
| head = head.Next; | |
| } | |
| count--; | |
| return temp.Data; | |
| } | |
| #endregion | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment