stack && queue

package elementary_data_structure;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class stack<Item> implements Iterable<Item> {
private Node<Item> first; // top of stack
private int n; // size of the stack


private static class Node<Item> {
private Item item;
private Node<Item> next;
}


public stack() {     //LIFO
first = null;
n = 0;
}


public boolean isEmpty() {
return first == null;
}


public int size() {
return n;
}


public void push(Item item) {
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
n++;
}


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--;
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();
}


public Iterator<Item> iterator() {
return new ListIterator<Item>(first);
}

// an iterator, doesn't implement remove() since it's optional
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current;

public ListIterator(Node<Item> first) {
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;
}
}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------

package elementary_data_structure;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class queue<Item> implements Iterable<Item> {       //FIFO
private Node<Item> first; // beginning of queue
private Node<Item> last; // end of queue
private int n; // number of elements on queue

// helper linked list class
private static class Node<Item> {
private Item item;
private Node<Item> next;
}


public queue() {
first = null;
last = null;
n = 0;
}


public boolean isEmpty() {
return first == null;
}


public int size() {
return n;
}


public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
return first.item;
}


public void enqueue(Item item) {
Node<Item> oldlast = last;
last = new Node<Item>();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
n++;
}


public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
Item item = first.item;
first = first.next;
n--;
if (isEmpty()) last = null; // to avoid loitering
return item;
}

public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}


public Iterator<Item> iterator() {
return new ListIterator<Item>(first);
}

// an iterator, doesn't implement remove() since it's optional
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current;

public ListIterator(Node<Item> first) {
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;
}
}




}

原文地址:https://www.cnblogs.com/wujunde/p/6965258.html