Click here to Skip to main content
6,634,665 members and growing! (17,482 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Data Structures     Intermediate

A Dequeue - also called a ring-buffer

By BenDi

Another addition to the System.Collections - a ring buffer, more sophisticated than ArrayList or Queue.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:29 Mar 2004
Views:40,868
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 2.18 Rating: 1.60 out of 5
16 votes, 69.6%
1
3 votes, 13.0%
2
1 vote, 4.3%
3
1 vote, 4.3%
4
2 votes, 8.7%
5

Introduction

Dequeue means 'double-ended-queue'. All who remember the good old STL days will know exactly what it is. For all the others: it's a System.Collections-compatible class implementing a queue that supports Enqueue and Dequeue at both ends.

Usage

The main functions (in addition to all the System.Collections-brabble) are:

  • EnqueueHead, EnqueueTail for adding to the DQ
  • DequeueHead, DequeueTail for taking from the DQ
  • EnqueueHeadRange, EnqueueTailRange for adding a collection to the DQ (the order is preserved)

The DQ allows enumerations and indexed access. For that, you need to know that the 'head' of the DQ is always the element with index 0, the tail is always the element with the index (Count-1). Enumeration is in that order as well.

EnqueueHead/Tail- and DequeueHead/Tail-operations are O(1)-complex, except when the buffer has to be grown, which will take O(n) steps (naturally). So you can see that the buffer is faster than an ArrayList and more flexible than a Queue.

Example

Dequeue D = new Dequeue();
D.EnqueueTail("The");
D.EnqueueTail("big");
D.EnqueueTail("brown");
D.EnqueueTail("fox");
// now D is [The big brown fox ]
Dequeue D2 = new Dequeue();
D2.EnqueueHead("dog.");
D2.EnqueueHead("lazy");
D2.EnqueueHead("the");
D2.EnqueueHead("over");
D2.EnqueueHead("jumped");
// now D2 is [jumped over the lazy dog. ]
Dequeue D3 = new Dequeue();
D3.EnqueueTailRange(D2);
D3.EnqueueHeadRange(D);
// now D3 is [The big brown fox jumped over the lazy dog. ]

History

  • 30.3.04 - Inserted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

BenDi


Member
I did my diploma in Dresden and Sydney where I dealt with algorithms, agents and other cool AI stuff. Now I moved to Frankfurt to work on my PhD dealing with software structures for artificial intelligence systems. If I can, I do things in C# and ASP.NET, but if I have to, my C++, Java and SQL are not that bad.
Long Live .NET.
Occupation: Web Developer
Location: Germany Germany

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Generalbug found PinmemberGuggsdu1:01 13 Sep '04  
Generalnot a ring buffer Pinmemberwyldeling10:19 30 Mar '04  
GeneralRe: not a ring buffer PinmemberBenDi10:57 30 Mar '04  
GeneralRe: not a ring buffer PinsussAnonymous17:21 18 Jun '04  
GeneralRe: not a ring buffer Pinmemberswampmonster15:01 25 Jul '08  
wyldeling is right about the name, it's called std::deque, yes.

Anonymous wrote:
Furthermore, there's nothing circular about about the stl deque. It simply allocates objects using an underlying linked list of pages.


Now see that's where you're completely wrong.
I would like to see a conforming implementation of a std::deque that uses a linked list of pages. std::deque has to implement at() and operator [], and as the standard dictates they have to run in constant time. Meaning you cannot walk a list of pages to get to the required element.

In one widely used implementation std::deque is implemented as an array of pointers to blocks (which hold 1 to 16 elements and which you call pages). So take your own advice and check up on your "facts" (which happen to be WRONG!) before you publish them.
Sign In·View Thread·PermaLink5.00/5 (1 vote)

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Mar 2004
Editor: Smitha Vijayan
Copyright 2004 by BenDi
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project