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++; } //Return to team leader element 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[0]; } //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){ //Find team leader element 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 public void Push_LinkQueue(LinkQueue queue,Object data){ 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 LinkQueueNode newNode=new LinkQueueNode(data,null); //Enter insert operation newNode.next=queue.head.next; //next of the queue top pointer is equal to the new insertion node queue.head.next=newNode; //Queue capacity plus 1 queue.size++; } //Outgoing queue public void Pop_LinkQueue(LinkQueue 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 LinkQueueNode pPrev=queue.head; LinkQueueNode pCurrent=pPrev.next; while (pCurrent.next!=null){ pPrev=pCurrent; pCurrent=pPrev.next; } pPrev.next=null; //Queue capacity minus 1 queue.size--; } //Return to team leader element public Object Front_LinkQueue(LinkQueue queue){ 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 LinkQueueNode pCurrent=queue.head; while (pCurrent.next!=null){ pCurrent=pCurrent.next; } return pCurrent.data; }
Main function:
public static void main(String[] args) { LinkQueueDao linkQueueDao=new LinkQueueDao(); //Initialize queue LinkQueue queue=linkQueueDao.Init_LinkQueue(); //Queue entry linkQueueDao.Push_LinkQueue(queue,"A"); linkQueueDao.Push_LinkQueue(queue,"B"); linkQueueDao.Push_LinkQueue(queue,"C"); linkQueueDao.Push_LinkQueue(queue,"D"); linkQueueDao.Push_LinkQueue(queue,"E"); //Output queue element while(!linkQueueDao.IsEmpty_LinkQueue(queue)){ //Find team leader element Object o=linkQueueDao.Front_LinkQueue(queue); System.out.println(o); //Outgoing queue linkQueueDao.Pop_LinkQueue(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~**