Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uk.ac.shef.dcs.com262.coa10pjh;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class myStack<Item> implements Iterable<Item> {
- private int N; // size of the stack
- private Node first; // top of stack
- private class Node {
- private Item item;
- private Node next;
- }
- /**
- * Create an empty stack.
- */
- public myStack() {
- first = null;
- N = 0;
- assert check();
- }
- public boolean isEmpty() {
- return first == null;
- }
- public int size() {
- return N;
- }
- public void push(Item item) {
- Node oldfirst = first;
- first = new Node();
- first.item = item;
- first.next = oldfirst;
- N++;
- assert check();
- }
- public Item pop() {
- if (isEmpty()) throw new NoSuchElementException("Stack underflow");
- Item item = first.item; // save item to return
- first = first.next; // delete first node
- N--;
- assert check();
- return item; // return the saved item
- }
- public Item peek() {
- if (isEmpty()) throw new NoSuchElementException("Stack underflow");
- return first.item;
- }
- public String toString() {
- StringBuilder s = new StringBuilder();
- for (Item item : this)
- s.append(item + " ");
- return s.toString();
- }
- // check internal invariants
- private boolean check() {
- if (N == 0) {
- if (first != null) return false;
- }
- else if (N == 1) {
- if (first == null) return false;
- if (first.next != null) return false;
- }
- else {
- if (first.next == null) return false;
- }
- // check internal consistency of instance variable N
- int numberOfNodes = 0;
- for (Node x = first; x != null; x = x.next) {
- numberOfNodes++;
- }
- if (numberOfNodes != N) return false;
- return true;
- }
- public Iterator<Item> iterator() { return new ListIterator(); }
- // an iterator, doesn't implement remove() since it's optional
- private class ListIterator implements Iterator<Item> {
- private Node current = first;
- public boolean hasNext() { return current != null; }
- public void remove() { throw new UnsupportedOperationException(); }
- public Item next() {
- if (!hasNext()) throw new NoSuchElementException();
- Item item = current.item;
- current = current.next;
- return item;
- }
- }
- }
- Thanks.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement