Your sort logic is perfectly fine. If you change your stack logic, the code will work perfectly fine
Here is a sample Stack Code:
class Stack{
Node head;
public Stack(){
this.head = null;
}
void push(int value){
Node temp = new Node(value);
temp.next = head;
if(head == null)
head = temp;
else{
temp.next = head;
head = temp;
}
}
int pop(){
if(head != null){
int temp = head.value;
head = head.next;
return temp;
} else {
throw new EmptyStackException();
}
}
boolean isEmpty() {
return head == null;
}
int peek(){
if(!isEmpty())
return head.value;
else
throw new EmptyStackException();
}
void print(){
Node ref = head;
System.out.println("Contents of stack are:");
while(ref != null){
System.out.println(ref.value);
ref = ref.next;
}
}
}
class Node{
public int value;
public Node next;
public Node(int value){
this.value = value;
this.next = null;
}
}
Replace this stack code in your program. There you go.. Your input will be sorted