Stack&Queue

Stack

LIFO last in first out

package algs;

import java.util.Iterator;

/*
 * LIFO
 * Last In First Out
 */
public class Stack<Item> implements Iterable<Item>{
    private class Node{
        Item item;
        Node next;
    }
    private Node first;
    private int N;//元素数量

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

    public int size(){return N;}

    //
    public void push(Item item){
        Node oldFirst=first;
        first=new Node();//need create a Node object assign to first.
        first.item=item;
        first.next=oldFirst;
        N++;
    }

    public Item pop(){
        Item item=first.item;
        first=first.next;
        N--;
        return item;
    }
    public Iterator<Item> iterator(){
        return new ListIterator();
    }
    private class ListIterator implements Iterator<Item>{
        private Node current=first;
        public boolean hasNext(){
            return current!=null;
        }
        public void remove(){

        }
        public Item next(){
            Item item=current.item;
            current=current.next;
            return item;
        }
    }
    public static void main(String[] args){
        Stack<String> stack=new Stack<String>();
        String[] s="to be or not to - be - - that - - - is".split(" ");
        for(String m:s) System.out.print(m);
        System.out.println();
        for(String x:s){
            if(!x.equals("-"))
            stack.push(x);
            else if(!stack.isEmpty()) System.out.print(stack.pop()+" ");
        }
        System.out.println("("+stack.size()+" left on stack)");
    }
}

Queue

FIFO first In first out

package algs;

import java.util.Iterator;

/*
 * FIFO
 * First In First Out
 */
public class Queue<T> implements Iterable<T> {

    private Node first;// point to the front of the queue
    private Node last;//point to the end of the queue
    private int N;//the length of the queue;
    private class Node{
        T item;
        Node next;
    }

    public boolean isEmpty(){return N==0;}
    public int size(){return N;}
    //
    public void enqueue(T item){
        Node oldLast=last;
        last=new Node();
        last.item=item;
        last.next=null;
        if(isEmpty()) first=last;
        else oldLast.next=last;
        N++;
    }
    public T dequeue(){
        T item=first.item;
        first=first.next;
        if(isEmpty()) last=null;
        N--;
        return item;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Queue<String> q=new Queue<String>();
        String[] s="to be or not to - be - - that - - - is".split(" ");
        for(String x:s){
            if(!x.equals("-"))
                q.enqueue(x);
            else if(!q.isEmpty()) System.out.print(q.dequeue()+" ");
        }
        System.out.println("("+q.size()+" left on queue)");
    }

    @Override
    public Iterator<T> iterator() {
        // TODO Auto-generated method stub

        return null;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值