1. Member
Join Date
Feb 2010
Location
Italy
Posts
4
Rep Power
0

## List Sorting method.

hi there.
i've started doing a java library with all the data structures necessary for future software i have in plan to do. i have some trouble with sorting a Linked List and i can't seem to understand where is the problem.

here's the code i've written. a big thanks if you can help, if not, thank you anyway :)

Java Code:
```public class LinkedList<U extends Comparable<U>> {
private Node head;          // this node points to the first element of the list.
private Node tail;          // this node points to the last element of the list.

/**
* The constructor of a linked list.
*
* @param element the first element of the list.
*/
}// end of constructor.

/**
* The constructor of an empty linked list.
*/
tail = null;
}// end of constructor.

/**
* A method that adds an element in the list, at the given position.
* If the given position is bigger than the number of elements of the list then the method simply adds the element in the end of the list.
*
* @param position the position in wich the elements must be added.
*/
int l = length();

else{

for(int i = 0; i < position - 1; i++)
t = t.next;

}// end of else.
}// end of method.

/**
* A method that creates a copy of the list, sorts it and returns it.
*
* @return a sorted copy of the original list.
*/
public void sort(){

if(l1.length() > 1) l1.sort();
if(l2.length() > 1) l2.sort();

int l = l1.length() + l2.length();

for(int i = 0; i < l; i++){
if(t1 != null && t2 != null){
if(t1.elem.compareTo(t2.elem) <= 0){
t1 = t1.next;
}// end of if.
else{
t2 = t2.next;
}// end of else.
}// end of if.
else if(t1 != null){
t1 = t1.next;
}// end of else-if.
else{
t2 = t2.next;
}// end of else.
}// end of cicle.
}// end of method.

/**
* A method that splits the linked list in two and returns the other list.
*
* @return the other half of the linked list.
*/
int l = length();

if(l > 1){

for(int i = 0; i < (l / 2) - 1; i++, n = n.next);

ris.tail = tail;
tail = n;
n.next = null;
}// end of if.

return ris;
}// end of method.

/**
* A method that calculates the length of a linked list.
*
* @return the length of the linked list.
*/
public int length(){
int ris = 0;

for(Node tmp = head; tmp != null; tmp = tmp.next) ris++;

return ris;
}// end of method.

/* other methods are in here, but they are tested and are ok */

/**
* The inner class of the linked list.
* This class creates and manipulates the nodes of the linked list.
*/
private class Node{
U elem;
Node next;

/**
* The constructor of the node of the linked list.
*
* @param el the element that the node must contain.
* @param n the pointer to the next node.
*/
Node(U el, Node n){
elem = el;
next = n;
}// end of constructor.

/**
* The constructor of the node of the linked list.
*
* @param el the element that the node must contain.
*/
Node(U el){
this(el, null);
}// end of constructor.
}// end of subclass.
}// end of class.```
The test was made with Integer objects (the list was made by the numbers 50, 20, 25, 24 and the result of the sort was 50, 24, 25, 20).

bit_bit

2. Member
Join Date
Feb 2010
Location
Italy
Posts
4
Rep Power
0
nobody can help? or maybe the way i want to do the sorting is not clear enough? i really need to understand the reason why it does not work. if there's something you do not understand in the code please ask me.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•