class Stack private int size; private Node top: public Stack() { this.size = 0; this.top null; } public int pop() { int toPop = -999; if (size != 0) { toPop = top.getData(); top = top.getNextNode(); size --; // decrease the value }else{ System.out.println("Stack is now empty."); } return toPop; } public int peak(){ in toPeak -999; if (size != 0) { toPeak = top.getData(); }else{ System.out.println("Stack is now empty."); } return toPeak; public void push(int data) { Node tempNode = new Node (data); tempNode.setNextNode(top); top = tempNode; size++; // ask what this does } 9 public void display(){
public void display(){ Node temp = top: while(temp != null){ System.out.println(temp.getData()); temp = temp.getNextNode(): ; } }
Modify the stack class that we implemented in-class on April 11, using Linked List to add another method to access or search) an element at a certain index from the stack. Hint: it would be a modified version of the display() method. You would be sending the index as an argument and you keep a counter. Instead of printing all elements, you print the element only when your counter matches the index,
class Stack private int size; private Node top: public Stack() { this.size = 0; this.top null; } public int pop() { int
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
class Stack private int size; private Node top: public Stack() { this.size = 0; this.top null; } public int pop() { int
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!