Ok so here is my code.
import java.util.*;
public class MainProgram
{
public static void main(String[] args)
{
Scanner userinput = new Scanner(System.in);
System.out.print("Please enter the size of the array. ");
int count = userinput.nextInt(); //user enters the size of the array.
int[] numbers = new int[count]; //array containing hte integers.
int r=0;
QueueLinkedList[] queue = new QueueLinkedList[10]; //created an instance of the queue. 10 bins needed for the Radix Sort.
for(int i=0; i<queue.length; i++)
{
queue[i] = new QueueLinkedList();
}
for(int index = 0; index < numbers.length; index++) //generating random integers.
{
numbers[index] = (int)(Math.random()*1001);
System.out.print(index+" ");
System.out.println(numbers[index]);
}
for(int i=0; i<numbers.length; i++) //identifying the LSD and store the integers in queues accoring to the LSD.
{ //the mistake is in this loop! In the enqueue I believe.
numbers[i] = (numbers[i] % 10)/1;
// System.out.println(numbers[r]);
queue[i].enqueue(numbers[i]);
//queue[r].dequeue();
}
for(int i=0;i<queue.length;i++){ //deque and printing the integers. Here the integers should be sorted by the LSD. 1st PASS.
queue[i].dequeue(); //however output is noot good!
}
}
}
class QueueLinkedList {
private Node head;
//LinkedList constructor
public QueueLinkedList()
{
head = null;
}
//Inserts a new Link at the head of the list
public void enqueue(int d1)
{
Node node = new Node(d1);
Node temp = head;
if (head == null)
{
head = node;
}
else
{
while (temp.nextNode != null)
temp = temp.nextNode;
temp.nextNode = node;
}
}
public Node dequeue()
{
Node temp = null;
if(head==null)
{
System.out.println("Queue is empty!");
}
else
{
temp = head;
head = head.nextNode;
temp.printList();
}
return temp;
}
//Prints list data
public void printList()
{
Node temp = head;
System.out.println("List: ");
while(temp != null)
{
temp.printList();
temp = temp.nextNode;
}
System.out.println("");
}
public void printLastItem()
{
Node temp = head;
while(temp.nextNode !=null)
{
temp = temp.nextNode;
}
temp.printList();
}
public void peek()
{
Node temp = head;
temp.printList();
}
public void isEmpty()
{
if(head == null)
{
System.out.println("Queue is empty!");
}
else
{
System.out.println("Queue is not empty!");
}
}
public void length()
{
Node temp = head;
int count = 0;
while(temp != null)
{
temp = temp.nextNode;
count = count + 1;
}
System.out.println(count);
}
}
class Node{
public int data1;
public Node nextNode;
//Link constructor
public Node(int d1)
{
data1 = d1;
}
//Print Link data
public void printList() {
System.out.println(data1);
}
}
Ok so here is my program. Im trying to implement a radix sort but Im stuck. Can anyone take a look at my code and tell my how to proceed please. Im having trouble enqueueing the integers according to their LSD.
Thanks in advance for ypur help!