We may be very vague when talking about stacks and queues, because this is closely related to the storage structure in the computer, and many people are reluctant to learn this. In fact, our method is wrong,All these things are very simple. What's important is that we should not separate them, and we should combine them with examples in our lives, so we can understand them very well.
First, stack
First, let's talk about the stack. To put it bluntly, the stack is actually a container that can store things, but this container is a little special and can only be listed. It is like putting two books in it, after putting the first book in it, you can only press the first book and put it on the first book, the first book can only be obtained after the first book is taken out. Combined with examples in our daily life, we get a very important feature of Stack: the advanced post-release principle.
Another troublesome thing is that pointers are used to indicate the inbound and outbound processes of stacks. This is also quite understandable. Like a book in a library, if I want to get a book, I first need to know where it is to be obtained, and where this address is its pointer, in the stack, I justTopPointer, always pointing to the top element. After stack entryTopAutomatically add one to point to the new element, while the output stack is the opposite.
Ii. Queue
It is actually very simple to mention the queue. In combination with the instances in life, it is a good queue to buy tickets in queue. If you are right, you can buy tickets first and then go out to get on the bus, after queuing, I bought the ticket and got on the bus. This also shows a very important feature of the queue, the first-in-first-out principle.
It is worth noting that the pointer problem also exists in the queue. There is a head pointer and a tail pointer. When the tail Pointer Points to the last element of the queue, the element pointed to by the header pointer has gone out, causing the queue to be idle if its number is small and has no value. However, when there is a data queue, it will cause overflow because there is no place. This is called false overflow, because there is a place where data can be stored, just like the aboveDGraph. Therefore, we made an improvement and made it a loop header and a tail link to solve this problem. For another piece of data, I call the tail pointer to the beginning to continue storage.
Conclusion: The above description makes it easy to think of a point. The data structure is actually very simple, and it can be easily understood as long as it is connected with the actual life. Similarly, data serves to solve problems in life. It comes from life and serves life. This is the purpose.
Stack and queue