Java implementation of queue data structure code

What is queue structure

A linear structure with a special algorithm [can only be deleted at one end (team head) and inserted at the other end (team tail)].

Classification:

Sequential queue structure chain queue structure

Basic operation:

Queue list queue

Some application queue scenarios are given

1): when jobs are sent to the printer, they can be arranged in the order of arrival, so each job is the node of the queue.

2): people at the ticket booth buy tickets in the order of first come, first buy.

3): when all terminals are occupied, due to limited resources, access requests need to be placed in a queue.

The queue is first in first out!  

We set up a generic collection class called linkqueue < T >, which has node as the internal class (used as a node), which contains the generic element and the next node pointing to the next node.

Set the queue head pointer front and queue tail pointer rear in linkqueue, with length size = 0; We first set a constructor linkqueue () to initialize the two pointer nodes. Of course, at the beginning of initialization, the two pointers are only one node, and the data in them is empty. We also make the two pointers equal.

When we add elements to the queue, a new node will be generated, and its data is the element you want to add. (when adding a node, the node is the last node pointed to by the end of the queue pointer, always in the last row). Therefore, the end of the queue rear. Next = newnode ("newly created node") This is the first and last node, so front next=newNode. Then we let rear = newnode (constantly updated).

When the queue is out of the queue, remember that we have a node that is front Next = newnode? This is the first node. Let's call it p for the moment, so p.next = the second node. At this time, let's call front next=p.next; In this way, the header pointer points to the second element (the queue header pointer will change every time it is called).

So far, the core operation of the queue is completed, and the rest, such as size (length) and isempty (whether it is empty), are no longer mentioned. (because it's too simple!)

The specific source code is as follows:

Another: I have read a book on JavaScript data structure, which is easy to understand. It is very suitable for front-end JS development, which makes people understand more deeply. I recommend it here.

JavaScript description of data structure and algorithm

Java programming uses two stacks to realize queue code sharing

Java programming to realize binary heap code sharing of priority queue

Java programming queue data structure code example

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>