Java programming queue data structure code example

Queue is a special linear table, which can only be deleted at the front end of the table and inserted at the back end of the table. The front end of the table is called (front) queue head and the back end of the table is called (rear) queue tail.

Therefore, the queue is very similar to the scene of life. When buying movie tickets in the cinema, people line up. The first person enters the end of the queue, first reaches the head of the queue, buys tickets and enters the cinema. The people in the back queue buy tickets and enter the cinema according to the order of queuing.

Therefore, queue is a first in first out data structure (FIFO).

Programming to achieve the queue in and out of the loop chain queue.

(1) according to the input queue length N and each element value, establish a queue represented by the cyclic chain list of the leading node (cyclic chain queue), set only a tail pointer to the tail node, and then output the values of each element in the queue.

⑵ queue the data element E and output the value of each element in the queue after joining the queue.

(3) take the first element of the cyclic chain queue out of the queue, and output the value of the out of queue element and the value of each element in the queue after leaving the queue.

When inserting data into the queue, the rear arrow goes all the way up and stops after it is inserted into the position of the maximum subscript of the table. The front arrow will also go up when removing data.

This may be a little inconsistent with the actual situation of people buying movie tickets in the cinema. Generally, after buying tickets, people move forward and continue to buy tickets. The team always moves forward.

In the computer, after each data item is deleted in the queue, other data can continue to move, but in this way, when processing large data, it is very inefficient, because every time one data item is deleted, all the remaining data must be moved forward.

When deleting data, the position in front of the front will be left blank, but since the arrows of the rear and front keep going up, the storage space of the front empty unit can not be used.

In order to avoid the situation that the queue is dissatisfied but can not continue to insert new data, the way to solve the recycling of the queue is to move its position to the most initial position of the table after the rear arrow and front arrow reach the position of the maximum subscript.

This is the circular queue:

The results are as follows:

summary

The above is all about the Java programming queue data structure code example in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Java programming to realize binary heap code sharing of priority queue

Java programming uses two stacks to realize queue code sharing

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
分享
二维码
< <上一篇
下一篇>>