From 89cdf3efb49335e7c07a68a5a64657eeec2288a6 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Mon, 6 Feb 2017 11:41:36 -0500 Subject: Inital commit --- projects/project2_LInfiniteInteger/LStack.java | 66 ++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 projects/project2_LInfiniteInteger/LStack.java (limited to 'projects/project2_LInfiniteInteger/LStack.java') diff --git a/projects/project2_LInfiniteInteger/LStack.java b/projects/project2_LInfiniteInteger/LStack.java new file mode 100644 index 0000000..15e9bed --- /dev/null +++ b/projects/project2_LInfiniteInteger/LStack.java @@ -0,0 +1,66 @@ + +public class LStack implements StackInterface +{ + private Node firstNode; + + public LStack() + { + firstNode = null; + } + + public void push(T anEntry) + { + Node newNode = new Node(anEntry, firstNode); + firstNode = newNode; + } + + public T pop() + { + T result = null; + + if(firstNode != null) + { + result = firstNode.data; + firstNode = firstNode.next; + } + + return result; + } + + public T peek() + { + if(firstNode != null) + { + return firstNode.data; + } + + return null; + } + + public boolean isEmpty() + { + return firstNode == null; + } + + public void clear() + { + firstNode = null; + } + + private class Node + { + private T data; + private Node next; + + private Node(T aData) + { + this(aData, null); + } + + private Node(T aData, Node nextNode) + { + data = aData; + next = nextNode; + } + } +} -- cgit v1.2.3-70-g09d2