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
Double ended queue
Started by lmc059, Nov 28 2007 10:15 AM
2 replies to this topic
#1
Posted 28 November 2007 - 10:15 AM
|
|
|
#2
Posted 29 November 2007 - 07:12 AM
Lets define Deque:
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)
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
#3
Posted 29 November 2007 - 09:58 AM
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.//
//edit - yes since 1.6 Deque (Java Platform SE 6) //
//edit2 - should have read the second post.//


Sign In
Create Account


Back to top









