Cyclic Queue : Queue « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » QueueScreenshots 
Cyclic Queue
        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Lilium.Collections
{
  public class CyclicQueue<T>: IEnumerable<T>
  {
    public CyclicQueue(int length)
    {
      if (length < 0)
        throw new InvalidOperationException("Queue length can't be less then zero");

      m_Queue = new T[length];
    }

    public void Enqueue(T item)
    {   
      var length = Queue.Length ;
      if (length == 0)
        return;
      
      Queue[Tail= item;
      Tail = (Tail + 1% length;
      if (Count == length)
        Head = (Head + 1% length;
      else
        Count++;
    }

    public T Dequeue()
    {
      if (Count == 0)
        throw new InvalidOperationException("Queue is empty");

      T local = Queue[Head];
      Queue[Headdefault(T);
      Head = (Head + 1% Queue.Length;
      return local;
    }


    /// <summary>
    /// Returns enumerator which enumerates from queue tail to the head.
    /// </summary>
    /// <returns></returns>
    public IEnumerator<T> GetReversedEnumerator()
    {
      var length = Count;
      if (length == 0)
        yield break;

      if (Tail > Head)
        for (int i = Tail - 1; i >= Head; i--)
          yield return Queue[i];
      else
      {
        for (int i = Tail - 1; i >= 0; i--)
          yield return Queue[i];
        for (int i = length - 1; i >= Head; i--)
          yield return Queue[i];
      }
    }

    public bool TryPeekLast(out T item)
    {
      if (Count != 0)
      {
        item = Queue[Tail != ? Tail - : Queue.Length - 1];
        return true;
      }
      else
      {
        item = default(T);
        return false;
      }
    }

    #region public int Count

    private int m_Count;

    public int Count
    {
      get
      {
        return m_Count;
      }
      private set
      {
        m_Count = value;
      }
    }

    #endregion

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
      var length = Queue.Length;
      if (length == 0)
        yield break;

      if (Tail > Head)
        for (int i = Head; i < Tail; i++)
          yield return Queue[i];
      else
      {
        for (int i = Head; i < length; i++)
          yield return Queue[i];
        for (int i = 0; i < Tail - 1; i++)
          yield return Queue[i];
      }
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
      return GetEnumerator();
    }

    #endregion

    #region private T[] Queue

    private readonly T[] m_Queue;

    private T[] Queue
    {
      get
      {
        return m_Queue;
      }
    }

    #endregion

    #region private int Head

    private int m_Head;

    private int Head
    {
      get
      {
        return m_Head;
      }
      set
      {
        m_Head = value;
      }
    }

    #endregion

    #region private int Tail

    private int m_Tail;

    private int Tail
    {
      get
      {
        return m_Tail;
      }
      set
      {
        m_Tail = value;
      }
    }

    #endregion
  }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.Put elements into a queue
2.Put user-defined objects to Queue collection
3.Implements the queue data type using an arrayImplements the queue data type using an array
4.A queue class for charactersA queue class for characters
5.illustrates the use of a Queueillustrates the use of a Queue
6.Queue testQueue test
7.Add exception handling to the queue classesAdd exception handling to the queue classes
8.Demonstrate the Queue classDemonstrate the Queue class
9.Priority Queue
10.Queue(T) Class represents a first-in, first-out collection of objects.
11.Priority Queue (2)
12.Dequeue
13.Implements a non-locking queue
14.Task queue
15.Syncronized Queue
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.