zoukankan      html  css  js  c++  java
  • 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;
    }
    }




    }

  • 相关阅读:
    NYOJ 527 AC_mm玩dota
    程序员励志小说链接
    android——ListView功能的实现
    调用系统工具
    HDU SPFA算法 Invitation Cards
    nginx sendfile tcp_nopush tcp_nodelay参数解释
    结构体中使用#define定义宏
    HRPlugin For Xcode发布(附源码地址)
    Derby的下载安装和使用,(和JAVA中使用Derby)
    UNIX环境高级编程——进程管理和通信(总结)
  • 原文地址:https://www.cnblogs.com/wujunde/p/6965258.html
Copyright © 2011-2022 走看看