IN JAVA THIS QUESTION HAS BEEN INCRORRECTLY ANSWERED TWICE I
IN JAVA!!!!!!!
THIS QUESTION HAS BEEN INCRORRECTLY ANSWERED TWICE IN C, I NEED IT ANSWERED IN JAVA!!!!
Thanks,
The Problem
Integer numbers must be loaded into and stored in a stack such that the largest value is always stored at the top position (that is the numbers are kept sorted in decreasing order).
The numbers are read by the program from a file. The numbers in the file are not necessarily sorted.
The stack must be linked list based.
Generic implementation is required, that is, the data in the nodes must be objects. Use the Integer type at instantiation for data rather than int.
The implementation must be tested and the result (decreasing storage) verified.
Determine the big-Oh for the performance of your algorithm
Analysis and Design
Make a careful design before implementation. Decide about the classes and additional methods needed to load up the stack.
Write a pseudo-code (attach it as a comment after the Application class).
Implementation and Testing
When a new element is added to the stack, one or more elements may have to be popped, since the new value may not be greater than the current top value. You will need a temporary storage for these popped elements before they can be pushed back. You must use a second stack for temporary storage .
Document your program
Solution
Here is the code for your question. The comments are inline. Please do rate the answer if you find it useful. Thanks.
--------
Stack.java
---------
class Node<T>
{
T data;
Node<T> next;
Node(T d) //constructor with only data
{
data=d;
next=null;
}
Node(T d,Node<T> n)//constructor with data nad next link
{
data=d;
next=n;
}
}
//A generic stack
public class Stack<T> {
protected Node<T> top;
protected int size;
public Stack() //default constructor
{
top=null;
size=0;
}
public void push(T data)
{
//always adding to top of the stack
top=new Node<T>(data,top);
size++;
}
//returns data after popping from stack
public T pop()
{
if(top==null)
return null;
else
{
T d=top.data;
top=top.next;
size--;
return d;
}
}
//returns data WITHOUT popping
public T top()
{
return top==null?null:top.data;
}
//returns true if stack is empty, false otherwise
public boolean isEmpty()
{
return top==null;
}
//return the number of elements in stack
public int size()
{
return size;
}
//displays the stack contents
public void display()
{
if(size!=0)
{
for(Node<T> temp=top;temp!=null;temp=temp.next)
{
System.out.println(temp.data);
}
}
}
}
----------
SortedNumbers.java
---------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SortedNumbers {
static Stack<Integer> temporary=new Stack<Integer>(); // a temporary stack used while sorting the stack
//Function to store a number in stack such that they are always sorted. When a number is to be stored,
//it is first compared with top, if its less than the top element, then all numbers that are greater than
// the element to be stored are popped and stored on temporary stack and then the new element is pushed
//into current stack and then elements from temporary stack to popped and pushed back into current stack
public static void storeSorted(Stack<Integer> stack,int num)
{
if(stack.isEmpty())//nothing to do just push
stack.push(new Integer(num));
else
{
//pop all numbers greater than num and store them on temporary stack
while(!stack.isEmpty() && num<stack.top())
{
temporary.push(stack.pop());
}
//now push num
stack.push(new Integer(num));
//now push back all numbers from temporary stack
while(!temporary.isEmpty())
stack.push(temporary.pop());
}
}
public static void main(String[] args) {
String filename=\"c:\\\\test\\\ umbers.txt\"; //path of file to read the numbers
int num;
Stack<Integer> stack=new Stack<Integer>();
try {
Scanner scanner=new Scanner(new File(filename)); //open the file to scan
while(scanner.hasNext())
{
num=scanner.nextInt(); //get the next number
storeSorted(stack, num);
}
scanner.close();
System.out.println(\"The sorted stack of numbers is as follows:\");
stack.display();
} catch (FileNotFoundException e) {
System.out.println(\"File not found : \"+filename);
}
}
}
-----------
sample numbers.txt
------
5 20 4 19 31 1 0 100 25 20 -1
---------
output
-------
The sorted stack of numbers is as follows:
100
31
25
20
20
19
5
4
1
0
-1



