The Question is :
Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers and temp is used during the sorting process. The sorting algorithm follows.

1 set up stack in and print it
2 while stack in is not empty repeat
2.1 max = in.pop
2.2 while there are still element in stack in repeat
2.2.1 value = in.pop
2.2.2 if value > max
2.2.2.1 temp.push(max)
2.2.2.2 max = value
2.2.3 else
2.2.3.1 temp.push(value)
2.3 in = temp
2.4 out.push(max)
2.5 temp.clear
3 print sorted stack

import java.util.*;
public class MainAssignment3
{
  public static void main(String[]args) 
  {
   int Max =0;
   int Value =0;

   LinkedList<Integer>Input=new LinkedList<Integer>();
   LinkedList<Integer>Temp=new LinkedList<Integer>();
   LinkedList<Integer>OutPut=new LinkedList<Integer>();

   Input.addLast(90);
   Input.addLast(21);
   Input.addLast(33);
   Input.addLast(80);
   Input.addLast(67);

   System.out.println("The Input Stack is : " + Input);
     
   while(!Input.isEmpty())
   {
       Max = Input.removeLast();  
       Value = Input.removeLast();   
     
      System.out.println("MAx: " +Max);
      System.out.println("Value: " +Value);
 
     if (Value > Max)
     {
      Temp.push(Max);
      Max=Value;
       
      } else {
      Temp.push(Value);
      
     }
     
     Input=Temp;
     OutPut.push(Max);
     Temp.clear();
  
 }
     System.out.println("The Output Stack is: " + OutPut);   
  
 }
}

can any one guide me how to sort a number ?
beause the OutPut Stack Should is [21, 33, 67, 80 ,90]
But i Only Get [67], any one can guide how to do ? thanks

Well, first off, why are you not using the Stack class, rather than a LinkedList?

Also, rather than doing Temp.clear() create a new Stack and assign it to Temp, because at that point Input and Temp both reference the same List/Stack and so you clear the Stack/List referenced by both (since they are the same stack at that point). That fact would also kept the second iteration of the loop from working since everything pushed onto Temp, would also be pushed onto Input (since they are the same Stack/List after the first iteration).

The other option is to use Input.addAll(Temp) , rather than Input = Temp . Then you would need to still call Temp.clear() , of course.

can any one guide me how to sort a number?
beause the OutPut Stack Should is [21, 33, 67, 80 ,90]
But i Only Get [67], any one can guide how to do? thanks

Did you not read the part about your reassigning and then clearing Input and Temp? That is your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.