The first linked structure we learned is linked list. It is one of the cases of the linked structure, which has reference to only one object.
Why do we sometimes use linked list rather than the normal list? I find the answer in the lab, in which we implement linked queue. In the timecheck of the linked queue, I find the time is a constant no matter how many numbers are in the linked queue, i.e. the time complexity is O(1), while the time complexity of the queue based on normal list, is O(n). From this case, we can see that linked structure shows the superiority in the operations such as insert.
I would say something more about the difference between the implementation of linked queue and linked stack. In the linked queue, we have to keep track of both the top and the bottom but in linked stack we only keep track of the top object. The cause of this difference is that we dequeue on the top(by top, I mean the start node) but enqueue on the bottom(by bottom, I mean the end node). I don't think we can dequeue on the bottom and enqueue on the top, because it's always easier to find the children of a node rather than the parents of that node.
Then we did something with the binary tree with linked nodes. The concept is the same to the binary tree based on python list, except that we need to use reference to the node rather than use the subscript of a python list.
Here is a problem for you - how can you mimic (enqueue&dequeue) the queue by having in your use two stacks?
回复删除I'm not using two stacks. I'm using two variables to keep track of the top node and bottom node.
删除That is just a standalone problem for extra practicing...
回复删除