# Chat queue

Posted by figo2476 on Wed, 04 Mar 2020 15:25:34 +0100

## Write before:

We talked about the stack in the last article—— What is stack?
I believe that many little friends understand the stack;
So this time, we also use the form of comics to talk about what is the queue;

## Mind mapping: ### What is a queue? Queue is a limited linear table;
The queue can only be inserted at one end and deleted at the other end;

### Characteristics of the queue?   The end that can be inserted is called the end of the queue, and the end that can be deleted is called the head of the queue;
First in, first out, last in, then out;

### Storage structure of the queue:  ### Code implementation:

In this article, we should pay attention to the official account of "time for programmers".
Background reply - data structure source code, which can obtain common data structure code;

Sequential storage of queues: Method class:

```//Join the team
public void Push_SeqQueue(SeqQueue queue, Object data){
if(queue==null){
return;
}
if(data==null){
return;
}
//If the queue capacity is less than or equal to the array capacity
if(queue.size==Max_Size){
return;
}
//Element entry
queue.data[queue.size]=data;
queue.size++;
}

public Object Front_SeqQueue(SeqQueue queue){
if(queue==null){
return null;
}
if(queue.size==0){
return null;
}
//Team head element subscript is 0
return queue.data;
}

//Team out
public void Pop_SeqQueue(SeqQueue queue){
if(queue==null){
return;
}
if(queue.size==0){
return;
}
//All elements need to be moved for outbound operation
for (int i = 0; i < queue.size-1; i++) {
queue.data[i]=queue.data[i+1];
}
queue.size--;
}
```

Main function:

```public static void main(String[] args) {
SeqQueueDao seqQueueDao=new SeqQueueDao();
//Initialize queue
SeqQueue queue=seqQueueDao.Init_SeqQueue();

//Join the team
seqQueueDao.Push_SeqQueue(queue,"A");
seqQueueDao.Push_SeqQueue(queue,"B");
seqQueueDao.Push_SeqQueue(queue,"C");
seqQueueDao.Push_SeqQueue(queue,"D");
seqQueueDao.Push_SeqQueue(queue,"E");

//Team out
while (queue.size>0){
Object o=seqQueueDao.Front_SeqQueue(queue);
System.out.println(o);
//Team out
seqQueueDao.Pop_SeqQueue(queue);
}
}
```

Operation result: Chained storage of queues: Method class:

```//Queue entry
if (queue == null){
return;
}
if (data == null){
return;
}
//Create the newly inserted node, the first element after the queue top pointer, because element insertion or deletion can only be performed at the top of the queue
//Enter insert operation
//next of the queue top pointer is equal to the new insertion node
//Queue capacity plus 1
queue.size++;
}

//Outgoing queue
if (queue == null){
return;
}
if (queue.size == 0){
return;
}
//pPrev refers to the head node, and pCurrent refers to the first element after the head node until pCurrent is empty
while (pCurrent.next!=null){
pPrev=pCurrent;
pCurrent=pPrev.next;
}
pPrev.next=null;
//Queue capacity minus 1
queue.size--;
}

if (queue == null){
return null;
}
if (queue.size == 0){
return null;
}
//The team head element is the element inserted in front of you. Use the loop to find the team head element all the time
while (pCurrent.next!=null){
pCurrent=pCurrent.next;
}
return pCurrent.data;
}
```

Main function:

```public static void main(String[] args) {
//Initialize queue

//Queue entry

//Output queue element
System.out.println(o);
//Outgoing queue
}
}
```

Operation result: Well, I'll share it with you today. Next time I'll give you an introduction to the tree!

```                        **It's not easy to be original. Please pay attention~**
```  Published 295 original articles, won 100 praises and 90000 visitors+

Topics: less