-3

I'm having a hard time starting my lab because I have no idea what the instruction is telling me to do.

Instruction:

enter image description here Here's what I have so far:

public class MyStack<AnyType> implements Stack<AnyType>  
{

public boolean isEmpty() 
{
    return false;
}

public void push(AnyType x) 
{   

    }

public AnyType pop() 
{   
    return null;
}

public AnyType peek() 
 {  
    return null;
 }
}

So, basically the only thing I have done so far was create another class that just contains an interface which sets up the methods for this class. I do have the "Lab 2" that the instructors assigned if I need to move that java file to this class, you can just say that. Also, how exactly do I instantiate this class? (My level of understanding Java is fairly low, so if you could ELI5 that would be great).

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • The first step asks you to make your SingleLinkedList a [member](https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html) of `MyStack`, using a specific [constructor implementation](https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html) that can instantiate the list. For the second part, see [this](http://stackoverflow.com/questions/294234/inserting-a-node-into-a-linked-list-in-constant-time). – SME_Dev Sep 30 '15 at 17:19

1 Answers1

0

Something along the lines of this:

interface IStack<T> {
    void push(T x);

    T pop();
}

class SinglyLinkedList<T> {

    private class Node {
        T data;
        Node next;

        public Node(T data) {
            this.data = data;
        }
    }

    private Node first;
    private int size;

    public void addFront(T x) {
        Node newNode = new Node(x);
        newNode.next = first;
        first = newNode;
        size++;
    }

    public void removeFront() {
        if (first == null) return;
        first = first.next;
        size--;
    }

    public T getFront() {
        if (first == null) return null;
        return first.data;
    }

    public int size() {
        return size;
    }
}

class Stack<T> implements IStack<T> {

    private SinglyLinkedList<T> referenceToTheSinglyLinkedListYouMadeInLab2;

    public Stack() {
        referenceToTheSinglyLinkedListYouMadeInLab2 = new SinglyLinkedList<>();
    }

    @Override
    public void push(T x) {
        referenceToTheSinglyLinkedListYouMadeInLab2.addFront(x);
    }

    @Override
    public T pop() {
        T result = referenceToTheSinglyLinkedListYouMadeInLab2.getFront();
        referenceToTheSinglyLinkedListYouMadeInLab2.removeFront();
        return result;
    }

    public int size() {
        return referenceToTheSinglyLinkedListYouMadeInLab2.size();
    }

    public static void testClient() {
        Stack<String> st = new Stack<>();
        st.push("one");
        st.push("two");
        System.out.println("Size: " + st.size());
        System.out.println(st.pop());
        System.out.println("Size: " + st.size());
        System.out.println(st.pop());
        System.out.println("Size: " + st.size());
    }
}

public class Program {
    public static void main(String[] args) {
        Stack.testClient();
    }
}

If you want you can make it throw an exception when there are no elements in the stack, but it's already your design choice.

Pavel
  • 1
  • 3
  • 17
  • 51