Queue of data structure, detailed knowledge

Keywords: Java data structure Back-end queue

catalog:

  • What is a queue?
  • What are the characteristics of queues?
  • Queue noun description
  • Detailed explanation of queue types
  • Queue operation

Text:

What is a queue?

Queue is a special linear table, which is only allowed at the front of the table( front)Delete, but at the back end of the table( rear)Like the stack, a queue is a linear table with restricted operations. The end of the insertion operation is called the tail of the queue, and the end of the deletion operation is called the head of the queue.

What are the characteristics of queues?

1. Only insert at one end and delete at the other end are allowed
2. Follow FIFO( FIFO)Principle of

Queue noun description

1. Queue element
 The data elements of a queue are also called queue elements
2. Join the team
 The data elements of a queue are also called queue elements
3. Out of the team
 Deleting a queue element from a queue is called dequeuing

Queue type

Sequential queue

definition:

The sequential queue structure must statically allocate or dynamically apply for a continuous storage space, and set two pointers for management. One is the queue head pointer front,It points to the queue head element; the other is the queue tail pointer rear,It points to the storage location of the next queued element.

Operation logic:

Each time an element is inserted at the end of the team, rear Add 1; each time an element is deleted at the team head, front Add 1. With the progress of insert and delete operations, the number of queue elements changes constantly, and the storage space occupied by the queue also moves in the continuous space allocated for the queue structure front=rear When there is no element in the queue, it is called an empty queue rear When it is added beyond the allocated continuous space, the queue cannot insert new elements, but at this time, there is often a large amount of available space that has not been occupied. These spaces are the storage units that have been occupied by queue elements that have been out of the queue.

Overflow in sequential queue:

  1. "Underflow" phenomenon: when the queue is empty, the overflow phenomenon caused by queue operation is made. "Underflow" is a normal phenomenon, which is often used as a condition for program control transfer.
  2. "True overflow" phenomenon: when the queue is full, space overflow occurs during stack operation. "True overflow" is an error state and should be avoided.
  3. "False overflow" phenomenon: the space of deleted elements can never be reused because the head and tail pointers only increase but not decrease in the queue entry and exit operations. When the actual number of elements in the queue is far less than the scale of vector space, the queue entry operation may not be performed because the tail pointer has exceeded the upper bound of vector space. This phenomenon is called "false overflow" phenomenon.

Circular queue

definition
In order to solve the waste of space in the sequential queue, the circular queue is born here, so that the queue space can be reused.
Whether inserting or deleting, once the real pointer increases by 1 or the front pointer increases by 1 beyond the allocated queue space, let it point to the starting position of this continuous space. If you really increase from MaxSize-1 to 0, you can use the remainder operations Real% maxsize and front%MaxSize. In fact, this is to imagine the queue space as a ring space, and the storage units in the ring space are recycled , the queue managed in this way is also called circular queue.
Operation logic
In the circular queue, when the queue is empty, there is front=rear, and when all queue spaces are full, there is also front=rear. In order to distinguish between these two cases, it is specified that the circular queue can only have MaxSize-1 queue elements at most. When there is only one empty storage unit left in the circular queue, the queue is full. Therefore, the condition of queue empty is front=rear, and the condition of queue full is front=rear In case of condition, front = (rear+1)% MaxSize.

Queue operation

Sequential queue operation

code:

/**
 * Sequential queue
 */
System.out.println("**********Sequential queue**************");
SequentialQueue<String> sequentialQueue = new SequentialQueue<>(5, new String[5]);

//Judge whether it is an empty queue
System.out.println("Judge whether it is an empty queue:");
System.out.println(sequentialQueue.isEmpty());

//Join the team
System.out.println("Sequential queue sequential queue hello world!: ");
sequentialQueue.inQueue("sequential ");
sequentialQueue.inQueue("queue ");
sequentialQueue.inQueue("hello ");
sequentialQueue.inQueue("world!");
sequentialQueue.inQueue("1!");
sequentialQueue.display();

//Get queue size
System.out.println("Get queue size:");
System.out.println(sequentialQueue.getSize());

//Get team leader
System.out.println("Get team leader:");
System.out.println(sequentialQueue.getFront());

//Out of the team
System.out.println("Sequential queue, out of the queue once!");
sequentialQueue.outQueue();

//Get queue size
System.out.println("Get queue size:");
System.out.println(sequentialQueue.getSize());

//Get team leader
System.out.println("Get team leader:");
System.out.println(sequentialQueue.getFront());

//Judge whether it is an empty queue
System.out.println("Judge whether it is an empty queue:");
System.out.println(sequentialQueue.isEmpty());

//Display queue
sequentialQueue.display();

sequentialQueue.inQueue("11");
sequentialQueue.inQueue("12");

sequentialQueue.display();

result:

**********Sequential queue**************
Judge whether it is an empty queue:
true
 Sequential queue sequential queue hello world!: 
Show queue:
sequential  
queue  
hello  
world! 
1! 

Get queue size:
5
 Get team leader:
sequential 
Sequential queue, out of the queue once!
Get queue size:
4
 Get team leader:
queue 
Judge whether it is an empty queue:
false
 Show queue:
queue  
hello  
world! 
1! 

Show queue:
queue  
hello  
world! 
1! 

Loop queue practice

code:

/**
 * Circular queue
 */
System.out.println("**********Circular queue**************");
CircularQueue<String> circularQueue = new CircularQueue<>(10, new String[10]);

//Judge whether it is an empty queue
System.out.println("Judge whether it is an empty queue:");
System.out.println(circularQueue.isEmpty());

//Join the team
System.out.println("Loop queue, queue:");
circularQueue.inQueue("circular ");
circularQueue.inQueue("queue ");
circularQueue.inQueue("hello ");
circularQueue.inQueue("world!");
circularQueue.inQueue("5!");
circularQueue.inQueue("6!");
circularQueue.inQueue("7!");
circularQueue.inQueue("8!");
circularQueue.inQueue("9!");
circularQueue.inQueue("10!");
circularQueue.display();

//Get queue size
System.out.println("Get queue size:");
System.out.println(circularQueue.getSize());

//Get team leader
System.out.println("Get team leader:");
System.out.println(circularQueue.getFront());

//Out of the team
System.out.println("Cycle the queue and get out of the queue once!");
circularQueue.outQueue();

//Get queue size
System.out.println("Get queue size:");
System.out.println(circularQueue.getSize());

//Get team leader
System.out.println("Get team leader:");
System.out.println(circularQueue.getFront());

//Judge whether it is an empty queue
System.out.println("Judge whether it is an empty queue:");
System.out.println(circularQueue.isEmpty());

//Display queue
circularQueue.display();

circularQueue.inQueue("10!");
circularQueue.inQueue("11!");
circularQueue.inQueue("12!");
circularQueue.display();

result:

**********Circular queue**************
Judge whether it is an empty queue:
true
 Loop queue, queue:
Show queue:
circular  
queue  
hello  
world! 
5! 
6! 
7! 
8! 
9! 

Get queue size:
9
 Get team leader:
circular 
Cycle the queue and get out of the queue once!
Get queue size:
8
 Get team leader:
queue 
Judge whether it is an empty queue:
false
 Show queue:
queue  
hello  
world! 
5! 
6! 
7! 
8! 
9! 
null 

Show queue:
queue  
hello  
world! 
5! 
6! 
7! 
8! 
9! 
10! 

------—END------

Posted by plaggypig on Mon, 29 Nov 2021 19:47:13 -0800