java - Issues with reversing objects in a LinkedList -
i'm writing code assignment requires method reverses elements in linkedlist, given portion of list reverse. example, if user enters 3 method reverse first 3 elements in array. i've written code instead of reversing code replaces 2nd element element present in first index. issue seems reversefirstsome method. i'm not asking write code me, pointer in right direction appreciated. here full code class:
import java.util.nosuchelementexception; public class linkedlist { //nested class represent node private class node { public object data; public node next; } //only instance variable points first node. private node first; // constructs empty linked list. public linkedlist() { first = null; } // returns first element in linked list. public object getfirst() { if (first == null) { nosuchelementexception ex = new nosuchelementexception(); throw ex; } else return first.data; } // removes first element in linked list. public object removefirst() { if (first == null) { nosuchelementexception ex = new nosuchelementexception(); throw ex; } else { object element = first.data; first = first.next; //change reference since it's removed. return element; } } // adds element front of linked list. public void addfirst(object element) { //create new node node newnode = new node(); newnode.data = element; newnode.next = first; //change first reference new node. first = newnode; } // returns iterator iterating through list. public listiterator listiterator() { return new linkedlistiterator(); } public string tostring() { linkedlistiterator iterator = new linkedlistiterator(); string result = "{ "; while (iterator.hasnext()) result += (iterator.next() + " "); result += "}\n"; return result; } public int size(){ linkedlistiterator iterator = new linkedlistiterator(); int = 0; while (iterator.hasnext()){ iterator.next(); a++; } return a; } public void addelement(object obj, int index){ if (index < 0 || index > size() ){ indexoutofboundsexception ex = new indexoutofboundsexception(); throw ex; } linkedlistiterator iterator = new linkedlistiterator(); for(int = 0; < index; i++){ if (iterator.hasnext()){ iterator.next(); }else{ nosuchelementexception ex = new nosuchelementexception(); throw ex; } } if (iterator.hasnext()){ object = iterator.next(); iterator.set(obj); while(iterator.hasnext()){ object b = iterator.next(); iterator.set(a); = b; } iterator.add(a); } else iterator.add(obj); } public object getelement(int index) { linkedlistiterator = new linkedlistiterator(); for(int = 0; < index; i++) {it.next();} return it.next(); } public object removeelement(int index) { if(index<0) {nosuchelementexception ex = new nosuchelementexception(); throw ex;} if(index>size()-1) {nosuchelementexception ex = new nosuchelementexception(); throw ex;} object result = null; linkedlistiterator = new linkedlistiterator(); result = getelement(index); if(index<size()-1) { for(int = 0; i<index+1; i++) it.next(); while(index<size()-2) { it.set(getelement(index+1)); it.next(); index++; } it.remove(); } else { for(int = 0; i<index+1; i++) it.next(); it.remove(); } return result; } public string findsmallest() { linkedlistiterator iterator = new linkedlistiterator(); if (!iterator.hasnext()){ return null; } string smallest = (string) getfirst(); while (iterator.hasnext()) { if (smallest.comparetoignorecase((string) iterator.next()) > 1) { smallest = (string) iterator.next(); } } return smallest; } public void searchandreplace(object first, object second){ linkedlistiterator iterator = new linkedlistiterator(); while(iterator.hasnext()){ if(iterator.next().equals(first)){ iterator.set(second); } } } public void searchandremove(object toberemoved){ linkedlistiterator iterator = new linkedlistiterator(); int = 0; while(iterator.hasnext()){ if(iterator.next().equals(toberemoved)){ removeelement(a); } a++; } } public void reversefirstsome(int howmany){ linkedlistiterator = new linkedlistiterator(); if(size() > 1){ int top = howmany - 1; int bot = 0; it.next(); do{ linkedlistiterator it1 = new linkedlistiterator(); object 1 = getelement(bot); object 2 = getelement(top); it.set(two); it.next(); for(int = 0; < top + 1; i++){ it1.next(); it1.set(one); bot++; top--; } }while(top > (size()/2) - 1); } } //nested class define iterator private class linkedlistiterator implements listiterator { private node position; //current position private node previous; //it used remove() method // constructs iterator points front // of linked list. public linkedlistiterator() { position = null; previous = null; } // tests if there element after iterator position. public boolean hasnext() { if (position == null) //not traversed yet { if (first != null) return true; else return false; } else { if (position.next != null) return true; else return false; } } // moves iterator past next element, , returns // traversed element's data. public object next() { if (!hasnext()) { nosuchelementexception ex = new nosuchelementexception(); throw ex; } else { previous = position; // remember remove if (position == null) position = first; else position = position.next; return position.data; } } // adds element after iterator position // , moves iterator past inserted element. public void add(object element) { if (position == null) //never traversed yet { addfirst(element); position = first; } else { //making new node add node newnode = new node(); newnode.data = element; newnode.next = position.next; //change link insert new node position.next = newnode; //move position forward new node position = newnode; } //this means cannot call remove() right after add() previous = position; } // removes last traversed element. method may // called after call next() method. public void remove() { if (previous == position) //not after next() called { illegalstateexception ex = new illegalstateexception(); throw ex; } else { if (position == first) { removefirst(); } else { previous.next = position.next; //removing } //stepping //this means remove() cannot called twice in row. position = previous; } } // sets last traversed element different value. public void set(object element) { if (position == null) { nosuchelementexception ex = new nosuchelementexception(); throw ex; } else position.data = element; } } //end of linkedlistiterator class
} //end of linkedlist class
i can think of implementation of appending standard logic of reversing list.
if pass index greater number of elements in list, reverses entire list.
if pass 0 or 1, list unaffected.
public boolean reversetillindex(int index) { int count = 0; if (index == 0) { return false; } node endcountnode = head; while (count++ < index && endcountnode != null) { endcountnode = endcountnode.next; } count = 0; // standard reverse list code node current = head; node h2 = null; while (current != null && count++ < index) { head = current.next; current.next = h2; h2 = current; current = head; } head = h2; while (h2.next != null) { h2 = h2.next; } h2.next = endcountnode; return true; }
Comments
Post a Comment