java - Merge Sort on Object Linked List (ascending order) -
i working on homework assignment , after days of effort cannot figure out why, after mergesort implemented, list contains last object in linked list. not output entire linked list, last object. how can change code stop list turning null
after 1 object.
please note: though call them cubes, know not since have random lengths, widths, , heights. assignment specifies called cubes, these random data fields. please ignore this.
public class sortingcubes { public static void main(string[] args) { system.out.println(" "); system.out.println("-----my linked list-----"); cube headcubell = new cube(); //create head cube int llnum = 5; //change number of linked list items desired here for(int = 0; < llnum; i++) { headcubell.length = math.random() * 99 + 1; //create random l,w,h each cube (between 1-100) headcubell.width = math.random() * 99 + 1; headcubell.height = math.random() * 99 + 1; headcubell.next = null; //sets end of list null if(headcubell.next == null) { //creates new cube until desired number reached in loop cube curr = new cube(headcubell.length, headcubell.width, headcubell.height); headcubell = curr; } headcubell.next = null; //sets last cube (next) null end list. system.out.println(headcubell.tostring() + " "); //print linked list until end } system.out.println(" "); system.out.println("-----new linked list after merge sort method implemented (asending order volume)-----"); long starttimemergesort = system.nanotime(); mergesort(headcubell); long timeelapsedmergesort = (system.nanotime()- starttimemergesort); printlist(headcubell); //method print linked list system.out.println("objects in list " + count(headcubell)); long starttimeinsertionsort = system.nanotime(); system.out.println(" "); system.out.println("time record: "); system.out.println("time elapsed merge sort on linked list: " + timeelapsedmergesort + " nanos"); } //end of main public static void printlist(cube headcubell){ while(headcubell != null){ system.out.println(headcubell.tostring() + " "); headcubell = headcubell.next; } system.out.println(); } public static int count(cube head){ int count = 0; while(head != null){ //system.out.println(head); count++; head = head.next; } return count; } public static cube mergesort(cube headcubell) { if(headcubell == null || headcubell.next == null) { //checking list null return headcubell; } int count = 0; //to count total number of elements cube temp = headcubell; //temporary head of list while(temp != null) { //break list 2 parts count++; //while not empty, count 1 , move temp temp.next evaluate temp = temp.next; } int middle = count/2; //create integer called middle , divide length of list (count) 2 cube = headcubell; //another temp head cube 1st split list cube b = null; //another cube null cube temp2 = headcubell; //create temp head cube 2nd list int counthalf = 0; //start @ half 0 again while(temp2 != null) { //while temp have 2nd list not null.... counthalf++; //add count length of linked list cube thenext = temp2.next; //create new cube assigned cube after head if(counthalf == middle) { //once reaches middle number temp2.next = null; //end list (set head.next null list 2) b = thenext; //take empty null cube , assign end null. } temp2 = thenext; //otherwise - move along , temp head temp head.next } //now have 2 parts, list , list b. cube half1 = mergesort(a); //recursively call sort each half cube half2 = mergesort(b); //merge cube merged = merge(half1, half2); //call 2nd method merge 2 halves //system.out.println(merged.tostring()); //print l,w,h , volume of each cube in array return merged; //return merged list. } public static cube merge(cube a, cube b) { //parameters 2 half's of original list cube pt1 = a; //2 parts going merge ascending lists according volume cube pt2 = b; cube temphead = new cube(); //temp head of new list cubes merged cube ptnew = temphead; //temp head list new list while(pt1 != null || pt2 != null) { //while either list not empty if(pt1 == null) { //but if first null ptnew.next = new cube(pt2.cubevolume()); //create new cube every cube volume pt2 = pt2.next; //loop } else if(pt2 ==null){ //but if 2nd null ptnew.next = new cube(pt1.cubevolume()); //create new cube every cube volume pt1 = pt1.next; //loop ptnew = ptnew.next; //move on assigning moving next cube creating when pt2 == null } else { if(pt1.cubevolume() < pt2.cubevolume()) { //moving while merging - comparing volumes of head of each list ptnew.next = new cube(pt1.cubevolume()); //when 1 greater new cube creating in new list , cube placed accordingly pt1 = pt1.next; //loop ptnew = ptnew.next; //loop through new merged list next comparison } else if(pt1.cubevolume() == pt2.cubevolume()) { //statement if cubes volumes equal ptnew.next = new cube(pt1.cubevolume()); ptnew.next.next = new cube(pt1.cubevolume()); ptnew = ptnew.next.next; pt1 = pt1.next; //place 1 next other pt2 = pt2.next; } else { ptnew.next = new cube(pt2.cubevolume()); //else made new cube in new list , place pt2 head pt2 = pt2.next; //loop ptnew = ptnew.next; //loop } } } return temphead.next; //return new merged list } } //end of class
my cube class: (all correct reference)
public class cube { double length; double width; double height; cube next = null; public cube() { //default constructor } public cube(double volume) { volume = this.cubevolume(); } public cube(double length, double width, double height) { this.length = length; this.width = width; this.height = height; this.next = next; } public string tostring() { //print string return "cube volume: (" + cubevolume() + ") ----- cube length: ("+ this.length +") ----- cube width (" + this.width + ") ----- cube height (" + this.height + ")"; } //set length public void setlength(double length) { this.length = length; } //get length public double getlength() { return this.length; } //set width public void setwidth(double width) { this.width = width; } //get width public double getwidth() { return this.width; } //set height public void setheight(double height) { this.height = height; } //set height public double setheight() { return this.height; } //set next public void setnext(cube next) { this.next = next; } //get next public cube getnext() { return this.next; } public double cubevolume () { double volume = (length*width*height); //system.out.println("test: " + volume); return volume; } } //end of class
output
-----my linked list----- cube volume: (14550.645379921463) ----- cube length: (19.526751823368887) ----- cube width (54.77537177724803) ----- cube height (13.604009167732666) cube volume: (1631.5144309742377) ----- cube length: (40.72878317573845) ----- cube width (22.07526604887876) ----- cube height (1.8146109949933575) cube volume: (17837.576670179817) ----- cube length: (4.606784797762423) ----- cube width (78.5447210731351) ----- cube height (49.29704940251802) cube volume: (113668.01972101796) ----- cube length: (24.366242383656253) ----- cube width (54.33809524521938) ----- cube height (85.85099307890556) cube volume: (432771.5800393206) ----- cube length: (83.95704403819472) ----- cube width (56.0616051224998) ----- cube height (91.94668276748753) -----new linked list after merge sort method implemented (asending order volume)----- cube volume: (432771.5800393206) ----- cube length: (83.95704403819472) ----- cube width (56.0616051224998) ----- cube height (91.94668276748753) objects in list 1 time record: time elapsed merge sort on linked list: 11831 nanos
the problem in main
method, not sorting methods. block
headcubell.next = null; //sets end of list null if(headcubell.next == null) { //creates new cube until desired number reached in loop cube curr = new cube(headcubell.length, headcubell.width, headcubell.height); headcubell = curr; } headcubell.next = null;
will go if
section. you're not setting next
field of curr
@ all. when assign curr
headcubell
, lose previous value of headcubell
. means there ever 1 object in list. throwing away object each time create new one.
you need
- remove final
headcubell.next = null;
- add new line
curr.setnext(headcubell);
after objectcurr
created.
that way, previous headcubell
remembered, next
element of new object created.
Comments
Post a Comment