Stack is"Back-in-first-out"(LIFO, Last InFirst Out).First-in-first-out"(FIFO, First InFirst Out) Data Structure
The role of the queue is the same as that of the people standing in front of the ticket gate: the first person in the queue will buy the tickets first, and the last person in the queue will finally be able to buy the tickets.
In computer operating systems or networks, various queues work quietly. The print job is waiting for printing in the print queue. When you press the keyboard, there is also a queue that stores the keyboard-typed content. If we press a key and the computer is currently doing other things, the hit content will not be lost, it will wait in the queue until the computer has time to read it. The queue ensures that the order of the typed content will not change during processing.
The naming methods for Stack insertion and deletion of data items are standard and become push and pop. The queue method has not yet been standardized. insertion can be called put, add, or enque, delete can be called delete, get, remove, or deque.
Queue
Next we still use arrays as the underlying container to encapsulate operations in a queue. Different from the stack, data items in the queue do not start with the first subscript of the array, the smaller the lower mark of the data item in the array indicates that the data item is arranged in front of the queue. The removed data item can only be removed from the header of the queue, and then the header pointer is removed, in this way, the first few positions of the array will be blank, as shown in:
This is the opposite of our intuitive feeling, because when we queue for tickets, the queue always moves forward. After the current person buys the tickets and leaves, others move forward. In our design, the queue does not move forward, because this will greatly reduce the efficiency. We only need to use pointers to mark the head and end of the queue. When the queue changes, move the pointer, the position of the data item remains unchanged.
However, there is still a problem with this design. As the elements in the head of the team are constantly removed, there will be more and more empty positions in front of the array. When the tail pointer moves to the last position, even if the queue is not full, we cannot insert new data items.
To solve this problemLoop Processing, That is, let the team tail pointer return to the first position of the array:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1eK + keys/QzrXEo6y1q8rH1NrC37ytyc/L/keys = "brush: java;"> public class Queue {private int [] queArray; private int maxSize; public int front; // store the subscript public int rear of the team Header element; // store the subscript private int length of the team end element; // Queue length // constructor, initialize the Queue public Queue (int maxSize) {this. maxSize = ma XSize; queArray = new int [maxSize]; front = 0; rear =-1; length = 0;} // insert public void insert (int elem) throwsException {if (isFull () {throw new Exception ("the queue is full and cannot be inserted! ") ;}// If the team end pointer has reached the end of the array, insert it to the first position of the array if (rear = maxSize-1) {rear =-1 ;} queArray [++ rear] = elem; length ++;} // remove public int remove () throws Exception {if (isEmpty ()) {throw new Exception ("the queue is empty and cannot be removed! ");} Int elem = queArray [front ++]; // if the frontend pointer has reached the end of the array, move it to the first position of the array if (front = maxSize) {front = 0;} length --; returnelem;} // view the team Header element public int peek () throws Exception {if (isEmpty ()) {throw new Exception ("no element in the queue! ");} Return queArray [front];} // get the queue length public int size () {return length;} // empty public boolean isEmpty () {return (length = 0);} // public boolean isFull () {return (length = maxSize );}}
Also knownDual-end queueData structure, each end of the queue can be inserted and removed.
In fact, dual-end queues are a combination of queues and stacks. If a segment of a restricted double-ended queue can only be inserted, and the other end can only be removed, it becomes a queue in the general sense. If the restricted double-ended queue can only be inserted and removed at one end, it becomes a stack.
Priority queue
Like a normal queue, a priority queue has one head and one tail, and also removes data from the head and inserts data from the end. The difference is that in a priority queue, data items are sorted by the value of keywords. data items are inserted to the appropriate position in order.
In addition to the ability to quickly access data items with the highest priority, Priority Queues should also be able to implement fast insertion. Therefore, priority queues are usually implemented using a data structure called a heap. In the following example, we still use Arrays for simplicity.
When the number of data items is relatively small, or you are not concerned about the speed, using arrays to implement priority queues can also meet the requirements. If there are many data items or there are high speed requirements, using heap is a better choice.
The implementation of priority queues is significantly different from that of normal queues.
The insertion of priority queue requires moving elements to locate the position where they should be inserted. Therefore, the advantages of loop queue that do not require moving elements are not obvious. In the following example, the first element of the array is always at the end of the team, and the last element of the array is always at the end of the team. Why is it not the opposite? Because the team header has a removal operation, the team header is placed at the end of the array to facilitate removal. If it is placed at the first segment, the queue needs to be moved forward each time the team header is removed.
Insert element
Remove Element
In the following example, we set a benchmark. The higher the distance from the element to the benchmark, the higher the priority. For example, if the distance from 2 to 3 is set to | 3-2 | = 1, the distance from-1 to 2 is |-1-2 | = 3, so the priority of 2 is higher than that of-1.
Public class PriorityQueue {private int [] queArray; private int maxSize; private int length; // queue length private int referencePoint; // reference point // constructor, initialize the public PriorityQueue (int maxSize, intreferencePoint) {this. maxSize = maxSize; this. referencePoint = referencePoint; queArray = new int [maxSize]; length = 0;} // insert public void insert (int elem) throwsException {if (isFull ()) {throw new Exception ("the queue is full and cannot be performed Insert operation! ") ;}// If the queue is empty, insert it to the first position of the array if (length = 0) {queArray [length ++] = elem ;} else {int I; for (I = length; I> 0; I --) {int dis = Math. abs (elem-referencePoint); // The distance from the element to be inserted to int curDis = Math. abs (queArray [I-1]-referencePoint); // distance of the current element // move one if (dis> = curDis) after an element with a higher priority than the inserted element) {queArray [I] = queArray [I-1];} else {break ;}} queArray [I] = elem; length ++ ;}/// remove public int remove () throws Exception {if (isEmpty () {thro W new Exception ("the queue is empty and cannot be removed! ");} Int elem = queArray [-- length]; return elem;} // view the team Header element public int peek () throws Exception {if (isEmpty ()) {throw new Exception ("no element in the queue! ");} Return queArray [length-1];} // return queue length public int size () {return length;} // empty public boolean isEmpty () {return (length = 0);} // public boolean isFull () {return (length = maxSize );}}