Doubly Linked List Java -
i'm having confusion creating dllist in java. instructions dllnode class should have links point previous , next nodes. need create under data section or method?
thank time
you can start declaring ddlist
this:
public class dllist { private static class node { int data; node previous; node next; node(int d) { data = d; } } private node head; private node tail; // ... }
some highlights:
- the
node
class inner class ofddlist
, member. declared in same section other members, e.g. fields. call "data section" believe. - you can declare
head
,tail
beforenode
. - thanks static,
node
not have referenceddlist
. saves (only) 4 bytes pernode
can avoid memory leaks if pass unlinkednodes
outsideddlist
. better style ihmo. - thanks private,
node
not visible outsidedllist
. since need access class access fields, similar settingnode
's fields private. more efficient because compiler doesn't have generate synthetic methods if accessnode
's fields indllist
. - consider making
data
final, cannot change. - consider making
ddlist
generic. cool exercise left reader :-)
Comments
Post a Comment