Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (c) Roxy the Programming Fox
- package stackLaboratory;
- import java.util.*;
- public class StackTrack {
- //attribs
- private Stack <Integer> mainStack;
- private Stack <Integer> nextMaxInStack;
- //constructors
- public StackTrack()
- {
- this.mainStack = new Stack<Integer>();
- this.nextMaxInStack = new Stack<Integer>();
- }
- //muttons
- public Stack<Integer> getMainStack() {
- return mainStack;
- }
- public void setMainStack(Stack<Integer> mainStack) {
- this.mainStack = mainStack;
- }
- public Stack<Integer> getnextMaxInStack() {
- return nextMaxInStack;
- }
- public void setnextMaxInStack(Stack<Integer> nextMaxInStack) {
- this.nextMaxInStack = nextMaxInStack;
- }
- //logicals
- /* 1. push to stack
- * 2. pop from stack
- * 3. peek from stack
- */
- public void push(int i)
- {
- this.mainStack.push(i);
- /* [if] first token,
- * -> first token is maximum (since it's the only token)
- *
- * [else, if] token > current maximum
- * -> push current token to nextMaxInStack
- *
- * [else] (token < or == current maximum)
- * -> push nextMaxInStack.peek() to nextMaxInStack
- */
- if (this.mainStack.size() == 1) {
- this.nextMaxInStack.push(i);
- }
- else if (i > this.nextMaxInStack.peek()){
- this.nextMaxInStack.push(i);
- }
- else {
- this.nextMaxInStack.push(this.nextMaxInStack.peek());
- }
- }
- public void pop()
- {
- Integer[] returnArray = {this.mainStack.pop(), this.nextMaxInStack.pop()};
- print(returnArray);
- }
- public void peek()
- {
- Integer[] returnArray = {this.mainStack.peek(), this.nextMaxInStack.peek()};
- print(returnArray);
- }
- public int nextLocalMaximum()
- {
- return this.nextMaxInStack.peek();
- }
- //print functions
- private void print(Integer[] n)
- {
- System.out.println(n[0] + "\t| " + n[1]);
- }
- public void popAll()
- {
- System.out.println("Current\t| Next");
- while(!this.mainStack.isEmpty())
- this.pop();
- }
- //hasNext
- public boolean hasNext()
- {
- if (!this.mainStack.isEmpty())
- return true;
- else return false;
- }
- //---main function---//
- //rand for generating stack
- static Random RAND = new Random();
- public static void main (String args[])
- {
- //initialize
- StackTrack st = new StackTrack();
- //put values (generate 10)
- for (int i = 0 ; i < 10 ; i++)
- st.push(RAND.nextInt(100));
- //pop values
- st.popAll();
- }
- }
- /* ---SAMPLE OUTPUT--- /
- * Current | Next
- * 70 | 98
- * 21 | 98
- * 66 | 98
- * 87 | 98
- * 98 | 98
- * 33 | 45
- * 45 | 45
- * 32 | 32
- * 18 | 24
- * 24 | 24
- * ---SAMPLE OUTPUT--- */
Advertisement
Add Comment
Please, Sign In to add comment