• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addAfter method DoublyLinkedList trouble

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been trying to work on this method for about 4 days.  everything in my code is fine but the addAfter method.. I am super stumped on it, even when i draw it out.



 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for using the code tags, but you didn't quite get them right (see this link): don't worry, I can correct that for you since you are new.

Remember the structure of a linked list.

Current node ≡ nextNode.prev

and

nextNode ≡ Current node.next

So we assume you already have the appropriate place marked by having a ”live” reference to the current node. What you need to do is add a new node such that:-
  • Current node.next ≡ newNode
  • newNode.next ≡ (old)nextNode
  • newNode.prev ≡ Current node
  • (old)nextNode.prev ≡ newNode
  • Don't try methods out in a large class. Create a small class that has the requisite features and try it out in that first. You can do that with two old nodes and one new node to insert between them. Once you have done that, you can copy the technique into your larger class.
    I don't think what you are doing is going to work. Find the place to insert a new value before you create the new node. The new node should point to its two neighbouring nodes, not the end nodes. Then you can make the neighbouring modes point to the new node. Remember that if you get to the end node, its next field will point to null, so you cannot find a prev field to point to the new node.
    I think maybe you should make the node class private.

    All the above applies to an addBefore method, except that you look for end nodes the other way and prev might point to null.
     
    tyler ruterbusch
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    SO i changed up a few things. Now my reverseList is busted. I am having a hard time tracking it in my head so I know what to set to what. That is the hardest thing about linkedlists to me as a new java programmer.. .next can refer to like 5 positions
     
    tyler ruterbusch
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I've tried giving it a break statement
    like

    if(trailer == current.next)
    break;

    nothing
     
    tyler ruterbusch
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the help, but i got a whole new issue now ): aha

    Campbell Ritchie wrote:Welcome to the Ranch

    Thank you for using the code tags, but you didn't quite get them right (see this link): don't worry, I can correct that for you since you are new.

    Remember the structure of a linked list.

    Current node ≡ nextNode.prev

    and

    nextNode ≡ Current node.next

    So we assume you already have the appropriate place marked by having a ”live” reference to the current node. What you need to do is add a new node such that:-

  • Current node.next ≡ newNode
  • newNode.next ≡ (old)nextNode
  • newNode.prev ≡ Current node
  • (old)nextNode.prev ≡ newNode
  • Don't try methods out in a large class. Create a small class that has the requisite features and try it out in that first. You can do that with two old nodes and one new node to insert between them. Once you have done that, you can copy the technique into your larger class.
    I don't think what you are doing is going to work. Find the place to insert a new value before you create the new node. The new node should point to its two neighbouring nodes, not the end nodes. Then you can make the neighbouring modes point to the new node. Remember that if you get to the end node, its next field will point to null, so you cannot find a prev field to point to the new node.
    I think maybe you should make the node class private.

    All the above applies to an addBefore method, except that you look for end nodes the other way and prev might point to null.

     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    tyler ruterbusch wrote: Now my reverseList is busted.


    This is too broad.  Remember to TellTheDetails (that's a link) when describing a problem.  This is good for the forum and good as you continue to be a developer. Some questions to answer: What is the output?  What is the input?  What did you expect?
     
    reply
      Bookmark Topic Watch Topic
    • New Topic