Jump to content

Double ended queue

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
lmc059

lmc059

    Newbie

  • Members
  • PipPip
  • 16 posts
Hi sorry i keep asking for help on the most simple things but i am having trouble learning java while learning web design etc. anyway has anyone written a double ended queue that they could post. i have written queues and stacks in C# but i find java is harder to get used to please help:confused:

thanks again

#2
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
Lets define Deque:

Quote

In computer science, a deque (short for double-ended queue) is an abstract data structure for which elements can be added to or removed from the front or back. This differs from a normal queue, where elements can only be added to one end and removed from the other. Both queues and stacks can be considered specializations of deques, and can be implemented using deques.

Rather than make your own, use Java's Vector class.
a Vector Object is like an array, with the exception that its size can be increased, or decreased during program runtime.
NOTE - the vector class is located in the java.util package

Here is the documentation of the vector class
Java 2 Platform SE v1.3.1: Class Vector

And a quick sample:

[highlight="Java5"]import java.util.Vector;

public class VectorSample {
public static void main (String[] arg){
Vector<String> stringList = new Vector<String>();
// Create an empty vector object of the type String

stringList.addElement("Red");
stringList.addElement("Green");
stringList.addElement("Blue");
stringList.addElement("Yellow");
StringList.addElement("Pink");
// Populate the list from element 0 to 4
// Note the size of the Vector is 5

stringList.addElement("Black", 0);
// Add a new element at the beginning
// Note, now the Vector is populated from 0 to 5
// All the other elements got "Pushed"
// And the new size of the Vector is 6

stringList.addElement("White", stringList.size());
// Add a new element at the end
// Note - public int size() returns the size of the Vector (6)
// Now the vector is populated from 0 to 6
// And the new size of the vector is 7
}
}[/highlight]


Upon a little more research, i found this
Deque (Java Platform SE 6)

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#3
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
Isn't there a deque in the standard library?

//edit - yes since 1.6 Deque (Java Platform SE 6) //

//edit2 - should have read the second post.//