java - LinkedList to store LinkedList of Nodes -
i have written linkedlist class accepts nodes stores integers.
i have created linkedlist stack = new linkedlist(), , have added nodes if data of nodes less data of nodes exist in stack.
if not, want put old stack new linkedlist called linkedlist pilesofstacks, , create newstack called linkedlist newstack = new linkedlist(), , add larger node newstack go linkedlist pilesofstacks.
my question is; since created linkedlist class accept nodes, how make new linkedlist accept linkedlists of these nodes, creating different piles of linkedlists in linkedlist?
this have far:
public void sort(linkedlist listofints) { linkedlist<linkedlist> piles = new linkedlist<linkedlist>(); linkedlist stack = new linkedlist(); for(int = 0; < listofints.getsize(); i++) { node x = listofints.pop(); for(int j = 0; j < piles.getsize(); j++) { node y = piles.peek(); //check first element of each pile if( ( ((comparable)y.getdata()).compareto(x.getdata()) ) <= 0) { stack.push(x); break; } } stack.push(x); //put value in stack piles.add(stack); } } edit: if use array, create double array of effect node[][] array = new node[20][20]; , search node[i][0], since can use linkedlist, i'm wondering how this?
okay, gave shot - using java collection framework mentioned kami , roman c. avoid confusion used full qualified name of interfaces/classes involved - even though makes code big , ugly.
i used java.util.linkedlist implements java.util.list java.util.deque interface. latter 1 gives methods treat stack.
i assume method name, want sort nodes in stack. had change parts of original example, since seemed not behave described.
i ended following variation of example:
public void sort(java.util.deque<node> stackofintnodes) { java.util.list<java.util.deque<node>> piles = new java.util.linkedlist<java.util.deque<node>>(); java.util.deque<node> currentstack = new java.util.linkedlist<node>(); inputloop : while (!stackofintnodes.isempty()) { node currentnode = stackofintnodes.pop(); (java.util.deque<node> singlepile : piles) { // check first element of each pile node smallestnodeinsinglepile = singlepile.peek(); object valueofsmallestnodeinsinglepile = smallestnodeinsinglepile.getdata(); if ((((java.lang.comparable) valueofsmallestnodeinsinglepile) .compareto(currentnode.getdata())) <= 0) { singlepile.push(currentnode); continue inputloop; } } piles.add(currentstack); currentstack = new java.util.linkedlist<node>(); currentstack.push(currentnode); // put value in stack } piles.add(currentstack); java.util.deque<node> sortedstackofintnodes = new java.util.linkedlist<node>(); (java.util.deque<node> singlepile : piles) { while (!singlepile.isempty()) { sortedstackofintnodes.push(singlepile.pop()); } } // result: got node elements in sorted order } but if using java.util.linkedlist instead of own implementation, use equivalent method:
public void sort(java.util.deque<node> stackofintnodes) { java.util.linkedlist<node> sortedlistofintnodes = new java.util.linkedlist<node>(stackofintnodes); java.util.collections.sort(sortedlistofintnodes, new comparator<node>() { @override public int compare(node nodeone, node nodetwo) { return ((java.lang.comparable) nodeone.getdata()) .compareto(nodetwo.getdata()); } }); // result: got node elements in sorted order } depending on actual node class and/or own linkedlist, might have apply further changes here.
Comments
Post a Comment