JAVA数据结构--队列

简介: JAVA中的队列实现: 1 // Queue.java 2 // demonstrates queue 3 // to run this program: C>java QueueApp 4 ////////////////////////////////////////////...

JAVA中的队列实现:

 1 // Queue.java
 2 // demonstrates queue
 3 // to run this program: C>java QueueApp
 4 ////////////////////////////////////////////////////////////////
 5 class Queue
 6    {
 7    private int maxSize;
 8    private long[] queArray;
 9    private int front;
10    private int rear;
11    private int nItems;
12 //--------------------------------------------------------------
13    public Queue(int s)          // constructor
14       {
15       maxSize = s;
16       queArray = new long[maxSize];
17       front = 0;
18       rear = -1;
19       nItems = 0;
20       }
21 //--------------------------------------------------------------
22    public void insert(long j)   // put item at rear of queue
23       {
24       if(rear == maxSize-1)         // deal with wraparound
25          rear = -1;
26       queArray[++rear] = j;         // increment rear and insert
27       nItems++;                     // one more item
28       }
29 //--------------------------------------------------------------
30    public long remove()         // take item from front of queue
31       {
32       long temp = queArray[front++]; // get value and incr front
33       if(front == maxSize)           // deal with wraparound
34          front = 0;
35       nItems--;                      // one less item
36       return temp;
37       }
38 //--------------------------------------------------------------
39    public long peekFront()      // peek at front of queue
40       {
41       return queArray[front];
42       }
43 //--------------------------------------------------------------
44    public boolean isEmpty()    // true if queue is empty
45       {
46       return (nItems==0);
47       }
48 //--------------------------------------------------------------
49    public boolean isFull()     // true if queue is full
50       {
51       return (nItems==maxSize);
52       }
53 //--------------------------------------------------------------
54    public int size()           // number of items in queue
55       {
56       return nItems;
57       }
58 //--------------------------------------------------------------
59    }  // end class Queue
60 ////////////////////////////////////////////////////////////////
61 class QueueApp
62    {
63    public static void main(String[] args)
64       {
65       Queue theQueue = new Queue(5);  // queue holds 5 items
66 
67       theQueue.insert(10);            // insert 4 items
68       theQueue.insert(20);
69       theQueue.insert(30);
70       theQueue.insert(40);
71 
72       theQueue.remove();              // remove 3 items
73       theQueue.remove();              //    (10, 20, 30)
74       theQueue.remove();
75 
76       theQueue.insert(50);            // insert 4 more items
77       theQueue.insert(60);            //    (wraps around)
78       theQueue.insert(70);
79       theQueue.insert(80);
80 
81       while( !theQueue.isEmpty() )    // remove and display
82          {                            //    all items
83          long n = theQueue.remove();  // (40, 50, 60, 70, 80)
84          System.out.print(n);
85          System.out.print(" ");
86          }
87       System.out.println("");
88       }  // end main()
89    }  // end class QueueApp
90 ////////////////////////////////////////////////////////////////

 

相关文章
|
1天前
|
存储 编译器 C语言
数据结构——顺序队列与链式队列的实现-2
数据结构——顺序队列与链式队列的实现
数据结构——顺序队列与链式队列的实现-2
|
1天前
|
存储 C语言
数据结构——顺序队列与链式队列的实现-1
数据结构——顺序队列与链式队列的实现
数据结构——顺序队列与链式队列的实现-1
|
5天前
栈与队列理解
栈与队列理解
12 1
|
5天前
|
存储 算法
数据结构与算法 栈与队列
数据结构与算法 栈与队列
11 0
数据结构与算法 栈与队列
|
5天前
|
C++
数据结构(顺序队列 循环队列
数据结构(顺序队列 循环队列
9 0
|
6天前
|
容器
【栈与队列】栈与队列的相互转换OJ题
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
10 0
|
8天前
|
C语言
猿创征文|栈和队列OJ刷题
猿创征文|栈和队列OJ刷题
[数据结构]~栈和队列(0-1)
[数据结构]~栈和队列(0-1)
|
14天前
|
存储
栈与队列练习题
栈与队列练习题
|
14天前
数据结构第四课 -----线性表之队列
数据结构第四课 -----线性表之队列