`
午刀十
  • 浏览: 35004 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

队列及优先级队列

阅读更多
    队列,与栈相反,先进先出:
/**
 * 
 * @Desc This class use to provide a queue which first in first out
 * @author xujp1 
 * @version Revision: 1.00 Date: Feb 17, 2012
 */
class Queue
{

    private final long[] queArray;
    private int          nItem;
    private int          front;
    private int          rear;
    private final int    maxSize;

    public Queue(int s)
    {
        maxSize = s;
        queArray = new long[maxSize];
        front = 0;//队头,删除数据从队头开始
        rear = -1;//队尾,新数据插入队尾
        nItem = 0;//队列的数量
    }

    public void insert(long item)
    {
        if(rear == maxSize - 1)
            rear = -1;
        queArray[++rear] = item;
        nItem++;
    }

    public long remove()
    {
        long temp = queArray[front++];
        if(front == maxSize)
            front = 0;
        nItem--;
        return temp;
    }

    public void showAll()
    {
        if(nItem == 0)
        {
            System.out.println("THERE HAVE NOT DATA");
        }
        else
            if(nItem == 1)
            {
                System.out.println(queArray[rear]);
            }
            else
            {
                if(front > rear)
                { //队尾在队头后面,则先依次输出队头,再依次输出队尾
                    for(int i = front; i < maxSize; i++)
                    {
                        System.out.println(queArray[i]);
                    }
                    for(int j = 0; j <= rear; j++)
                    {
                        System.out.println(queArray[j]);
                    }
                }
                else
                    if(front < rear)
                    { //队尾在对头后面,则依次输出
                        for(int i = front; i <= rear; i++)
                        {
                            System.out.println(queArray[i]);
                        }
                    }
            }
    }

    public boolean isEmpty()
    {
        return (nItem == 0);
    }

    public boolean isFull()
    {
        return (nItem == maxSize);
    }

    public int size()
    {
        return nItem;
    }

    public long peakFront()
    {
        return queArray[front];
    }
}

    优先级队列:
/**
 * 优先级队列与队列唯一的区别是优先级队列内部数据项有序
 * 
 * @Desc This class use to create PriorityQ class
 * @author xujp1 
 * @version Revision: 1.00 Date: Feb 20, 2012
 */
class PriorityQ
{

    private final long[] priorityQ;
    private final int    maxSize;
    private int          nItem;

    public PriorityQ(int s)
    {
        maxSize = s;
        priorityQ = new long[maxSize];
        nItem = 0;
    }

    //remove和insert方法实现在插入后队列即有序
    public long remove()
    {
        return priorityQ[--nItem];
    }

    public void insert(long s)
    {
        int j;
        if(nItem == 0)
        {
            priorityQ[nItem] = s;
        }
        else
        {
            for(j = nItem - 1; j >= 0; j--)
            {
                if(priorityQ[j] < s)
                {
                    priorityQ[j + 1] = priorityQ[j];
                }
                else
                    break;
            }
            priorityQ[j + 1] = s;

        }
        nItem++;
    }

    //下面的remove2和insert2主要以remove2实现优先级队列
    public long remove2()
    {
        int min = 0;
        long minData;
        for(int i = 1; i < nItem; i++)
        {
            min = priorityQ[min] < priorityQ[i] ? min : i;
        }
        minData = priorityQ[min];
        for(int j = min; j < nItem - 1; j++)
        {
            priorityQ[j] = priorityQ[j + 1];
        }
        nItem--;
        return minData;
    }

    public void insert2(long s)
    {
        priorityQ[nItem++] = s;
    }

    public boolean isEmpty()
    {
        return (nItem == 0);
    }
}


注:以上代码均出自《Java数据结构和算法中文第二版》
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics