Question: Help needed please. Why I'm trying to use the top method in order to print the first element of my list. why is it not

Help needed please. Why I'm trying to use the top method in order to print the first element of my list. why is it not working?

import java.util.Stack; public class MyDS { Stack s; Stack minStack; Stack maxStack; public int top; public MyDS(){ s = new Stack(); minStack = new Stack(); maxStack = new Stack(); } //top method public top() { return stack[t]; } //Size method public int size() { return int top = -1; } //Empty method public boolean isEmpty() { return t == -1; } // Push Method public void push(int k){ if(minStack.isEmpty()){ minStack.push(k); }else if(k <= minStack.peek()){ minStack.push(k); } if(maxStack.isEmpty()){ maxStack.push(k); }else if(k >= maxStack.peek()){ maxStack.push(k); } s.push(k); } // Pop Method public void pop(){ int popped; if(!s.isEmpty()){ popped = s.pop(); }else{ popped = -1; } if(popped == min()){ minStack.pop(); } if(popped == max()){ maxStack.pop(); } } // Min Method public int min(){ if(!minStack.isEmpty()){ return minStack.peek(); }else{ return Integer.MIN_VALUE; } } // Max Method public int max(){ if(!maxStack.isEmpty()){ return maxStack.peek(); }else{ return Integer.MAX_VALUE; } } }

import java.util.Stack; public class DS { static Stack stack; static Stack minStack; static Stack maxStack; public DS(){ stack = new Stack(); minStack = new Stack(); maxStack = new Stack(); } // Push Method public void push(int k){ stack.push(k); if(!minStack.isEmpty()){ minStack.push(Math.min(k, minStack.peek())); }else{ minStack.push(k); } if(!maxStack.isEmpty()){ maxStack.push(Math.max(k, maxStack.peek())); }else{ maxStack.push(k); } } // Pop Method public void pop(){ if(!stack.isEmpty() && !minStack.isEmpty() && !maxStack.isEmpty()){ stack.pop(); minStack.pop(); maxStack.pop(); } } // Find Min public int findMin(){ if(!minStack.isEmpty()){ return minStack.peek(); } return Integer.MIN_VALUE; } // Find Max public int findMax(){ if(!maxStack.isEmpty()){ return maxStack.peek(); } return Integer.MAX_VALUE; } public static void main(String[] args) { DS ds = new DS(); System.out.println("size is " + stack.size()); System.out.println("Is Empty? " + stack.isEmpty()); System.out.println("Push 7, 6, 5: "); ds.push(7); ds.push(6); ds.push(5); System.out.println("Size now is " + stack.size()); System.out.println("Is Empty? now " + stack.isEmpty()); System.out.println("top element is "+ stack.top()); //This line says cannot find symbol. why? System.out.println("S1: " + stack); System.out.println("S2: " + minStack); System.out.println("S3: " + maxStack); System.out.println("Min till now: " + ds.findMin()); System.out.println("Max till now: " + ds.findMax()); System.out.println("Push 4, 3: "); ds.push(4); ds.push(3); System.out.println(stack); System.out.println(minStack); System.out.println(maxStack); System.out.println("Min till now: " + ds.findMin()); System.out.println("Max till now: " + ds.findMax()); System.out.println("1 pop(): "); ds.pop(); System.out.println("Min till now: " + ds.findMin()); System.out.println("Max till now: " + ds.findMax()); System.out.println("1 pop(): "); ds.pop(); System.out.println("Min till now: " + ds.findMin()); System.out.println("Max till now: " + ds.findMax()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!