A linked stack is implemented using a standard Node class as follows: import jav
ID: 3791984 • Letter: A
Question
A linked stack is implemented using a standard Node class as follows: import java.util.*; class stack implements Iterable {private Node top; private int size; public stack () {top = null; size = 0;} public Object pop() {if (size == 0) throw new RuntimeException (""); Object answer = top.getData (); top = top.getNext (); Size --; return answer;} public void push (Object x) {Node newNode = new Node (x top); top = newNode; size++;}//the iterator method is missing} Write a class StackIterator to implement objects that can be returned by the stack iterator. Also write the missing stack method called iterator. You can decide whether the iterator will run through the data in the stack in LIFO or FIFO order (one choice is much easier).Explanation / Answer
Hi,
Pease find the program below:
import java.util.*;
import java.lang.Iterable;
@SuppressWarnings("rawtypes")
public class LinkedStack implements Iterable{
private int n; // size of the stack
private Node first; // top of stack
// Helper linked list class
private class Node {
private Object item;
private Node next;
}
/**
* Initializes an empty stack.
*/
public LinkedStack() {
first = null;
n = 0;
}
/**
* Is this stack empty?
* @return true if this stack is empty; false otherwise
*/
public boolean isEmpty() {
return first == null;
}
/**
* Returns the number of items in the stack.
*/
public int size() {
return n;
}
/**
* Adds the item to this stack.
*/
public void push(Object item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
n++;
}
/**
* Removes and returns the item most recently added to this stack.
* @return the item most recently added
*/
public Object pop() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Object item = first.item; // save item to return
first = first.next; // delete first node
n--; //decreases the size by 1
return item; // return the saved item
}
/**
* Returns (but does not remove) the item most recently added to this stack.
* @return the item most recently added to this stack
*/
public Object peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return first.item;
}
/**
* Returns an iterator to this stack that iterates through the items in LIFO order.
*/
public Iterator iterator() {
return new ListIterator();
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator<Object> {
private int i = n;
private Node first1 = first; //the first node
public boolean hasNext()
{
return (i > 0);
}
public Object next()
{
Object item = first1.item;
first1 = first1.next;
i--;
return item;
}
public void remove()
{
// not needed as this is optional
}
}
//Main method to implement the test
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedStack */
LinkedStack ls = new LinkedStack();
/* Perform Stack Operations */
System.out.println("Linked Stack Test ");
char ch;
do
{
System.out.println(" Linked Stack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. size");
System.out.println("6. Iterate Stack");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to push");
ls.push( scan.nextInt() );
break;
case 2 :
try
{
System.out.println("Popped Element = "+ ls.pop());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = "+ ls.peek());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+ ls.isEmpty());
break;
case 5 :
System.out.println("Size = "+ ls.size());
break;
case 6 :
System.out.print("Stack = ");
for (Object i : ls)
System.out.print(i +" ");
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
In case of any issue, please contact.
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.