数据结构实现循环队列的两种方法

一、包含队列元素nItem方式:

 1 package data.struct.algorithm;
 2 
 3 
 4 //定义队列,包含五个域值
 5 class Queuex {
 6     private int maxSize;
 7     private int[] queueArray;
 8     private int front;
 9     private int rear;
10     private int nItem;
11 
12     public Queuex(int s) {
13         maxSize = s;
14         queueArray = new int[maxSize];
15         front = 0;
16         rear = -1;
17         nItem = 0;
18     }
19 
20     //队列尾部插入元素
21     public void insert(int value) {
22         if(nItem==maxSize){
23             throw new RuntimeException("队列满了,不能插入数据了");
24         }
25         if (rear == maxSize - 1) {
26             rear = -1;
27         }
28         queueArray[++rear] = value;
29         nItem++;
30     }
31 
32     //队头删除元素
33     public int remove() {
34         if(nItem==0){
35             throw new RuntimeException("队列中没有元素了,不能进行删除操作了");
36         }
37         int value = queueArray[front++];
38         if (front == maxSize) {
39             front = 0;
40         }
41         nItem--;
42         return value;
43     }
44 
45     //查看队头元素
46     public int peek() {
47         return queueArray[front];
48     }
49 
50     //判断队列是否为空
51     public boolean isEmpty() {
52         return nItem == 0;
53     }
54 
55     //判断队满
56     public boolean isFull() {
57         return nItem == maxSize;
58     }
59 
60     //获取队列大小
61     public int getSize() {
62         return nItem;
63     }
64 }
65 
66 public class QueueApp {
67 
68     /**
69      * @param args
70      */
71     public static void main(String[] args) {
72 
73         Queuex theQueue=new Queuex(10);
74         theQueue.insert(3);
75         theQueue.insert(13);
76         theQueue.insert(23);
77         theQueue.insert(33);
78         theQueue.insert(43);
79         theQueue.insert(53);
80         System.out.println(theQueue.peek());
81         System.out.println(theQueue.remove());
82         theQueue.insert(63);
83         theQueue.insert(73);
84         theQueue.insert(83);
85         theQueue.insert(93);
86 //        theQueue.insert(103);
87         System.out.println(theQueue.peek());
88         while(!theQueue.isEmpty()){
89             System.out.print(theQueue.remove()+" ");
90         }
91         System.out.println();
92         
93     }
94 
95 }

二、不包含队列元素nItem方式:

 1 package data.struct.algorithm;
 2 
 3 //定义队列,包含四个域值,不包含队列大小nItems
 4 //判断队空:rear==front
 5 //判断队空:(rear+1)%maxSize==front,即队头指针在队尾指针的下一个位置
 6 class Queuey {
 7     private int maxSize;
 8     private int[] queueArray;
 9     private int front;
10     private int rear;
11 
12     public Queuey(int s) {
13         maxSize = s;
14         queueArray = new int[maxSize];
15         front = 0;
16         rear = 0;
17     }
18 
19     // 队列尾部插入元素
20     public void insert(int value) {
21         if (this.isFull()) {
22             throw new RuntimeException("队满");
23         }
24         queueArray[rear] = value;
25         rear=(rear+1)%maxSize;
26     }
27 
28     // 队头删除元素
29     public int remove() {
30         if (this.isEmpty()) {
31             throw new RuntimeException("队列中没有元素了,不能进行删除操作了");
32         }
33         int value = queueArray[front];
34         front = (front + 1) % maxSize;
35         return value;
36     }
37 
38     // 查看队头元素
39     public int peek() {
40         return queueArray[front];
41     }
42 
43     // 判断队列是否为空
44     public boolean isEmpty() {
45         return rear == front;
46     }
47 
48     // 判断队满
49     public boolean isFull() {
50         return (rear + 1) % maxSize == front;
51     }
52 
53     // 获取队列大小
54     public int getSize() {
55         return (rear - front + maxSize) % maxSize;
56     }
57 }
58 
59 public class CopyOfQueueApp {
60 
61     /**
62      * @param args
63      */
64     public static void main(String[] args) {
65 
66         Queuey theQueue = new Queuey(5);
67         theQueue.insert(13);
68         theQueue.insert(23);
69         theQueue.insert(33);
70         theQueue.insert(43);
71 //        theQueue.insert(63);
72         System.out.println(theQueue.getSize());
73         System.out.println(theQueue.remove());
74         System.out.println(theQueue.remove());
75         theQueue.insert(63);
76         theQueue.insert(73);
77         System.out.println(theQueue.peek());
78         while (!theQueue.isEmpty()) {
79             System.out.print(theQueue.remove() + " ");
80         }
81         System.out.println();
82 
83     }
84 
85 }

 三、用链表来实现队列

存放结点:

1 public class Node {
2 
3     public  int data;
4     public Node next;
5     public Node(int data,Node next){
6         this.data=data;
7         this.next=next;
8     }
9 }

定义的链表的操作:

 
 1 public class LinkList {
 2 
 3     private Node head;
 4     private Node tail;
 5     public LinkList(){
 6         head=null;
 7         tail=null;
 8     }
 9     public void insertLast(int data){
10         Node newNode=new Node(data, null);
11         if(isEmpty()){
12             head=newNode;
13         }
14         else {
15             tail.next=newNode;
16         }
17         tail=newNode;
18     }
19     public int deleteHead(){
20         if(head.next==null){
21             tail=null;
22             return head.data;
23         }
24         if(head!=null){
25             
26             Node tempNode=head;
27             head=head.next;
28             return tempNode.data;
29         }
30         else {
31             throw new RuntimeException("不能删除元素了");
32         }
33     }
34     public boolean isEmpty(){
35         return head==null;
36     }
37     
38 }

链表模拟的队列

 1 //用的是insertLast()和deleteHead()方法,即在尾部插入,在链表头删除
 2 public class LinkQueue {
 3 
 4     private LinkList linkList;
 5     public LinkQueue(){
 6         linkList=new LinkList();
 7     }
 8     public void push(int data){
 9         linkList.insertLast(data);
10     }
11     public int pop(){
12         return linkList.deleteHead();
13     }
14     public boolean isEmpty(){
15         return linkList.isEmpty();
16     }
17 }

测试类

 1 public class QueuqTest {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7 
 8         LinkQueue linkQueue=new LinkQueue();
 9         linkQueue.push(10);
10         linkQueue.push(20);
11         linkQueue.push(30);
12         linkQueue.push(40);
13         System.out.println(linkQueue.pop());
14     }
15 }
原文地址:https://www.cnblogs.com/ysw-go/p/5355659.html