Forum Stats

  • 3,838,465 Users
  • 2,262,367 Discussions
  • 7,900,657 Comments

Discussions

how to display the smallest number in stack on java

a48b83c2-9191-4110-8dff-2b8afd3e2e4b
edited Oct 28, 2018 10:46AM in Java Programming

Hi, I'm using netBeans IDE, and I need help with,

I have the following code, and I want a simple method to print the smallest number in stack, I so beginner so if you can please write a very simple method to me

class stack1 {<br/>  int maxsize,top;<br/>  //String arr[];<br/>  public int arr[];<br/>  int maxsize1;<br/>  stack1 (int maxsize){<br/>  this.maxsize=maxsize;<br/>  arr=new int[maxsize];<br/>  top=0;<br/>  }<br/>  void push (int data) {<br/>  if (top<maxsize) {<br/>  arr[top]=data;<br/>  top++;}<br/>  else System.out.print("stack is overflow");<br/>  }<br/><br/>  void main(String arg[]) {<br/><br/>stack1 s=new stack1(4);<br/>s.push(1);<br/>s.push(10);<br/>s.push(3);<br/>s.push(9);<br/>}<br/>}

I also have a code to display a maxumi

rpc1

Answers

  • Praveen.Kumar
    Praveen.Kumar Member Posts: 2 Red Ribbon
    edited Jan 29, 2018 6:58PM

    I would advise you to go through some Data Structure concepts like Sorting and Searching and you'll find your answers eventually.

  • 3469961
    3469961 Member Posts: 1
    edited May 14, 2018 5:56AM

    Solution 1:

    If you are implement this program without  java collection then need one more stack so that can pop from stack1 to stack2  until get small number and

    again push numbers to stack1 which are poped from stack2 util its empty.

    Solution 2:

    In development it's better to use Java collection called java.util.Stack

    Output will be:[observe output, iterated like FIFO not FILO]

    1

    2

    3

    Stack<Integer> stack = new Stack<Integer>(); 
    stack.push(1);
    stack.push(2);
    stack.push(3);for(Integer i : stack) {      System.out.println(i);}
  • 272e892f-97e3-4b8e-a578-a7debc4f6ea0
    edited Oct 28, 2018 4:21AM

    Judging from your code your better off using Binary Search. Re-arrange your push in numeric order so that the smallest one will be selected. If you do it this way, you can select the first smaller immediatly.

    Or you can go by the index of the push, and select the smallest that fullfills the for conditional.

  • Unknown
    edited Oct 28, 2018 10:46AM
    I have the following code, and I want a simple method to print the smallest number in stack, I so beginner so if you can please write a very simple method to me 

    As a 'beginner' it is important for you to know two things:

    1. Java already has a 'Stack' class - so you should learn how to use that functionality rather than try to create your own

    https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html

    The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

    See that last clause? You can now also search the stack for an item.

    2. A stack is NOT designed for random access or access to elements other than the top. So you are NOT using a stack in the intended manner.

    Perhaps this is a class assignment? If so it is a TERRIBLE one for both of the above reasons.

    I suggest you use the functionality Java already provides.

    rpc1
This discussion has been closed.