Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Data Structures Double Linked List - moveUp and moveDown methods  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I am trying to write a Double Linked List code that just implements the different methods like addToFront, AddToRear, moveUp, and so on (it's suppose to be very simple). My list can be unordered or ordered since I will be doing an ascending and descending sort in my code. I am having trouble implementing the moveUp and moveDown method in java code. I only to move the element one space up towards the head or one space during the tail. I know that, for instance, if I want to moveUp, I need a temp to hold the the element being replaced by the element being moved. I was thinking I could store the element being replaced while incrementing or decrementing the element being moved and simply moving the element back.

I haven't coded in java in over a year so a lot of learned material has been forgotten and I am really struggling with the summer course I am taking now. I would appreciate any help or direction by anyone.

This is my doubleNode:

This is my Customlist:

And my main:
 
Maria Macalino
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! It looks much better. I will in the future
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking through your code... it looks like you posted the DoubleNode twice, instead of the list code. Can you post the list code (since that seems to be where the two methods you are having trouble with are) and also explain what trouble you are having with it (what is it doing wrong or what is it not doing)?
 
Maria Macalino
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Steve,

Thanks for looking over it and I can't believe I did that. I no longer have any code for the methods because it wasn't compiling and I wanted to start fresh. What I tried to do was a loop that searches for the element A that I wanted to move up and if found I set my pointer the the previous one and stored that element B in a temp and then tried to move the element A up by decrementing it's position on the list and then reinserting B after A. I got a bunch of errors I couldn't understand and I wish I did not delete my code before posting here. I do remember that I got a bunch of errors about ".previous" and ".next" saying that they had private access in DoubleNode... Sorry if that didn't help you much. I was just wondering if I was on the right track..


Does anyone know where I can look at some sample code on moveup or moveDown? or what steps I might need to take? Thank you.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you get to code, think about the steps you will need. This exercise works well on a white board or using sticky notes. Start with 4 cards with items, 3 labeled NEXT, and 3 labeled PREVIOUS. Put the items in a list, and use the NEXT/PREVIOUS cards to create your linked list. Then see what steps you need to move item #3 to #2, and from #2 to #3. Follow some simple rules that emulate how a computer works).
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Luke wrote:Hi Maria,
...I just helped format your code so it is easier to read.


@Maria: And I've removed most of those blank lines. It helps prevent CTS for old guys like me if we don't have to scroll too much.

Winston
 
Marshal
Posts: 58830
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maria Macalino wrote: . . . My list can be unordered or ordered . . .

No, it can’t. All lists are ordered, by definition.

What you are doing when you sort a List is imposing a new order on it.
By the way: you would do well to create an asArray() method and a constructor which takes an array as its parameter. The reason for that is performance: look here. Remember that anything you want to sort must support “natural ordering”, (which all primitives do, too), or must be comparable with a Comparator.
 
Maria Macalino
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for being so helpful!!
 
Campbell Ritchie
Marshal
Posts: 58830
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome Did you get it all to work? Show us how you implemented move up and move down.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!