Skip to content

Instantly share code, notes, and snippets.

@dislogical
Created March 13, 2012 01:20
Show Gist options
  • Select an option

  • Save dislogical/2026003 to your computer and use it in GitHub Desktop.

Select an option

Save dislogical/2026003 to your computer and use it in GitHub Desktop.
Queue Data Structure: FILO
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