• Post Reply Bookmark Topic Watch Topic
  • New Topic

DoublyEndedList not sure how is it working?  RSS feed

 
Anton Sotnikov
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!
In some course i found implementation of DoublyLinkedList.
in two words:
There are three classes Node.java DoublyEndedList.java DoublyEndedListDemo.java






result is:

Yet one
Node [date=10]
5



1. Method toString didn't work for all nodes only for one and i don't know why. Is anybody have some thoughts?
2.In this snippet i didn't understand in which case this.tail!=null ???

 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for asking if (tail != null) ... is to confirm that you are not dealing with an empty list. I cannot understand it and believe it may be incorrect. Why do you not have a constructor in those classes? Did you know you can make Node an inner class? Since a Node is part of a linked list it is a candidate for being an inner class.

Please space your code correctly.
  • 1: Spaces after if and round binary operators e.g. !=
  • 2: Blank line before the name of the method, not after the method name nor after {
  • We have some formatting suggestions here.

    Did you find that code (if so tell us where), or did you write it yourself? If you wrote it yourself, how did you design it? What concept do you have of how a doubly linked list works? Have you ever seen diagram of linked lists? Look here, or here where you will find diagrams which will probably help you.
     
    Anton Sotnikov
    Ranch Hand
    Posts: 59
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The reason for asking if (tail != null) ... is to confirm that you are not dealing with an empty list.


    Is it true that if we have declaration:


    it means that tail == null ??? and why if (tail != null) must to work ?



    I cannot understand it and believe it may be incorrect. Why do you not have a constructor in those classes? Did you know you can make Node an inner class? Since a Node is part of a linked list it is a candidate for being an inner class.

    Please space your code correctly.
    1: Spaces after if and round binary operators e.g. !=
    2: Blank line before the name of the method, not after the method name nor after {
    We have some formatting suggestions here.


    Yes i understood i will corect.


    Did you find that code (if so tell us where), or did you write it yourself? If you wrote it yourself, how did you design it? What concept do you have of how a doubly linked list works?

    I am not sure but i want to undertand how it works is add Node at tail, not into head.
     
    Campbell Ritchie
    Marshal
    Posts: 56553
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Only use code tags for code, not for quotes; I have changed your post.

    I think that method sets the new node to be the tail of the current tail node, then sets the current tail node. That will not work because the head node has no means of connection to the tail node.
    In fact, if you start with an empty list, I think you will add the same node twice.

    Go back to the diagrams and see how a linked list works. The important part is that nodes must be linked to each other. You don't appear to be linking anything to the head node. Remember this is what a list looks like:-Both head and tail in the List point to null and there are no nodes at all.Note that the single node has both its previous field ← and its next field → pointing to nullNote here there are still two nulls one at each end of the list, being previous for node1 and next for node2. And you can add node3.etc., etc.
     
    Campbell Ritchie
    Marshal
    Posts: 56553
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote: . . .
    In fact, if you start with an empty list, I think you will add the same node twice.
    . . .
    I was probably mistaken there. Sorry.
     
    Anton Sotnikov
    Ranch Hand
    Posts: 59
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Only use code tags for code, not for quotes; I have changed your post.

    I think that method sets the new node to be the tail of the current tail node, then sets the current tail node. That will not work because the head node has no means of connection to the tail node.
    In fact, if you start with an empty list, I think you will add the same node twice.

    Go back to the diagrams and see how a linked list works. The important part is that nodes must be linked to each other. You don't appear to be linking anything to the head node. Remember this is what a list looks like:-


    thanks i take into account your recomendations.
    and fixed this code:




    But without prev pointer only next Node.
     
    Campbell Ritchie
    Marshal
    Posts: 56553
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That appears to be a singly‑linked list now. Don't call it double ended. All lists have two ends except circular lists which have no ends.
    I think your if‑else starting line 12 is too complicated. You probably only want:-Apart from that it looks all right. Remember that if you write the list correctly
    head == null ⇔ tail == null
    …so you only need one test for nullity.
     
    Anton Sotnikov
    Ranch Hand
    Posts: 59
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I want to change first and last Node in this list. But method revers() didn't work.
    for my unfortunately i don't know why. Is any view why? thank you.


     
    Campbell Ritchie
    Marshal
    Posts: 56553
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ouch! That code is illegible because you didn't indent it correctly. So I tried copying it to Eclipse and using ctril‑A&#c2011;ctrl‑I. Now if I copy the code back, you get this:-
    Lines 21-22 should simply read else.
    Your indentation is inconsistent about whether you have spaces before { and about whether { goes on its own line.
    Never never use ==false or == true.
    There is something wrong about your using -2. If you had a proper doubly‑linked list you wouldn't need that; you could simply reverse the List by looking cor currentNode.previous.
    If you have to reverse a singly‑linked List, copy the entire contents of the List into an array, then copy the array back into the List (backwards). Look here to see where I got the idea.
    It should not be NextNode but nextNode. Fields don't start with capital letters. In the method where you set it use this.nextNode.
    I have already told you you should not need to check whether tail is null.
    Change the print() method to this… and put all the other logic in a toString method.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!