In the words of the university teacher said sister is more important than work ~, work can be changed, sister this ... So... These two months have been busy fall in love, hi, slowly adjust the state of mind, this article on the selection of a simple data structure to chat, saying that there are many data structures are playing combination boxing, such as: Block linked list, block array, of course, there are two-terminal queue, yes, it is the stack and queue of the combination.
One: Concept
We know that the normal queue is a restricted level, the other end of the FIFO form, the stack is one end in and out of the LIFO form, and the two-terminal queue does not have such a restrictive level, that is, we can at both ends of the queue to insert or delete operations.
Two: Coding
1: Define the structure body
Usually, the interior of the queue is implemented using an array, and with two pointer head and tail to point to the interval of the array, in order to make full use of the array space, we will also use% to implement the logical loop array, as shown below.
public class Myqueue
{public
int head;
public int tail;
public int maxSize;
public int size;
Public t[] list;
Public Myqueue ()
{head
= tail = size = 0;
MaxSize = 3;
List = new T[maxsize];
}
One of the notable details here is the Size field, which is to make it easier to count whether the queue is full or if the queue is empty.
2: Team tail Teams
I just said that. The two-terminal queue can be inserted and deleted at both ends of the queue, and note that when we use head and tail pointers, the tail pointer is the next position to the element, and the head pointer points to the current element, so we can push the data from the tail end as long as " Move down one position clockwise.
<summary>
///team
///</summary>
///<param name= "T" ></param>
/// <returns></returns> Public
bool Push_tail (T t)
{
//judge whether the queue is full
if (myqueue.size = = MyQueue.list.Length) return
false;
Myqueue.list[myqueue.tail] = t;
Clockwise rotation
Myqueue.tail = (myqueue.tail + 1)% Myqueue.maxsize;
myqueue.size++;
return true;
}