数据结构之图(深度优先搜索和广度优先搜索使用的队列和栈)

本文详细介绍了栈和队列在无向不带权图深度优先搜索和广度优先搜索中的实现方法,包括栈的入栈、出栈、查看栈顶元素操作以及队列的入列、出列、查看队首元素操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package graph;

/**
 * 
 * 用作无向不带权图深度优先搜索用得栈
 * @version  [版本号, 2012-12-25]
 */
public class Stack
{
    /**
     * 栈中得数据,顶点的索引
     */
    private int[] data;
    /**
     * 栈中的元素个数
     */
    private int nElement;
    
    /**
     * <默认构造函数>
     */
    public Stack()
    {
        this(20);
    }
    
    /**
     * <默认构造函数>
     */
    public Stack(int capacity)
    {
        data = new int[capacity];
        nElement = 0;
    }
    
    /**
     * 判断栈是否为空
     * @return
     * @see [类、类#方法、类#成员]
     */
    public boolean isEmpty()
    {
        return nElement == 0;
    }
    
    /**
     * 入栈操作,如果栈满,扩容一倍
     * @param index
     * @see [类、类#方法、类#成员]
     */
    public void push(int index)
    {
        if (nElement == data.length)
        {
            int[] temp = new int[data.length * 2];
            System.arraycopy(data, 0, temp, 0, data.length);
            data = temp;
        }
        data[nElement++] = index;
    }
    
    /**
     * 出栈操作
     * @return
     * @throws Exception 
     * @see [类、类#方法、类#成员]
     */
    public int pop()
        throws Exception
    {
        if (isEmpty())
        {
            throw new Exception("Empty Stack Exception");
        }
        return data[--nElement];
    }
    
    /**
     * 查看栈顶元素操作
     * @return
     * @see [类、类#方法、类#成员]
     */
    public int peek()
    {
        return data[nElement - 1];
    }
}
/*
 * 文 件 名:  Queue.java
 * 修改时间:  2012-12-25
 */
package graph;

/**
 * 用作无向不带权图广度优先搜索用得循环队列队列
 * @version  [版本号, 2012-12-25]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class Queue
{
    /**
     * 队列中得数据,顶点的索引
     */
    private int[] data;
    /**
     * 队列中的元素个数
     */
    private int nElement;
    /**
     * 队首和队尾指针
     */
    private int head, tail;
    
    /**
     * <默认构造函数>
     */
    public Queue()
    {
        this(20);
    }
    
    /**
     * <默认构造函数>
     */
    public Queue(int capacity)
    {
        if (capacity <= 0)
        {
            capacity = 20;
        }
        data = new int[capacity];
        nElement = 0;
        head = tail = -1;
    }
    
    /**
     * 判断队列是否为空
     * @return
     * @see [类、类#方法、类#成员]
     */
    public boolean isEmpty()
    {
        return nElement == 0;
    }
    
    /**
     * 入列操作,如果队列满,扩容一倍
     * @param index
     * @see [类、类#方法、类#成员]
     */
    public void enQueue(int index)
    {
        //空
        if (isEmpty())
        {
            data[nElement++] = index;
            head = tail = 0;
        }
        //满
        else if (nElement == data.length)
        {
            int[] temp = new int[data.length * 2];
            for (int i = 0, current = head; i < nElement; i++, current++)
            {
                if (current >= nElement)
                {
                    current -= nElement;
                }
                temp[i] = data[current];
            }
            data = temp;
            data[nElement++] = index;
            head = 0;
            tail = nElement - 1;
        }
        //正常
        else
        {
            if (tail + 1 >= data.length)
            {
                tail -= data.length;
            }
            data[++tail] = index;
            nElement++;
        }
    }
    
    /**
     * 出列操作
     * @return
     * @throws Exception 
     * @see [类、类#方法、类#成员]
     */
    public int deQueue()
        throws Exception
    {
        if (isEmpty())
        {
            throw new Exception("Empty Queue Exception");
        }
        else
        {
            if (head + 1 >= data.length)
            {
                head -= data.length;
            }
            nElement--;
            return data[head++];
        }
    }
    
    /**
     * 查看队首元素操作
     * @return
     * @see [类、类#方法、类#成员]
     */
    public int peek()
    {
        return data[head];
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值