Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1
- package One;
- /**
- * Created by Daniel on 4/6/2016.
- */
- public class ArrayStack
- {
- private static final int SIZE = 10; //size of array
- private Object[] item; //items within the stack
- private int numItem; //#of items
- public ArrayStack()
- {
- item = new Object[SIZE];
- numItem = 0;
- }
- // Pushes (adds)
- public void push(Object x)
- {
- if(item.length-1 == numItem)
- {
- item[numItem] = x;
- numItem--;
- }
- }
- //Pops (removes)
- public Object pop()
- {
- if(numItem == item.length-1)
- {
- item[numItem] = null;
- numItem--;
- }
- return item[numItem];
- }
- }
- ---------------
- 2
- package Two;
- /**
- * Created by Daniel on 4/6/2016.
- */
- public class LinkedStack
- {
- private static class Node
- {
- int item;
- Node next;
- }
- private Node top = null; //node that is at the top of the stack is null
- public void push(int n)
- {
- Node newTop; //holds new top stack node
- newTop = new Node();
- newTop.item = n;
- newTop.next = top; //new node refers to old top
- top = newTop; //new top
- }
- public int pop()
- {
- int topItem = top.item;
- top = top.next; //popped item
- return topItem; //previous node is now the top
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement