Question: //trying to make stack implemented by LinkedList which takes input from user via console(i dont know if i am doing this right) and pops the

//trying to make stack implemented by LinkedList which takes input from user via console(i dont know if i am doing this right) and pops the word after "*" or pushes(item) if there is no "*"

example input: Hey there * how * are * you * doing there

//Disregard use of StdIn and StdOut style I have to use them for this exercise.

public class Practice { private Node first= null; private int N= 0; private class Node { private Item item; private Node next; } public boolean isEmpty() { return (N==0);} public void push(Item item) { Node second= first; first= new Node(); first.item= item; first.next= second; N++; } public Item pop() { Item item= first.item; first= first.next; N--; return item; } public int size() { return N; }

public static void main(String[] args) { Practice stack= new Practice(); while(!StdIn.isEmpty()) { String item= StdIn.readString(); if(item.equals("*")) System.out.print(stack.pop()+ " "); else stack.push(item); } StdOut.println(); }

}

//Explanations required about StdIn and use of console for input : Best regards

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!