• Post Reply Bookmark Topic Watch Topic
  • New Topic

coding a doubly linked list  RSS feed

 
John Diesal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really new to Java. I'm trying to write methods of a class of a doubly linked list, but not implement it ( its for class). I'm trying to insert element to appear between prior and followers, but I'm not sure how to write the code for the addBetween and addFront methods. Most importantly, I don't know where to start. Do I use recursion ( and if so, how?) to insert elem in the middle of prior and follower for addBetween? Do I replace the head node for addFront?
I'm not asking for the answer, of course, but I would like to know where to start on both methods in order to complete them.


 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where to start. 

I recommend taking paper and pencil and drawing a diagram for each case and note on the diagram which pointers need to be changed to implement the case.
Also note the order that the pointers need to be changed to preserve their values.
 
John Diesal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Norm Radder

That's the problem. I'm not quite sure how to visualize the linkedlist working. Maybe its my brain, but I have a hard time seeing all the working parts of the linkedlist. I just need a pointer (hehe) on the path to figure it out.
 
William Barnes
Ranch Hand
Posts: 1050
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> how to visualize

    Node  --->   Node  --->    Node
    One   <---     Two  <---     Three

https://en.wikipedia.org/wiki/Doubly_linked_list

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to do some research on how a  doubly linked list works before you can design it.

A diagram would include rectangles for the nodes and connecting lines that represent the links between the nodes.
First draw a DLL with 4 nodes and the links connecting them horizontally across the paper
Then draw a node below and between two of the above nodes.
Then change the lines so the new node is properly connected.
 
William Barnes
Ranch Hand
Posts: 1050
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Do I use recursion ( and if so, how?) to insert elem in the middle of prior and follower for addBetween?
I wouldn't.  I am sure it could be done.  But keep it as simple as possible.

> Do I replace the head node for addFront?
I would have to see the assignment, it should give that level of detail.  If not tell the instructor you don't have enough requirements to complete the project.  In a nice way.  Most likely yes. 
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Follow the Wikipedia link you were given and see how much help you get from that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!