数据结构3_java---顺序队列,循环队列,优先级队列(链式结构)java

1、顺序队列

顺序队列,遵循先进先出原则,使用java编写

 1 package Main;
 2 
 3 
 4 import java.util.Scanner;
 5 
 6 import javax.lang.model.element.Element;
 7 import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
 8 
 9 /*队列操作*/
10 public class Main{
11     public Object[] quequeelem;
12     public int front;
13     public int rear;
14     public int maxsize;
15     //初始化
16     public Main(int n)
17     {
18         front = 0;
19         rear = 0;
20         maxsize = n;
21         quequeelem = new Object[n];
22     }
23     //清空队列
24     public void clear()
25     {
26         rear = front = 0;
27     }
28     //判断队列是否为空
29     public boolean isEmpty()
30     {
31         if(rear == 0)
32             return true;
33         return false;
34     }
35     //返回队列长度
36     public int length()
37     {
38         return rear-front;
39     }
40     //将数据元素x插入到队列中成为队尾元素
41     //入队
42     public void insert(Object x) throws Exception
43     {
44         if(rear == maxsize)
45         {
46             throw new Exception("the queue is full!");
47         }
48         quequeelem[rear] = x;
49         rear++;
50     }
51     //出队
52     public Object poll() throws Exception
53     {
54         if(rear == front)
55             throw new Exception("the queue is empty!");
56         Object xObject = quequeelem[front];
57         front++;
58         return xObject;
59     }
60     public void print()
61     {
62         if(!isEmpty())
63         for(int i=front;i<rear;i++)
64         {
65             System.out.print(quequeelem[i]+" ");
66         }
67         else {
68             System.out.println("the queue is empty!");
69         }
70         System.out.println();
71     }
72         public static void main(String[] args) throws Exception {
73             int sum,content;
74             Scanner aScanner = new Scanner(System.in);
75             System.out.println("please input the max_number of the queue");
76             sum = aScanner.nextInt();
77             Main aMain = new Main(sum);
78             for(int i=0;i<5;i++)
79             {
80                 content = aScanner.nextInt();
81                 aMain.insert(content);
82             }
83             aMain.print();
84             aMain.poll();
85             aMain.print();
86             
87         }
88 }

2、循环队列

考虑到顺序队列的多次入队和多次出队会造成存储空间却不能进行入队操作的“假溢出”现象,所以使用循环队列。

“假溢出现象”是因为顺序队列的存储单元没有重复使用机制,为了解决顺序队列因数组下标越界而引起的“溢出”问题,可使序列的首位相连

区别于顺序队列:

  此处空出一个存储单元,使队列最多存储maxsize-1个数据元素。

  同时此处的队满判定条件为front = (rear+1)%maxsize;

同时,当输出队列所有内容的时候,i从front开始到rear结束,其中i++变为i=(i+1)%maxsize,此处需要注意

 1 package Main;
 2 
 3 
 4 import java.util.Scanner;
 5 
 6 /*队列操作*/
 7 public class Main{
 8     public Object[] quequeelem;
 9     public int front;
10     public int rear;
11     public int maxsize;
12     //初始化
13     public Main(int n)
14     {
15         front = 0;
16         rear = 0;
17         maxsize = n;
18         quequeelem = new Object[n];
19     }
20     //清空队列
21     public void clear()
22     {
23         rear = front = 0;
24     }
25     //判断队列是否为空
26     public boolean isEmpty()
27     {
28         if(rear == front)
29             return true;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
30         return false;
31     }
32     //返回队列长度
33     public int length()
34     {
35         return (rear-front+maxsize)%maxsize;
36     }
37     //将数据元素x插入到队列中成为队尾元素
38     //入队
39     public void insert(Object x) throws Exception
40     {
41         if((rear+1)%maxsize == front)
42         {
43             throw new Exception("the queue is full!");
44         }
45         quequeelem[rear] = x;
46         rear = (rear+1)%maxsize;
47     }
48     //出队
49     public Object poll() throws Exception
50     {
51         if(rear == front)
52             throw new Exception("the queue is empty!");
53         Object xObject = quequeelem[front];
54         front = (front + 1)%maxsize; 
55         return xObject;
56     }
57     public void print()
58     {
59         if(!isEmpty())
60         for(int i=front;i<rear;i=(i+1)%maxsize)
61         {
62             System.out.print(quequeelem[i]+" ");
63         }
64         else {
65             System.out.println("the queue is empty!");
66         }
67         System.out.println();
68     }
69         public static void main(String[] args) throws Exception {
70             int sum,content;
71             Scanner aScanner = new Scanner(System.in);
72             System.out.println("please input the max_number of the queue");
73             sum = aScanner.nextInt();
74             Main aMain = new Main(sum);
75             for(int i=0;i<5;i++)
76             {
77                 content = aScanner.nextInt();
78                 aMain.insert(content);
79             }
80             aMain.print();
81             aMain.poll();
82             aMain.print();
83             
84         }
85 }

 3、优先级队列

  优先级队列在顺序队列基础上将队列中的数据元素按照关键字的值进行有序排列,优先级队列插入到队列的合适位置按照优先级

  可以采用顺序和链式两种结构,但为了快速的访问优先级高的元素以及快速的插入元素,此处我们采用链式结构

