gigabytemon

StackTrack.java

Jun 30th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. // (c) Roxy the Programming Fox
  2.  
  3. package stackLaboratory;
  4. import java.util.*;
  5.  
  6. public class StackTrack {
  7.     //attribs
  8.     private Stack <Integer> mainStack;
  9.     private Stack <Integer> nextMaxInStack;
  10.    
  11.     //constructors
  12.     public StackTrack()
  13.     {
  14.         this.mainStack = new Stack<Integer>();
  15.         this.nextMaxInStack = new Stack<Integer>();
  16.     }
  17.    
  18.     //muttons
  19.     public Stack<Integer> getMainStack() {
  20.         return mainStack;
  21.     }
  22.     public void setMainStack(Stack<Integer> mainStack) {
  23.         this.mainStack = mainStack;
  24.     }
  25.     public Stack<Integer> getnextMaxInStack() {
  26.         return nextMaxInStack;
  27.     }
  28.     public void setnextMaxInStack(Stack<Integer> nextMaxInStack) {
  29.         this.nextMaxInStack = nextMaxInStack;
  30.     }
  31.    
  32.     //logicals
  33.     /*  1. push to stack
  34.      *  2. pop from stack
  35.      *  3. peek from stack
  36.      */
  37.     public void push(int i)
  38.     {
  39.         this.mainStack.push(i);
  40.         /* [if] first token,
  41.          * -> first token is maximum (since it's the only token)
  42.          *
  43.          * [else, if] token > current maximum
  44.          * -> push current token to nextMaxInStack
  45.          *
  46.          * [else] (token < or == current maximum)
  47.          * -> push nextMaxInStack.peek() to nextMaxInStack
  48.          */
  49.         if (this.mainStack.size() == 1) {
  50.             this.nextMaxInStack.push(i);
  51.         }
  52.         else if (i > this.nextMaxInStack.peek()){
  53.             this.nextMaxInStack.push(i);
  54.         }
  55.         else {
  56.             this.nextMaxInStack.push(this.nextMaxInStack.peek());
  57.         }
  58.     }
  59.    
  60.     public void pop()
  61.     {
  62.         Integer[] returnArray = {this.mainStack.pop(), this.nextMaxInStack.pop()};
  63.  
  64.         print(returnArray);
  65.     }
  66.    
  67.     public void peek()
  68.     {
  69.         Integer[] returnArray = {this.mainStack.peek(), this.nextMaxInStack.peek()};
  70.        
  71.         print(returnArray);
  72.     }
  73.    
  74.     public int nextLocalMaximum()
  75.     {
  76.         return this.nextMaxInStack.peek();
  77.     }
  78.    
  79.     //print functions
  80.     private void print(Integer[] n)
  81.     {
  82.         System.out.println(n[0] + "\t| " + n[1]);
  83.     }
  84.    
  85.     public void popAll()
  86.     {
  87.         System.out.println("Current\t| Next");
  88.         while(!this.mainStack.isEmpty())
  89.             this.pop();
  90.     }
  91.    
  92.     //hasNext
  93.     public boolean hasNext()
  94.     {
  95.         if (!this.mainStack.isEmpty())
  96.             return true;
  97.         else return false;
  98.     }
  99.  
  100.  
  101.     //---main function---//
  102.     //rand for generating stack
  103.     static Random RAND = new Random();
  104.     public static void main (String args[])
  105.     {      
  106.         //initialize
  107.         StackTrack st = new StackTrack();
  108.        
  109.         //put values (generate 10)
  110.         for (int i = 0 ; i < 10 ; i++)
  111.             st.push(RAND.nextInt(100));
  112.        
  113.         //pop values
  114.         st.popAll();
  115.     }
  116. }
  117.  
  118. /* ---SAMPLE OUTPUT--- /
  119.  * Current  | Next
  120.  * 70       | 98
  121.  * 21       | 98
  122.  * 66       | 98
  123.  * 87       | 98
  124.  * 98       | 98
  125.  * 33       | 45
  126.  * 45       | 45
  127.  * 32       | 32
  128.  * 18       | 24
  129.  * 24       | 24
  130.  * ---SAMPLE OUTPUT--- */
Advertisement
Add Comment
Please, Sign In to add comment