import java.util.EmptyStackException;
public class IntStackTest implements IntStack{ private int top = -1;
private int[] data;
private static final int DEFAULT_CAPACITY = 10;
public IntStackTest() {
data = new int[DEFAULT_CAPACITY];
}
public void push(int item) {
if (top == data.length - 1) resize(2*data.length);
data[++top] = item; }
public int pop() {
//if (isEmpty()) throw new EmptyStackException();
return data[top--]; }
private void resize(int newCapacity) { int[] newData = new int[newCapacity]; for (int i = 0; i <= top; i++) {
newData[i] = data[i]; }
data = newData; }
public static void main(String[] args) { int x;
IntStack s = new IntStackTest(); s.push(7);
s.push(4);
s.push(18);
4
x = s.pop(); System.out.println("pop() ---> " + x ); x = s.pop(); System.out.println("pop() ---> " + x ); x = s.pop(); System.out.println("pop() ---> " + x );
} }
A. Provide a graphical representation of the Stack when the above code is run
import java.util.IntSummaryStatistics;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.PrimitiveIterator.OfInt;
import java.util.function.BiConsumer;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntPredicate;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.ObjIntConsumer;
import java.util.function.Supplier;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
public class IntStackTest implements IntStream {
private int top = -1;
private int[] data;
private static final int DEFAULT_CAPACITY = 10;
public IntStackTest() {
data = new int[DEFAULT_CAPACITY];
}
public void push(int item) {
if (top == data.length - 1)
resize(2 * data.length);
data[++top] = item;
}
public int pop() {
//if (isEmpty()) throw new EmptyStackException();
return data[top--];
}
private void resize(int newCapacity) {
int[] newData = new int[newCapacity];
for (int i = 0; i <= top; i++) {
newData[i] = data[i];
}
data = newData;
}
public static void main(String[] args) {
int x;
IntStackTest s = new IntStackTest();
s.push(7);
s.push(4);
s.push(18);
x = s.pop();
System.out.println("pop() ---> " + x);
x = s.pop();
System.out.println("pop() ---> " + x);
x = s.pop();
System.out.println("pop() ---> " + x);
}
@Override
public boolean isParallel() {
// TODO Auto-generated method stub
return false;
}
@Override
public IntStream unordered() {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream onClose(Runnable closeHandler) {
// TODO Auto-generated method stub
return null;
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public IntStream filter(IntPredicate predicate) {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream map(IntUnaryOperator mapper) {
// TODO Auto-generated method stub
return null;
}
@Override
public <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) {
// TODO Auto-generated method stub
return null;
}
@Override
public LongStream mapToLong(IntToLongFunction mapper) {
// TODO Auto-generated method stub
return null;
}
@Override
public DoubleStream mapToDouble(IntToDoubleFunction mapper) {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream flatMap(IntFunction<? extends IntStream> mapper) {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream distinct() {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream sorted() {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream peek(IntConsumer action) {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream limit(long maxSize) {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream skip(long n) {
// TODO Auto-generated method stub
return null;
}
@Override
public void forEach(IntConsumer action) {
// TODO Auto-generated method stub
}
@Override
public void forEachOrdered(IntConsumer action) {
// TODO Auto-generated method stub
}
@Override
public int[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public int reduce(int identity, IntBinaryOperator op) {
// TODO Auto-generated method stub
return 0;
}
@Override
public OptionalInt reduce(IntBinaryOperator op) {
// TODO Auto-generated method stub
return null;
}
@Override
public <R> R collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner) {
// TODO Auto-generated method stub
return null;
}
@Override
public int sum() {
// TODO Auto-generated method stub
return 0;
}
@Override
public OptionalInt min() {
// TODO Auto-generated method stub
return null;
}
@Override
public OptionalInt max() {
// TODO Auto-generated method stub
return null;
}
@Override
public long count() {
// TODO Auto-generated method stub
return 0;
}
@Override
public OptionalDouble average() {
// TODO Auto-generated method stub
return null;
}
@Override
public IntSummaryStatistics summaryStatistics() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean anyMatch(IntPredicate predicate) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean allMatch(IntPredicate predicate) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean noneMatch(IntPredicate predicate) {
// TODO Auto-generated method stub
return false;
}
@Override
public OptionalInt findFirst() {
// TODO Auto-generated method stub
return null;
}
@Override
public OptionalInt findAny() {
// TODO Auto-generated method stub
return null;
}
@Override
public LongStream asLongStream() {
// TODO Auto-generated method stub
return null;
}
@Override
public DoubleStream asDoubleStream() {
// TODO Auto-generated method stub
return null;
}
@Override
public Stream<Integer> boxed() {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream sequential() {
// TODO Auto-generated method stub
return null;
}
@Override
public IntStream parallel() {
// TODO Auto-generated method stub
return null;
}
@Override
public OfInt iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public java.util.Spliterator.OfInt spliterator() {
// TODO Auto-generated method stub
return null;
}
}
Comments
Leave a comment