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 of ddlist , member. declared in same section other members, e.g. fields. call "data section" believe.
  • you can declare head , tail before node.
  • thanks static, node not have reference ddlist. saves (only) 4 bytes per node can avoid memory leaks if pass unlinked nodes outside ddlist. better style ihmo.
  • thanks private, node not visible outside dllist. since need access class access fields, similar setting node's fields private. more efficient because compiler doesn't have generate synthetic methods if access node's fields in dllist.
  • consider making data final, cannot change.
  • consider making ddlist generic. cool exercise left reader :-)

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -