• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double-ended queue java source code or c++ source code  RSS feed

 
jing hu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to look at double-ended queue implementation.
Any one who know the double-ended queue implementation can give the source code or the link.
I understand the double-ended queue. I just want to look at the source code. I want to to know how the complete double-ended queue is designed.
Thanks for any help.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a java.util.Deque interface, which has a few implementations. You could check the sources, but the JDK implementation is probably not as simple as you could imagine.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find the source code for classes in the JDK, such as java.util.Deque, in the file src.zip which is in your JDK installation directory.
 
jing hu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Thanks.
the java.util.Queue may be too complicated for me. I just want to know some necessary member for Dequeue.
So I post my Dequeue.java source code .
Thanks for any suggestions for the below Dequeue.java code.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to make the method names correspond to the names in the Deque interface. The names push and pop are usually applied to stacks, not queues, although you find the Deque interface can be used as a stack.
You have a class with instance fields in; you need to initialise that in the constructor. Then you can forget about the != null tests. If the array is null, then it is impossible for your class to perform any functionality at all. Your code to increase the size of the array is duplicated; it should be moved into a private method for resizing the capacity.
Your pop methods should throw an EmptyStackException when you have a size of 0, rather than returning -1. What happens if you push -1 onto your stack?
Your pop methods leave an unused reference on the stack. They should be implemented more like thisThis is more important with reference types; you need to set them to null, not 0, otherwise there is a risk of a memory leak.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Your pop methods should throw an EmptyStackException when you have a size of 0, rather than returning -1.

That should be java.util.NoSuchElementException
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, all right then, NoSuchElement.
 
jing hu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!