Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
how to display the smallest number in stack on java

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
Answers
-
I would advise you to go through some Data Structure concepts like Sorting and Searching and you'll find your answers eventually.
-
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);} -
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.
-
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 classVector
with five operations that allow a vector to be treated as a stack. The usualpush
andpop
operations are provided, as well as a method topeek
at the top item on the stack, a method to test for whether the stack isempty
, and a method tosearch
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.