包(Bag)、队列(Queue)、堆栈(Stack)的实现

本文记录用Java来实现包(Bag)、队列(Queue)、堆栈(Stack)

一、包(Bag)

特点: 只进不出

package com.hef.algorithms.fundamentals.bags_queues_stacks;

import java.util.Iterator;

/**
 * FILO   first in last out
 * @param <Item>
 */
public class Bag<Item> implements Iterable<Item>{

    // first node in list
    private Node first;
    private class Node{
        Item item;
        Node next;
    }
    public void add(Item item){
        // same as push() in Stack
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }
    @Override
    public Iterator<Item> iterator() {
        return new ReverseIterator();
    }
    private class ReverseIterator implements Iterator<Item> {

        private Node current = first;
        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}

二、队列(Queue)

先进先出

package com.hef.algorithms.fundamentals.bags_queues_stacks;

import java.util.Iterator;

/**
 * FIFO
 * @param <Item>
 */
public class Queue<Item> implements Iterable<Item>{

    // link to least recently added node
    private Node first;
    // link to most recently added node
    private Node last;
    // number of items on the queue
    private int N;

    private class Node {
        // nested class to define nodes
        Item item;
        Node next;
    }

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

    public void enqueue(Item item){
        // Add item to the end of the list
        Node oldlast = last;
        last = new Node();
        last.item = item;
        last.next = null;
        if (isEmpty()) first = last;
        else oldlast.next = last;
        N ++;
    }
    public Item dequeue(){
        // Remove item from the beginning of the list
        Item item = first.item;
        first = first.next;
        if (isEmpty()) last = null;
        N--;
        return item;
    }
    @Override
    public Iterator<Item> iterator() {
        return new QueueIterator();
    }
    private class QueueIterator implements Iterator<Item> {

        private Node current = first;

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
    public static void main(String[] args) {
        Queue<String> queue = new Queue<>();
        queue.dequeue();
    }
}

三、堆栈(Stack)

package com.hef.algorithms.fundamentals.bags_queues_stacks;

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

public class Stack<Item> implements Iterable<Item>{

    // top of stack (most recently added node)
    private Node first;
    // number of items
    private int N;
    private class Node{
        Item item;
        Node next;
    }
    public boolean isEmpty() { return first == null; }
    public int size() { return N; }

    public void push(Item item){
        // Add item to top of stack
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        N ++;
    }
    public boolean hasNext(){
        return N > 0;
    }
    public Item pop(){
        if(isEmpty()) throw new NoSuchElementException("overflow");
        // Remove item from top of stack
        Item item = first.item;
        first = first.next;
        N --;
        return item;
    }
    public Item peek(){
        if (first == null){
            throw new NoSuchElementException("Stack underflow");
        }else {
            return first.item;
        }
    }
    @Override
    public Iterator<Item> iterator() {
        return new ListIterator();
    }
    private class ListIterator implements Iterator<Item> {

        private int totalSize = N;
        private Node current = first;

        @Override
        public boolean hasNext() {
            if (totalSize == N){
                return current != null;
            }else{
                throw new ConcurrentModificationException();
            }
        }
        @Override
        public Item next() {
            if(totalSize == N){
                Item item = current.item;
                current = current.next;
                return item;
            }else {
                throw new ConcurrentModificationException();
            }

        }
    }
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.push("a");
        stack.push("b");
        stack.push("c");
        System.out.println("size: " + stack.size());
        System.out.println(stack.pop());
        Stack<String>[] stacks = (Stack<String>[]) new Stack[5];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值