具体内容见代码:

注:数字越小优先级越高,一个结点包含数据域,指针域和优先级数三部分

  1 package Main;
  2 
  3 
  4 import java.util.Scanner;
  5 
  6 /*优先级队列操作
  7  * 链式队列结构*/
  8 public class Main{
  9     //自定义结点类
 10     class Node{
 11         public int priority;                       //结点优先级
 12         public Node next;                          //指针域
 13         public Object xObject;                     //数据域
 14         public Node(Object xObject, int priority)
 15         {
 16             this.priority = priority;
 17             this.xObject = xObject;
 18         }
 19     }
 20     public Node front;
 21     public Node rear;
 22    
 23     //清空队列
 24     public void clear()
 25     {
 26        front = rear = null;
 27     }
 28     //判断队列是否为空
 29     public boolean isEmpty()
 30     {
 31        return front==null;
 32     }
 33     //返回队列长度
 34     public int length()
 35     {
 36         Node aNode = front;
 37         int length = 0;
 38         while(aNode!=null)
 39         {
 40             aNode = aNode.next;
 41             length++;
 42         }
 43         return length;
 44     }
 45     //将数据元素x插入到队列中成为队尾元素
 46     //入队
 47     public void insert(Object x,int priority) throws Exception
 48     {
 49        Node aNode = new Node(x, priority);
 50        if(!isEmpty())
 51        {
 52            Node pNode = front;
 53            Node qNode = front;
 54            //找到插入位置
 55            while(pNode!=null&&pNode.priority<=aNode.priority)
 56            {
 57                qNode = pNode;
 58                pNode = pNode.next;
 59            }
 60            if(pNode==null)                  //插入位置在队尾
 61            {
 62                rear.next = aNode;
 63                rear = aNode;
 64            }else if (pNode == front) {      //插入位置在队首
 65                aNode.next = front;
 66                front = aNode;
 67             }else {                        //插入位置在队中
 68                 qNode.next = aNode;
 69                 aNode.next = pNode;
 70             }
 71        }else {                             //队列为空
 72            front = rear = aNode;
 73        }
 74     }
 75     //出队
 76     public Object poll() throws Exception
 77     {
 78         if(front==null)
 79             return null;
 80         Node aNode = front;
 81         front  =front.next;
 82         if(aNode==rear)
 83         {
 84             rear = null;
 85         }
 86         return aNode.xObject;
 87        
 88     }
 89     //打印队列内容
 90     public void print()
 91     {
 92         Node aNode = front;
 93         if(!isEmpty())
 94         {
 95             while(aNode!=null)
 96             {
 97                 System.out.println(aNode.xObject+" "+aNode.priority);
 98                 aNode = aNode.next;
 99             }
100         }else {
101             System.out.println("the queue is empty!");
102         }
103         
104     }
105      public static void main(String[] args) throws Exception {
106          Main aMain = new Main();
107          aMain.insert(1,20);
108          aMain.insert(2,30);
109          aMain.insert(3,10);
110          aMain.insert(4,50);
111          System.out.println("the sequence of the process services is :");
112          aMain.print();
113          aMain.poll();
114          System.out.println("After one sequence out of the team:");
115          aMain.print();
116     }
117 }

 

posted @ 2018-03-22 12:00  风云傲天  阅读(642)  评论(0编辑  收藏  举报