in Java for data structure Consider the given codeblocks abo

in Java for data structure

Consider the given code-blocks above.

1.Create a new project named StackTesting and create then the Stack interface and the two classes (ArrayStack and LinkedStack) written above.

2.Create a main class to test both classes (i.e. ArrayStack and LinkedStack) by giving a list of specified values within the main method.

3.Add this member method to the preceding ArrayStack class: public E bottom() // returns the bottom element of this stack

4.Add this member method to the preceding LinkedStack class: public E bottom() // returns the bottom element of this stack

5.Add this member method to the ArrayStack class: public void reverse() // reverses the contents of this stack

6. Add this member method to the LinkedStack class

Solution

1.

// ArrayStack.java: This stack is used to implement the Stack<E>

public class ArrayStack<E> implements Stack<E> {

private E[] arrayelement;

private int size;

private static final int INITIAL = 50;

publicArrayStack() {

arrayelement = (E[]) new Object[INITIAL];

    }

publicArrayStack(int newcapacity) {

arrayelement = (E[]) new Object[newcapacity];

    }

public boolean isEmpty() {

return (size == 0);

    }

public E peek() {

if (size == 0) {

throw new java.util.EmptyStackException();

        }

return arrayelement[size - 1]; // This is the top of stack

    }

public E pop() {

if (size == 0) {

throw new java.util.EmptyStackException();

        }

        E createelement = arrayelement[--size];

arrayelement[size] = null;

return createelement;

    }

public void push(E createelement) {

if (size == arrayelement.length) {

resize();

        }

arrayelement[size++] = createelement;

    }

public int size() {

return size;

    }

private void resize() {

assert newsize == arrayelement.length;

Object[] o = new Object[2 * newsize];

System.arraycopy(arrayelemen, 0, a, 0, newsize);

arrayelement = (E[]) o;

    }

}

//Linked stack.java-linked list implementation for the same

public class LinkedStack<E> implements Stack<E> {

private Node<E> topelement;

private int size;

public LinkedStack(){ // This is the constructor

topelement = null;

size = 0;

    }

publicbooleanisEmpty() {

return (size == 0);

    }

public E peek() {

if (size == 0) {

throw new java.util.EmptyStackException();

        }

Return topelement.createlement; //This is the top of stack

    }

public E pop() {

if (isEmpty()) {

throw new java.util.EmptyStackException();

        }

        E createelement = topelement.createelement;

topelement = topelement.next;       

        --size;

return createelement;

    }

public void push(E creteelement) {

top = new Node<E>(createelement, top);

        ++size;

    }

public int size() {

return size;

    }

private static class Node<E> {

        E createelement;

        Node<E> next;

Node(E createelement, Node<E> next) {

this.createelement = createelement;

this.next = next;

        }

    }

in Java for data structure Consider the given code-blocks above. 1.Create a new project named StackTesting and create then the Stack interface and the two class
in Java for data structure Consider the given code-blocks above. 1.Create a new project named StackTesting and create then the Stack interface and the two class
in Java for data structure Consider the given code-blocks above. 1.Create a new project named StackTesting and create then the Stack interface and the two class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site