java - Class for Doubly-Linked List -
i have code class handling doubly-linked list doesn't have empty head or tail node. have modified code obtained supposed doubly-linked list throwing following exception. how prevent exception occurring , changes required remove empty head or tail node?
exception in thread "main" java.lang.nullpointerexception @ p4dll.removelast(p4dll.java:105) @ p4dll.main(p4dll.java:197)
code:
class listnode { object element; listnode next; listnode prev; public listnode( object anelement ) { element = anelement; next = null; prev = null; } public listnode( object anelement, listnode nextptr, listnode prevptr) { element = anelement; next = nextptr; prev = prevptr; } } // end class listnode public class p4dll { private listnode head; private listnode tail; private int size; public p4dll() { head = null; tail = null; size = 0; } public void addatfirst( object anelement ) { //create new node stored @ beginning of list listnode newnode = new listnode( anelement); newnode.next = head; head = newnode; size++; if(tail == null) tail = head; } public void addatlast( object anelement ) { //create new node stored @ begginning of list listnode newnode = new listnode( anelement); if(tail == null) { head = tail = newnode; } else { tail.next = newnode; tail = tail.next; } size++; } public object removefirst( ) { //the list has no elements remove if ( isempty( ) ) { return null; } //get ptr first data node listnode ptr = head.next; //save data in node can returned object data = head.next.element; //link around node ptr.next.prev = ptr.prev; ptr.prev.next = ptr.next; size--; //return data in node return data; } public object removelast( ) { //if list has no elements there nothing return if ( isempty( ) ) { return null; } //get ptr last data node listnode ptr = tail.prev; //save data in node can returned object data = tail.element; //link around node ptr.next.prev = ptr.prev; // p4dll.java:105 ptr.prev.next = ptr.next; size--; //return data in node return data; } public object getfirstelement( ) { if ( size == 0 ) { return null; } else { return head.next.element; } } public object getlastelement( ) { if ( size == 0 ) return null; else return tail.prev.element; } public int getnumelements( ) { return size; } public boolean isempty( ) { return size == 0; } public void displaylist( ) { if ( isempty( ) ) { system.out.println ( "the list empty.\n" ); } else { system.out.println ( this.tostring( ) ); } } public string tostring( ) { string returnstr = ""; if ( ! isempty( ) ) { listnode ptr = head.next; while ( ptr != tail ) { returnstr += ptr.element.tostring (); returnstr += "\n"; ptr = ptr.next; } } return returnstr; } // makes list empty public void clear( ) { if ( ! isempty( ) ) { head.next = tail; tail.prev = head; size = 0; } } public static void main ( string[] args ) { p4dll list = new p4dll( ); list.addatfirst ( "abe" ); list.addatfirst ( "beth"); list.addatlast ( "ed" ); list.displaylist (); system.out.println( "the number of elements in list " + list.getnumelements() ); system.out.println( "\nnow remove last element , display.\n" ); list.removelast(); // p4dll.java:197 list.displaylist (); } }
you code producing nullpointerexception
inside removelast()
method. fix it! :-d
it seems copied code somewhere, including spelling mistakes:
http://www.cramster.com/answers-jul-09/computer-science/doubly-linked-list-xi5te-program-implements-class-doubly-linked_619688.aspx
the code written assuming head , tail nodes. you'll need re-write method (and others) account new reality.
edit:
i finished reviewing diff between code , code found in above link. suggestion scrap , write linked list scratch. not written assuming head , tail nodes, it's broken in @ least 1 way. you've noticed since size--
missing supplied removelast()
, had add yourself. there's issue of comments talking "pointers", , spelling mistakes, , etc.
here's starting point. working implementation of removelast()
:
public object removelast( ) { if (size == 0) return null; final object result = last.element; if (size == 1) first = last = null; else { last = last.prev; last.next = null; } size--; return result; }
since you're not using head or tail nodes, i'm using fields called first
, last
better clarity.
Comments
Post a Comment