Use a tree stack to sort number in java?

Use a tree stack to sort number in java?

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

this my coding

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);

} }

the answer i get is

The Input Stack is : [90, 21, 33, 80, 67] MAx: 67 Value: 80

The Output Stack is: [80]

i want to get the sorted stack in Output stack

View Answers









Related Tutorials/Questions & Answers:
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?
Advertisements
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?
Use a tree stack to sort number in java?
difference between java5 and java6 - Java Beginners
Java2
Javah
about java1
Maven dependency for com.gooddata - gooddata-http-client version 1.0.0+java7 is released. Learn to use gooddata-http-client version 1.0.0+java7 in Maven based Java projects
About Java2
javaa swings - IDE Questions
Javap Tool application
Stack
javab - Java Beginners
Javah -  Header File Generator
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-alpha05 is released. Learn to use plugins-javax-inject version 2.0.0-alpha05 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-alpha04 is released. Learn to use plugins-javax-inject version 2.0.0-alpha04 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-alpha01 is released. Learn to use plugins-javax-inject version 2.0.0-alpha01 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0 is released. Learn to use plugins-javax-inject version 2.0.0 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-rc2 is released. Learn to use plugins-javax-inject version 2.0.0-rc2 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-alpha06 is released. Learn to use plugins-javax-inject version 2.0.0-alpha06 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-alpha03 is released. Learn to use plugins-javax-inject version 2.0.0-alpha03 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-alpha02 is released. Learn to use plugins-javax-inject version 2.0.0-alpha02 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-rc1 is released. Learn to use plugins-javax-inject version 2.0.0-rc1 in Maven based Java projects
Maven dependency for com.episode6.mockspresso2 - plugins-javax-inject version 2.0.0-beta01 is released. Learn to use plugins-javax-inject version 2.0.0-beta01 in Maven based Java projects
Sort
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.16 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.16 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.11 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.11 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.15 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.15 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.14 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.14 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.12 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.12 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.8 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.8 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.7 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.7 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.6 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.6 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.9 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.9 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.10 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.10 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.13 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.13 in Maven based Java projects
Maven dependency for io.yupiik.uship - servlet-jakarta-javax-bridge version 1.0.5 is released. Learn to use servlet-jakarta-javax-bridge version 1.0.5 in Maven based Java projects
Sort
Artifacts of javax
Javac -source KeyTest.java
bubble sort
Java Stack Example
Maven dependency for javax - javaee-web-api version 7.0 is released. Learn to use javaee-web-api version 7.0 in Maven based Java projects
ModuleNotFoundError: No module named 'javax'
ModuleNotFoundError: No module named 'javax'
javax - javaee-endorsed-api version 7.0 Maven dependency. How to use javaee-endorsed-api version 7.0 in pom.xml?
Tree

Ads