In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the java queue and stack structure of the sample analysis, has a certain reference value, interested friends can refer to, I hope you read this article after a great harvest, the following let Xiaobian take you to understand.
1. Queue structure 1. Basic concepts
Queue is a special linear table, special in that it only allows delete operations at the front of the table (front), and insert operations at the rear of the table (rear), like the stack, queue is an operation restricted linear table. The end where the insertion operation is performed is called the tail of the queue, and the end where the deletion operation is performed is called the head of the queue.
2. Description of characteristics
A queue is an ordered list that can be implemented as an array or linked list, following the first-in, first-out principle. That is, the data that enters the queue first will be taken out first; the data that enters the queue later will be taken out later; that is, the FIFO principle.
Schematic diagram of queue entry:
Schematic diagram of exit queue:
From the above two diagrams, it is not difficult to find some characteristics of the queue structure:
Data that comes in first goes out first;
Data enters at the end of the queue and exits at the head of the queue;
Frequent changes in the subscripts of the queue based on the array description;
The dequeue algorithm can be modulo based on container size;
The core of the queue structure is the judgment algorithm of whether the container is empty or full, that is, the container is empty and cannot be retrieved, and the container is full and cannot be stored again; the algorithm structure is widely adapted in the storage field.
3. Message queue
Message queues are implemented based on the "first-in, first-out" policy in the data structure. Messages are placed in the queue in a queued manner, and then consumed out of the queue:
Sometimes a certain type of message consumption needs to be controlled in sequence, that is, the common ID in the message can be processed by modulo, that is, a certain type of message can be placed in a queue.
4. API use cases
The LinkedList class implements the Queue interface, so you can simulate queue effects based on LinkedList.
import java.util.LinkedList;import java.util.Queue;public class M01_Queue { public static void main(String[] args) { //queue Queue queue = new LinkedList(); queue.add("head") ; queue.add("middle") ; queue.add("tail") ; //size is constantly changing when the team lists data int queueSize = queue.size() ; int loop = 0 ; //Keep coming out of queue according to queue size while (loop < queueSize) { System.out.println(queue.poll()); System.out.println(queue); loop ++ ; } }} II. Stack structure 1. Basic concepts
A stack, also known as a stack, is a linear table with limited operations. Linear tables that restrict insertions and deletions only to the end of the table. This end is called the top of the stack, and the opposite end is called the bottom of the stack. Inserting a new element into a stack is also called pushing, pushing or pushing, which is to put the new element on top of the stack element to make it a new stack top element; deleting an element from a stack is also called popping or pushing, which is to delete the stack top element and make its adjacent element a new stack top element.
2. Description of characteristics
A stack is a first-in-last-out ordered list, adding and deleting can only be operated at the top of the stack, and the other end is a fixed end, called the bottom of the stack.
Stack diagram:
Schematic diagram of stack pulling:
From the above two diagrams, some characteristics of the stack structure are as follows:
Stacks are pushed and pulled through the top of the stack;
The pointer at the bottom of the stack is not moved in and out of the stack;
Move the stack top pointer in and out of the stack;
Based on the definition of stack, it can be seen that the first element placed in the stack is at the bottom of the stack, the last element placed in the stack is at the top of the stack, and the opposite is true for deleting elements from the stack container. The last element placed is deleted first, and the first element placed is deleted last.
3. Recursive application
Stack common applications in Java programming,(1) subroutine calls: before jumping to the subroutine, the address of the next instruction will be stored in the stack, until the subroutine is executed and then the address will be taken out, back to the original program;(2) processing recursive calls: similar to subroutine calls, in addition to storing the address of the next instruction, but also parameters, regional variables and other data stored in the stack.
4. API use cases
Stack API is a subclass of Vector, it implements a standard last-in-first-out stack, stack only defines the default constructor, used to create an empty stack, stack in addition to all methods defined by Vector, but also defines some of its own methods.
import java.util.Stack;public class M02_Stack { public static void main(String[] args) { //into stack Stack stack = new Stack() ; stack.push("First") ; stack.push("Second") ; stack.push("Third") ; int stackSize = stack.size() ; int loop = 0 ; //Keep popping stack according to stack size while (loop < stackSize) { System.out.println(stack.pop()); System.out.println(stack); loop ++ ; } }} Thank you for reading this article carefully. I hope that the article "Example Analysis of Queue and Stack Structure in Java" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.