Write a display method to go inside of the LinkedStack class. Display the conten
ID: 3709965 • Letter: W
Question
Write a display method to go inside of the LinkedStack class.
Display the contents like this (including the BOTTOM and TOP labels):
BOTTOM element3 element2 element1 TOP
The method header is: public void display()
Notes:
This method goes inside of the LinkedStack class, so you can directly access the instance data variables of that class.
For full credit, your methods should be efficient- O(n).
Take advantage of being able to directly access the chain of linked nodes.
Consider thinking recursively!
Explanation / Answer
public class LinkedStack { static class Node { private int data; private Node next; } private Node head; public void display() { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.