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 nodeclass inner class ofddlist, member. declared in same section other members, e.g. fields. call "data section" believe.
- you can declare head,tailbeforenode.
- thanks static, nodenot have referenceddlist. saves (only) 4 bytes pernodecan avoid memory leaks if pass unlinkednodesoutsideddlist. better style ihmo.
- thanks private, nodenot 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 datafinal, cannot change.
- consider making ddlistgeneric. cool exercise left reader :-)
Comments
Post a Comment