Click here to Skip to main content
Licence CPOL
First Posted 26 Nov 2006
Views 30,032
Downloads 214
Bookmarked 16 times

Circular Queue Data Structure in C#

By | 26 Nov 2006 | Article
Implementation of a circular queue data structure in C#.

Introduction

I recently had a requirement of using a queue data structure whose length was finite due to memory constraints, and hence I preferred to adopt the circular queue data structure. When I searched for a circular queue over the internet, I could not find much, so I decided to write this article.

A circular queue follows the basic rules of a queue data structure. It has the first in first out mechanism, but its size is restricted. We just need to keep track of two terms to understand, implement, and use a circular queue; they are:

  1. Front position (from where you can dequeue)
  2. Rear position (the last element's position in the queue)

I have implemented this in C# using the ArrayList data type.

The two methods that we expose are Enqueue and Dequeue (the traditional queue methods). I have also added a ResetQueue method to reset the queue to its initial state. The main requirement for this type of a data structure is to implement a restricted number of place holders which can hold an instance of the object that is inserted in to the queue.

The Code

Initializing

In the constructor, our circular queue class will accept an integer parameter to set its MaxQueueSize property. The whole queue implementation is based on the maximum queue size. In any typical application scenario, this maximum size needs to be selected based on the amount of memory an object instance will take and how much memory can be allotted for our queue. Initially, FrontPosition is set to zero and RearPosition to -1.

Enqueue()

If the queue has any vacant position, the Enqueue method will add an element (object) to the circular queue at RearPosition+1. If there is no vacant position, the method will return false.

public bool Enqueue(object obj)
{
    if((nRearPosition == (nMaxSize-1) && nFrontPosition==0) ||
             ((nRearPosition!=-1)&&(nRearPosition+1)==nFrontPosition))
        return false;
    if(nRearPosition == (nMaxSize -1) && nFrontPosition > 0)
       nRearPosition = -1;
    nRearPosition+=1;
    alstQueueContent[nRearPosition] = obj;
    if((nRearPosition-1) == nFrontPosition && 
               alstQueueContent[nFrontPosition]==null)
       nFrontPosition = nFrontPosition+1;
    return true;
}

Dequeue()

If the queue has any element at the position pointed by FrontPosition, the Dequeue method will remove the element (object) from the circular queue and increment FrontPosition. When FrontPosition equals MaxSize -1, then after the dequeue operation, FrontPosition must be set to 0 instead of incrementing.

public object Dequeue()
{
    object Output = "Empty" ;
    if(alstQueueContent[nFrontPosition] != null)
    {
        Output = alstQueueContent[nFrontPosition];
        alstQueueContent[nFrontPosition]=null;
        if((nFrontPosition+1)<nMaxSize && 
                alstQueueContent[nFrontPosition+1] !=null)
            nFrontPosition += 1;
        else if(alstQueueContent[0] !=null && (nFrontPosition+1)== nMaxSize)
            nFrontPosition = 0;
    }
    return Output; 
}

The complete implementation is available ias a downloadable attachment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

karam chandrabose

Web Developer

India India

Member

Karam Chandra Bose was a Component developer at A UK Based MNC, most of his work involve pure c# programming, and he has been doing it since june 2004. apart from coding, he likes to paint, catch mosquitoes. He has moderate knowledgen in COM,ATL .
 
Currently he is a consultant in MS , specialising in IPTV

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhi Pinmemberemma33218:33 30 Mar '12  
GeneralQueue PinmemberMember 37719190:45 15 Mar '10  
Generalfor mr.karam chandrabose Pinmemberkittaneh9:45 9 Feb '09  
QuestionYour code does not work Pinmemberjhallissey23:45 26 Nov '07  
AnswerRe: Your code does not work Pinmemberkaram chandra bose3:43 8 Jul '08  
GeneralAlso called a ring buffer.... PinmemberKeith Vinson8:13 4 Dec '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 27 Nov 2006
Article Copyright 2006 by karam chandrabose
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid