Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using this Node class below complete the stack class(which is below the Node class) to get the yays. class Node { private T value; private

Using this Node class below complete the stack class(which is below the Node class) to get the yays. class Node { private T value; private Node next; private Node prev; public Node(T value) { this.value = value; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } public Node getNext() { return this.next; } public void setNext(Node next) { this.next = next; } public Node getPrev() { return this.prev; } public void setPrev(Node prev) { this.prev = prev; } public static String listToString(Node head) { StringBuilder ret = new StringBuilder(); Node current = head; while(current != null) { ret.append(current.value); ret.append(" "); current = current.getNext(); } return ret.toString().trim(); } public static String listToStringBackward(Node head) { Node current = head; while(current.getNext() != null) { current = current.getNext(); } StringBuilder ret = new StringBuilder(); while(current != null) { ret.append(current.value); ret.append(" "); current = current.getPrev(); } return ret.toString().trim(); } public static void main(String[] args) { //main method for testing, edit as much as you want //make nodes Node n1 = new Node<>("A"); Node n2 = new Node<>("B"); Node n3 = new Node<>("C"); //connect forward references n1.setNext(n2); n2.setNext(n3); //connect backward references n3.setPrev(n2); n2.setPrev(n1); //print forward and backward System.out.println(Node.listToString(n1)); System.out.println(Node.listToStringBackward(n1)); } } 
import javax.xml.soap.Node; import java.util.Iterator; class CallStack implements Iterable { // You'll want some instance variables here //Create a linkedlist Node -item, value-value of the item. //LIFO public CallStack() { } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } /** * Return item on top. * Size will be reduced. * @return */ public T pop() { //pop an item off the stack //if there are no items on the stack, return null //O(1) //replace this dummy return statement return null; } /** * Return the item on top. * @return */ public T peek() { //return the top of the stack (but don't remove it) //if there are no items on the stack, return null //O(1) //replace this dummy return statement return null; } public String toString() { //Create a string of the stack where each item //is separated by a space. The top of the stack //should be shown to the right and the bottom of //the stack on the left. //Hint: Reuse the provided code from another class //instead of writing this yourself! //O(n) //replace this dummy return statement return null; } public void clear() { //remove everything from the stack //O(1) } public int size() { //return the number of items on the stack //O(1) //replace this dummy return statement return size; } public boolean isEmpty() { //return whether or not the stack is empty //O(1) //replace this dummy return statement return false; } @SuppressWarnings("unchecked") public Object[] toArray() { //Return an array representation of the stack. //The top of the stack should be element 0 //in the array. //O(n) //replace this dummy return statement return null; } /** * * @return the top element. Make sure the top element gets updated. */ public Iterator iterator() { //Return an iterator that traverses from //the top of the stack to the bottom of //the stack. //The iterator's hasNext() and next() methods //must both be O(1) //next() should throw a NullPointerException //if you try to use next when there are no //more items //replace this dummy return statement return null; } public static void main(String[] args) { CallStack s1 = new CallStack<>(); s1.push("a"); s1.push("b"); CallStack s2 = new CallStack<>(); s2.push(1); s2.push(2); s2.push(3); if(s1.toString().equals("a b") && s1.toArray()[0].equals("b") && s1.toArray()[1].equals("a") && s1.toArray().length == 2) { System.out.println("Yay 1"); } if(s1.peek().equals("b") && s2.peek().equals(3) && s1.size() == 2 && s2.size() == 3) { System.out.println("Yay 2"); } if(s1.pop().equals("b") && s2.pop().equals(3) && s1.size() == 1 && s2.size() == 2) { System.out.println("Yay 3"); } if(s1.toString().equals("a") && s1.peek().equals("a") && s2.peek().equals(2) && s1.pop().equals("a") && s2.pop().equals(2) && s1.size() == 0 && s2.size() == 1) { System.out.println("Yay 4"); } if(s1.toString().equals("") && s1.peek() == null && s2.peek().equals(1) && s1.pop() == null && s2.pop().equals(1) && s1.size() == 0 && s2.size() == 0) { System.out.println("Yay 5"); } s2.push(10); s2.push(20); s2.push(30); if(s1.isEmpty() && s1.toArray().length == 0 && !s2.isEmpty()) { s2.clear(); if(s2.isEmpty()) { System.out.println("Yay 6"); } } CallStack s3 = new CallStack<>(); s3.push(3); s3.push(2); s3.push(1); int i = 1; for(Integer item : s3) { if(i == item) System.out.println("Yay " + (6+i)); else System.out.println(item); i++; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions