Java 实现“数据结构”中的字符串模式匹配BF算法、循环队列、链队列、顺序栈、共享栈、链栈

概念
数据结构:所谓数据结构就是存在一定关系的数据元素的集合。data-struct=(D,R),D表示数据元素,例如(a1,a2,a3,…),R表示关系,关系可以为一对一,一对多,多对多,以及相互没映射关系(也可以看做是一种关系)。所以数据结构有按照逻辑关系来则有“集合(没映射关系)”、“线性表(一对一)”、“树(一对多)”、“图(多对多)”。
程序:所谓程序就是数据结构+算法。
好了,不瞎扯些没用的了,下面是文章的正题
1.栈的示例图
这里写图片描述
2.队列的示例图
这里写图片描述
字符串模式匹配、循环队列、链队列、顺序栈、共享栈、链栈的具体实现

package sss;

/**
 * 工具类
 * <ul><li>字符串匹配<li>循环队列<li>链队列<li>顺序栈<li>共享栈<li>链栈</ul>
 * @author name:zf Email:[email protected]
 *
 */
public class Tool {
    /**
     * 
     * <p>匹配字符串,匹配成功则返回模板的字符串的下标,未匹配到则返回0。</p>
     * <p>匹配使用的朴素匹配模式</p>
     * @param sourceStr 母字符串
     * @param targetStr 需要匹配的字符串
     * @return <b>!0</b>:匹配成功!母字符串的下标,<p><b>0</b>:未匹配成功!表示未匹配到字符串</p>
     */
    public static int matchStringBySimple(String sourceStr,String targetStr){
        int i=0,j=0;
        char [] sourceChar=new char[sourceStr.length()+1],targetChar=new char[targetStr.length()+1];
        sourceStr.getChars(0, sourceStr.length(), sourceChar, 0);
        targetStr.getChars(0, targetStr.length(), targetChar, 0);
        sourceChar[sourceChar.length-1]='\0';
        targetChar[targetChar.length-1]='\0';
        while(sourceChar[i]!='\0' && targetChar[j]!='\0'){
            if(sourceChar[i]==targetChar[j]){
                i++;j++;
            }else{
                i=i-j+1;j=0;
            }
        }
        if(targetChar[j]=='\0'){return i-j+1;}
        return 0;
    }


    /**
     * <p>得到循环队列
     * <P>使用的数组实现
     * @param length 循环队列的容量大小
     * @return 循环队列
     */
    public static CicleQueue getCicleQueue(int length){
        CicleQueue cicleQueue=new CicleQueue(length);
        return cicleQueue;
    }

    /**
     * <p>得到链队列
     * <P>使用的链表实现
     * @return 链队列
     */
    public static ChainQueue getChainQueue(){
        ChainQueue chainQueue=new ChainQueue();
        return chainQueue;
    }


    /**
     * <p>获取顺序栈
     * <p>使用数组实现
     * @param length 构造顺序栈的容量大小
     * @return 顺序栈
     */
    public static OrderStack getOrderStack(int length){
        OrderStack orderStack=new OrderStack(length);
        return orderStack;
    }

    /**
     * <p>获取共享栈
     * <p>使用数组实现
     * @param length 构造共享栈的容器大小
     * @return 共享栈
     */
    public static ShareStack getShareStack(int length){
        ShareStack shareStack=new ShareStack(length);
        return shareStack;
    }

    /**
     * <p>获取链栈
     * <p>使用链表实现
     * @return 链栈
     */
    public static ChainStack getChainStack(){
        ChainStack chainStack=new ChainStack();
        return chainStack;
    }
}

/**
 * 循环队列
 * @author name:zf Email:[email protected]
 *
 */
class CicleQueue{
    /**
     * 队头
     */
    private int head;
    /**
     * 队尾
     */
    private int foot;
    /**
     * 循环队列容器
     */
    private Object [] cicleQueue;
    /**
     * 队列当前存储元素个数
     */
    private int size;

    /**
     * 构造循环队列
     * @param length 构建队列的长度大小
     */
    public CicleQueue(int length){
        ++length;
        this.cicleQueue=new Object[length];
        head=foot=length-1;
    }

    /**
     * 入队列
     * <P>循环队列插入数据元素
     * @param o 需要插入队列的数据元素
     * @throws Exception 队列容量已经达到最大。无法插入数据元素
     */
    public void insert(Object o) throws Exception{
        if((foot+1)%cicleQueue.length==head){
            throw new Exception("队列已满!上溢出异常");
        }
        foot=(foot+1)%cicleQueue.length;
        cicleQueue[foot]=o;
        size++;
    }

    /**
     * 出队列
     * <p>循环队列移除数据元素
     * @return 对头数据元素
     * @throws Exception 队列已经为null,无法再取队头数据元素
     */
    public Object outOfQueue() throws Exception{
        if(head==foot){
            throw new Exception("队列已空!下溢出异常");
        }
        head=(head+1)%cicleQueue.length;
        Object v= cicleQueue[head];
        size--;
        return  v;
    }

    /**
     * 获取队头数据元素
     * <p>从循环队列中取队头数据元素,同时<b>不</b>移除队头元素
     * @return 队头元素
     */
    public Object getQueueHeadValue(){
        if(head==foot){
            return null;
        }
        int i=(head+1)%cicleQueue.length;
        Object v= cicleQueue[i];
        return  v;
    }

    /**
     * 获取当前循环队列数据元素的个数
     * @return 当前数据元素个数
     */
    public int getCurentSize(){
        return this.size;
    }

    /**
     * 获取循环队列的总容量大小
     * @return 队列容器的容量大小
     */
    public int getQueueLength(){
        return this.cicleQueue.length-1;
    }

    /**
     * 判断当前队列是否为空
     * @return <b>true</b>:队列不存在数据元素<b><p><b>false</b>:队列存在数据元素</p>
     */
    public boolean isEmpty(){
        if(head==foot){
            return true;
        }
        return false;
    }

    /**
     * 判断当前队列是否已经为满队列
     * @return <b>true</b>:队列已经满数据元素<b><p><b>false</b>:队列未满</p>
     */
    public boolean isFull(){
        if((foot+1)%cicleQueue.length==head){
            return true;
        }
        return false;
    }
}




/**
 * 链队列
 * @author name:zf Email:[email protected]
 */
class ChainQueue{
    /**
     * 队头结点
     */
    private ChainNode head;
    /**
     * 队尾结点
     */
    private ChainNode foot;
    /**
     * 队列中当前结点个数
     */
    private int size;

    /**
     * 构造链队列
     */
    public ChainQueue() {
        super();
        head=foot=new ChainNode(null);
    }

    /**
     * 入队列
     * <p>链队列添加数据元素
     * @param node 结点对象的数据
     */
    public void insert(Object node){
        ChainNode next=new ChainNode(node);
        next.setNext(null);
        foot.setNext(next);
        foot=next;
        size++;
    }

    /**
     * 出队列
     * <p>链队列移除数据元素
     * @return 数据元素
     * @throws Exception 队列已经为null,无法再取队头数据元素
     */
    public Object outOfQueue() throws Exception{
        if(head==foot){
            throw new Exception("队列已空!下溢出异常");
        }
        head=head.getNext();
        size--;
        return head.getData();
    }

    /**
     * 获取当前链队列数据元素的个数
     * @return 当前数据元素个数
     */
    public int getCurentSize(){
        return this.size;
    }

    /**
     * 判断当前队列是否为空
     * @return <b>true</b>:队列不存在数据元素<b><p><b>false</b>:队列存在数据元素</p>
     */
    public boolean isEmpty(){
        if(head==foot){
            return true;
        }
        return false;
    }

}

/**
 * 链结点
 * @author name:zf Email:[email protected]
 */
class ChainNode{
    /**
     * 结点数据
     */
    private Object data;
    /**
     * 结点的后继节点
     */
    private ChainNode next;

    public ChainNode(Object data,ChainNode next){
        this.data=data;
        this.next=next;
    }
    public ChainNode(Object data){
        this.data=data;
    }

    public void setNext(ChainNode next){
        this.next=next;
    }

    public ChainNode getNext(){
        return this.next;
    }

    public Object getData(){
        return this.data;
    }
    public void setData(Object data){
        this.data=data;
    }
}


/**
 * 顺序栈
 * @author name:zf Email:[email protected]
 */
class OrderStack{
    /**
     * 栈顶下标
     */
    private int top;
    /**
     * 顺序栈容器
     */
    private Object [] orderStack;
    /**
     * 当前栈内元素个数
     */
    private int size;

    /**
     * 构造顺序栈
     * @param length 初始栈的容量大小
     */
    public OrderStack(int length) {
        super();
        top=-1;
        orderStack=new Object[length];
    }

    /**
     * 入栈
     * @param o 需要入栈的数据元素
     * @throws Exception 栈已经为满栈,无法再进行入栈操作
     */
    public void insert(Object o) throws Exception{
        if(top==orderStack.length-1){
            throw new Exception("栈满,无法再入栈数据元素");
        }
        orderStack[++top]=o;
    }

    /**
     * 出栈
     * @return 栈顶数据元素
     * @throws Exception 栈已经为空,无法再获取栈内元素
     */
    public Object outOfStack() throws Exception{
        if(top==-1){
            throw new Exception("栈空,无法再获取栈数据元素");
        }
        return orderStack[top--];
    }

    /**
     * 获取当前栈元素个数
     * @return 栈当前元素个数
     */
    public int getCurentSize(){
        return this.size;
    }

    /**
     * 获取顺序栈的总容量大小
     * @return 栈的容量大小
     */
    public int getStackLength(){
        return this.orderStack.length;
    }

    /**
     * 判断当前栈是否为空
     * @return <b>true</b>:栈不存在数据元素<b><p><b>false</b>:栈存在数据元素</p>
     */
    public boolean isEmpty(){
        if(top==-1){
            return true;
        }
        return false;
    }

    /**
     * 判断当前栈是否为满栈
     * @return <b>true</b>:栈已经满数据元素<b><p><b>false</b>:栈未满</p>
     */
    public boolean isFull(){
        if(top==orderStack.length-1){
            return true;
        }
        return false;
    }
}


/**
 * 共享栈
 * @author name:zf Email:[email protected]
 */
class ShareStack{
    /**
     * 共享栈类型1 的栈顶下标
     */
    private int top1;
    /**
     * 共享栈类型2 的栈顶下标
     */
    private int top2;
    /**
     * 共享栈的容器
     */
    private Object [] shareStack;
    /**
     * 栈端类型1的数据元素个数
     */
    private int size1;
    /**
     * 栈端类型2的数据元素个数
     */
    private int size2;

    /**
     * 栈端类型1
     */
    public static final int S_TYPE1=1;
    /**
     * 栈端类型2
     */
    public static final int S_TYPE2=2;

    /**
     * 构造共享栈
     * @param length 共享栈的初始容器大小
     */
    public ShareStack(int length) {
        super();
        top1=-1;
        top2=length;
        shareStack=new Object[length];
    }

    /**
     * 入栈
     * @param type 共享栈中的类型 <em>S_TYPE1</em>:共享栈的一端类型<em>S_TYPE2</em>:共享栈的一端类型
     * @param o 入栈的数据元素
     * @throws Exception 栈满,无法再进行入栈操作
     */
    public void insert(int type,Object o) throws Exception{
        if(type==1){
            if(top1==top2-1){
                throw new Exception("栈满,无法再插入数据元素");
            }else{
                shareStack[++top1]=o;
                size1++;
            }
        }else{
            if(top2==top1+1){
                throw new Exception("栈满,无法再插入数据元素");
            }else{
                shareStack[--top2]=o;
                size2++;
            }
        }
    }

    /**
     * 出栈
     * @param type 出栈类型
     * @return 栈顶数据元素
     * @throws Exception 栈空,无法再取栈内元素
     */
    public Object outOfStack(int type) throws Exception{
        if(type==1){
            if(top1==-1){
                throw new Exception("栈空,无法再获取数据元素");
            }else{
                size1--;
                return shareStack[top1--];
            }
        }else{
            if(top2==shareStack.length){
                throw new Exception("栈空,无法再获取数据元素");
            }else{
                size2--;
                return shareStack[top2++];
            }
        }
    }

    /**
     * 获取当前栈内元素个数
     * @param type 栈端的类型
     * @return 当前站内元素个数
     */
    public int getCurentSize(int type){
        if(type==1){
            return size1;
        }else{
            return size2;
        }
    }

    /**
     * 栈是否为空
     * @param type 栈端类型
     * @return <b>true</b>:栈不存在数据元素<b><p><b>false</b>:栈存在数据元素</p>
     */
    public boolean isEmpty(int type){
        if(type==1){
            if(top1==-1){
                return true;
            }
            return false;
        }else{
            if(top2==shareStack.length){
                return true;
            }
            return false;
        }
    }

    /**
     * 是否满栈
     * @param type 栈端类型
     * @return <b>true</b>:栈为满栈<b><p><b>false</b>:栈未满</p>
     */
    public boolean isFull(int type){
        if(type==1){
            if(top1==top2-1){
                return true;
            }
            return false;
        }else{
            if(top2==top1+1){
                return true;
            }
            return false;
        }
    }
}

/**
 * 链栈
 * @author name:zf Email:[email protected]
 */
class ChainStack{
    private ChainNode top;
    private int size;

    public ChainStack() {
        super();
        this.top=new ChainNode(null,null);
    }

    /**
     * 入栈
     * @param o 入栈的数据元素
     */
    public void insert(Object o){
        ChainNode next=new ChainNode(o);
        top.setNext(next);
        top=next;
        size++;
    }
    /**
     * 出栈
     * @return 栈顶数据元素
     * @throws Exception 栈为null,无法再获取数据元素操作
     */
    public Object outOfStack() throws Exception{
        if(top.getNext()==null){
            throw new Exception("链栈为空,无法再获取数据元素");
        }else{
            top=top.getNext();
            size--;
            return top.getData();
        }
    }

    /**
     * 获取当前栈内元素个数
     * @return 当前栈内元素个数
     */
    public int getCurentSize(){
        return this.size;
    }

    /**
     * 栈是否为空
     * @return <b>true</b>:栈不存在数据元素<b><p><b>false</b>:栈存在数据元素</p>
     */
    public boolean isEmpty(){
        if(top.getNext()==null){
            return true;
        }else{
            return false;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42914296/article/details/81508822