java - Generic Doubly Linked List -


for personal practice trying make basic, generic doubly linked list , want know if methods addtohead() , addtotail() made correct , efficient , if not better? , how remove list methods removingdataat(), removingfromtail(), removingfromhead()?

node class:

public class practicenode<t> {  private t data; private practicenode<t> next; private practicenode<t> prev;  practicenode() {     next = null;     prev = null;     data = null; }  pratcicenode(t data) {  this(data, null, null);  }  practicenode(t data, practicenode<t> next, practicenode<t> prev) {     this.data = data;     this.next = next;     this.prev = prev; }  public void setnextnode(practicenode<t> next) {     this.next = next; }  public void setprevnode(practicenode<t> prev) {     this.prev = prev; }  public void setdata(t data) {     this.data = data; }  public practicenode<t> getnextnode() {     return next; } public practicenode<t> getprevnode() {     return prev; }  public t getdata() {     return data; }  } 

linked list class:

public class practicelinkedlist<t> {  private practicenode<t> head; private practicenode<t> tail;  public void addtohead(t data ) {     practicenode<t> newnode=new practicenode<t>();     if(head==null){         head=newnode;         tail=newnode;         newnode.setnextnode(null);         newnode.setprevnode(null);     }else{          newnode.setnextnode(head);          head.setprevnode(newnode);          head=newnode;     }  }  public void addtotail(t data) {     practicenode<t> newnode=new practicenode<t>();      if(tail==null){             head=newnode;             tail=newnode;             newnode.setnextnode(null);             newnode.setprevnode(null);         }else{             newnode.setprevnode(tail);             tail.setnextnode(newnode);             tail=newnode;         }   }  public t removingdataat (int){     //.... }  public t removingfromtail (){     //.... }  public t removingfromhead (){     //.... } } 

addtotail looks me.

with removingdataat(), removingfromtail(), , removingfromhead(), want have done addtotail , addtohead. because seems assignment, not give completed code, tell how it.
see have navigating instances of head , tail, recommend implement 'current' allow navigate through list things such removingdataat(location). i'm not sure efficient method of removing things, java, garbage collection automatically remove head using head = head.getnextnode(). removing tail similar story.
removingdataat() need method find data data first, unless use knows in each location of list. perhaps find(object) return error message on fail , move 'current' instance found object otherwise. you'd implement using this:
for(current = head; current!=null; current = current.getnextnode())

remember check if there other items in linked list.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -