• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding to the front of a Deque (Double ended Queue)  RSS feed

 
Ronnie Bowyer
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

I am tasked with creating a Deque in java with options to add left, add right, remove left and remove right.

I have coded the add right and remove right methods successfully however I have issues trying to get add left and remove left working.

I think I have gone massively wrong somewhere. I have just tried swapping round the variables for add left and reversing the calculations which did not work and would just come up with the following :


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:421)
at java.lang.StringBuffer.append(StringBuffer.java:265)
at datastructuresass1.DendQueue.toString(DendQueue.java:107)
at datastructuresass1.main.main(main.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)


I am trying to create the deque using arrays. Below is my code for the add right and add left methods:


Add Left (which is not working)





Add Right (Which is working)



Please could someone explain to me why this addLeft method is not working. It would be a great help as I have been stumped on this for a while! Thanks in advance.

Ronnie








 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

The exception has nothing to do with your add methods. It's the toString() method that's failing.

I would strongly suggest you add methods to your class that perform common calculations, so you can build your add methods on top of them. Also, you may want to create a circular buffer class first that has a fixed length and performs the basic operations, and your double-ended queue then just swaps its internal buffer for a larger one when it's full:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!