I am having a bit of a problem with a linked list. I am generating 40 random integers using a random object from random class and append them to a linked list. This also uses a specified seed. Everything works perfectly minus this one bug. The first thing being printed in the console/output is the linked list with the randomly generated 40 ints. I am then trying to sort the list using a decreasing insert sort which is where I think my bug lies. My attempt at the "decreasing insert sort algorithm" is done in the isdRecI and isdRecII methods, these are recursive (Be careful large parts of this program are recursive, so if you are not familiar with recursion beware). Once the sort is complete I want to print the linked list again in decreasing order. Please try to keep it as simple as possible and if you can like my style of code since I am somewhat of a Java beginner i.e. Please don't over complicate it. As you can see if you compile my code you will see the duplicates in the sorted print. My code is listed below. I also understand the concept of a linked list and the insert sort quite well, but I am having a rough time getting the code to output the way I want. Please feel free to modify my methods. Thank you for your time and contribution.
public class Node
{
private int data = 0;
Node next;
public Node (int datax) //constructor
{
data = datax;
next = null;
}
public int getData() // get the data value
{
return data;
}
public void setData(int datax) // sets the data value
{
data = datax ;
}
public void print() // print node data on one line.
{
System.out.print(data + " ");
}
public Node getNext()
{
return (next);
}
public void setNext(Node nextx)
{
next = nextx;
}
}
import java.util.Random;
public class MySort
{
Node head;
/*
* This method appends iteratively to the end of the linked list
*/
public void appendIter(int datax)
{
Node newNode = new Node(datax);
Node rightpointer = head;
if (rightpointer == null)
{
head = newNode;
return;
}
else
{
while (rightpointer.getNext() != null)
{
rightpointer = rightpointer.getNext();
}
rightpointer.setNext(newNode);
}
}
/*
* This method passes the data to isdRecI
*/
public void isRecI(MySort unsortedList)
{
isRecII(head);
}
public void isRecII(Node unsortedPointer)
{
int data;
if(unsortedPointer == null)
{
return;
}
data = unsortedPointer.getData();
isdRecI(data);
isRecII(unsortedPointer.getNext());
}
/*
* This method sorts the data using insert sort and sorts in decreasing order
*
*/
public void isdRecI(int dx)
{
head = isdRecII(head, dx);
}
public Node isdRecII(Node hp, int dx)
{
Node nxp;
/*
if(hp == null)
{
nxp = new Node(dx); // commented out for testing purposes please uncomment if you need
return nxp;
}
*/
if(dx >= hp.getData())
{
nxp = new Node(dx);
nxp.setNext(hp);
return nxp;
}
hp.setNext(isdRecII(hp.getNext(),dx));
return hp;
}
/*
* This method is an iterative print method for the linked list
*/
public void print()
{
System.out.println("print list start: ");
Node nextrightpointer = head;
while (nextrightpointer != null)
{
nextrightpointer.print();
nextrightpointer = nextrightpointer.getNext();
}
System.out.println("print list end");
}
public static void main(String[] args)
{
MySort SortObject = new MySort ();
Random random = new Random(12345);
for(int i=0; i < 40;i++)
{
SortObject.appendIter(random.nextInt(200));
}
SortObject.print();
SortObject.isRecI(SortObject);
System.out.println();
SortObject.print();
}
}
Also including the output:
print list start: 51 80 41 28 55 84 175 2 101 189 117 142 190 6 12 184 187 103 132 175 1 151 192 116 28 181 25 143 71 39 129 197 101 25 103 155 152 31 10 108 print list end
(After sort // this is not printed just FYI)
print list start: 197 192 190 189 187 184 181 175 175 155 152 151 143 142 132 129 117 116 108 103 103 101 101 84 80 71 55 51 51 80 41 41 39 31 28 28 28 55 84 175 25 25 12 10 6 2 2 101 189 117 142 190 6 12 184 187 103 132 175 1 1 151 192 116 28 181 25 143 71 39 129 197 101 25 103 155 152 31 10 108 print list end