java
create java program that make stack with LinkedList and stack is
implement iterator. When stack’s iterator call next(), it pop its
data.
here is the example of output
//by user
5
1 2 3 4 5
//then output comes like this
5
4
3
2
1
Stack is empty.
here is the code that i'm going to use
class Stack<T> implements Iterator<T> {
LinkedList<T> list;
public Stack() {
list = new
LinkedList<T>();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void push(T data) {
// need code
}
public T pop() {
if (list.isEmpty())
return
null;
// need code
}
public T peek() {
if (list.isEmpty())
return
null;
// need code
}
public boolean hasNext() {
if (list.size() <= 0)
return
false;
return true;
}
public T next() {
// need code
}
}
class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Stack<Integer> stack = new
Stack<>();
int count = sc.nextInt();
for (int i = 0; i < count; i++)
{
int num =
sc.nextInt();
stack.push(num);
}
Iterator<Integer> itr =
stack;
while(itr.hasNext())
System.out.println(itr.next());
if (stack.isEmpty())
System.out.println("Stack is empty.");
sc.close();
}
}
//java code
import java.util.Iterator; import java.util.LinkedList; class Stack<T> implements Iterator<T> { LinkedList<T> list; public Stack() { list = new LinkedList<T>(); } public boolean isEmpty() { return list.isEmpty(); } public void push(T data) { ; list.push(data); } public T pop() { if (list.isEmpty()) return null; T data= list.pop(); return data; } public T peek() { if (list.isEmpty()) return null; // need code T data= list.peekFirst(); return data; } public boolean hasNext() { if (list.size() <= 0) return false; return true; } public T next() { // need code T data= list.pop(); return data; } }
import java.util.Iterator; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Stack<Integer> stack = new Stack<>(); System.out.print("How many numbers you want to stack: "); int count = sc.nextInt(); System.out.println("Enter numbers: "); for (int i = 0; i < count; i++) { int num = sc.nextInt(); stack.push(num); } System.out.println("Peek value: "+stack.peek()); Iterator<Integer> itr = stack; while(itr.hasNext()) System.out.println(itr.next()); if (stack.isEmpty()) System.out.println("Stack is empty."); sc.close(); } }
//output
//if you need any help regarding this solution ............ please leave a comment ............ thanks..
java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...
What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...
NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...
Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i, j; System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) "); int num...
Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...
Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list. This is only supposed to use THAT method, calling a normal Stack will give me a zero. I do have the conversion to postfix, but there may be error in there. But the main problem currently is the evaluation of postfix. I keep getting an error that I made for an empty stack, which I will include. For testing it is only supposed to...
Array with Iterator. Java style
Implement an array data structure as a class JstyArray<E>
to support the Iterable interface such that the following code
works:
JstyArray<Integer> data;
data = new JstyArray<Integer>(10);
for (int i = 0; i < 10; ++i) {
data.set(i, new Integer(i) );
}
int sum = 0;
for ( int v : data ) {
if (v == null)
continue; // empty cell
sum += v;
}
The iterator provided by this class follows the behaviour of...
Im try to create a java program that checks to see if a given boolean expression is a tautology or not my code so far is as follows: public static class TreeNode { char data; TreeNode left; TreeNode right; TreeNode(char item) { data = item; left = null; right = null; } } public static...
I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array. //Array implementation of stacks. import java.util.Arrays; public class ArrayStack implements Stack { //Declare a class constant called DEFAULT_STACK_SIZE with the value 10. private static final int DEFAULT_STACK_SIZE = 10; /* Declare two instance variables: 1. An integer called...