Data structure: Queue first-in, first-out data structure description
In the first-in, FIFO data structure, the first element in the queue, the position element referred to by front, is processed first .
As shown, the queue is a typical FIFO data structure. The insert operation is also knownas a queue (enqueue), and the 队列的末尾 new element is always added . The delete operation is also known as the Out of band (dequeue). You only have to 第一个元素 remove .
Sample Enqueue operation
Out of team operation
Queue implementation
To implement the queue, we can use dynamic arrays and indexes that point to the head of the queue .
As described above, the queue should support two operations: Enqueue and outbound. The team appends a new element to the queue, and the first element is deleted. So we need a lead. Indicates the starting point .
"Static void main" must is defined in a public class.class myqueue {//store elements Private List<integer> ; Data A pointer to indicate the start position private int p_start; Public Myqueue () {data = new arraylist<integer> (); P_start = 0; }/** Insert an element into the queue. Return True if the operation is successful. */public Boolean enQueue (int x) {data.add (x); return true; }; /** Delete an element from the queue. Return True if the operation is successful. */Public Boolean deQueue () {if (isEmpty () = = True) {return false; } p_start++; return true; }/** Get the front item from the queue. */public int Front () {return data.get (P_start); }/** Checks whether the queue is empty or not. */Public Boolean isEmpty () {return p_start >= data.size (); }};p Ublic class Main {public static void MaIn (string[] args) {myqueue q = new Myqueue (); Q.enqueue (5); Q.enqueue (3); if (q.isempty () = = False) {System.out.println (Q.front ()); } q.dequeue (); if (q.isempty () = = False) {System.out.println (Q.front ()); } q.dequeue (); if (q.isempty () = = False) {System.out.println (Q.front ()); } }}
The above implementation is simple, but in some cases inefficient. as the starting pointer moves, more and more space is wasted. When we have space constraints, it will be unacceptable .
If we have an element out of the team, then the first position will be empty, and the waste of this space can be solved using the loop queue .
Circular Queue Description
A cyclic queue is a linear data structure whose operation is based on the FIFO (first-in, in/out) principle and the end of the team is connected to the head of the team to form a loop . It is also known as a "ring buffer".
One advantage of the loop queue is that we can take advantage of the space we used before in this queue . In a normal queue, once a queue is full, we cannot insert the next element, even though there is still space in front of the queue. But with circular queues, we can use these spaces to store new values.
Realize
Class Mycircularqueue {private int[] data; private int head; private int tail; private int size; /** Initialize your data structure here. Set the size of the queue to be k * * Public mycircularqueue (int k) {data = new int[k]; head =-1; tail =-1; size = k; }/** Insert an element into the circular queue. Return True if the operation is successful. */public boolean enQueue (int value) {if (isfull () = = True) {return false; } if (isEmpty () = = True) {head = 0; } tail = (tail + 1)% size; Data[tail] = value; return true; }/** Delete an element from the circular queue. Return True if the operation is successful. */Public Boolean deQueue () {if (isEmpty () = = True) {return false; } if (head = = tail) {head =-1; tail =-1; return true; } head = (head + 1)% size; return true; }/** Get the front item from the queue. */public int Front () {if (isEmpty () = = true) {return-1; } return Data[head]; }/** Get the last item from the queue. */public int Rear () {if (isEmpty () = = true) {return-1; } return Data[tail]; }/** Checks whether the circular queue is empty or not. */Public Boolean isEmpty () {return head = =-1; }/** Checks whether the circular queue is full or not. */Public Boolean isfull () {return ((tail + 1)% size) = = head; }}/** * Your Mycircularqueue object would be instantiated and called as such: * mycircularqueue obj = new Mycircularqueue (k ); * Boolean param_1 = Obj.enqueue (value); * Boolean param_2 = Obj.dequeue (); * int param_3 = obj. Front (); * int param_4 = obj. Rear (); * Boolean param_5 = Obj.isempty (); * Boolean param_6 = Obj.isfull (); */Queue usage
In addition to the basic Collection operations, the queue also provides additional insert, Fetch, and check operations. Each of these methods exists in two forms: one throws an exception when the operation fails, and the other Returns a special value (null or false , depending on the action). The latter form of insert operation is specifically used for capacity-constrained queue implementations; in most implementations, the insert operation does not fail.
"Static void main" must is defined in a public class.public class main {public static void Main (string[] args) {
//1. Initialize a queue. queue<integer> q = new LinkedList (); 2. Get the first Element-return null if the queue is empty. System.out.println ("The first element is:" + q.peek ()); 3. Push new element. Q.offer (5); Q.offer (+); Q.offer (8); Q.offer (6); 4. Pop an element. Q.poll (); 5. Get the first element. System.out.println ("The first element is:" + q.peek ()); 7. Get the size of the queue. System.out.println ("The size is:" + q.size ());} }
Data structure: Queues