Queue (Queue): It is a linear table that restricts insertion operations to only one end of the table and deletion operations to the other end.
Stack (Stack): It is a linear table that limits insertion and deletion operations to only one end of the table.
The differences are as follows:
1. Different rules
1. Queue: First In First Out (First In First Out) FIFO
2. Stack: First In Last Out (First In Last Out) FILO
2. Different restrictions on insertion and deletion operations
1 . Queue: Inserts can only be made at one end of the table, and deletions can be made at the other end of the table.
2. Stack: Insertion and deletion can only be done at one end of the table.
3. Different data traversal speeds
1. Queue: Traverse based on address pointer, and can be traversed from the head or tail, but not at the same time, no need Open up space, because the data structure is not affected during the traversal process, so the traversal speed is faster.
2. Stack: Data can only be fetched from the top. That is to say, the first thing that enters the bottom of the stack needs to be traversed through the entire stack before it can be taken out. Moreover, while traversing the data, it is necessary to open up temporary space for the data and keep the data. Consistency before traversal.
Many java training videos, all on the PHP Chinese website, welcome to learn online!
The above is the detailed content of What is the difference between java queue and stack. For more information, please follow other related articles on the PHP Chinese website!