Programming community primary school
I do not say that all api, api want this thing with his help @list on the line.
First, the characteristics
Type list ordered repeat structure where the lift is not refers to the size or number in descending order according a rule, but rather sequentially into elements.
Second, the positive and negative index
such as
lpush lp a b c d e f
# lrange的结果是f e d c b a
lrange lp 0 -1
# 得到b a
lrange lp -2 -1
Two knowledge points
- Abcdef out into the reverse is true, because lpush used, each time to the left put.
- lrange display element actually supports 0-1 / -2-1 this negative, precisely because of the negative index.
Types of | a | b | c | d | e | f |
---|---|---|---|---|---|---|
Forward Index | 5 | 4 | 3 | 2 | 1 | 0 |
Negative index | -1 | -2 | -3 | -4 | -5 | -6 |
三, fire
1、lindex
1.1, the concept of
Syntax: lindex key index
find the n-th element, does not know how long the list, but want to find the last element, then the direct use of negative index lindex key -1
can, looking for the penultimate element is thelindex key -1
1.2 demo
Positive and negative control index table, quite right.
2, lset
2.1, the concept of
The new value of the setting value of the index element. For example, I want to set the key of the last element is xxx, then
lset key -1 xxx
2.2 demo
3, other api
Own help @list
view.
Fourth, the simulation data structure
1, analog Stack
With the command stack can be simulated, such as lpush + lpop equivalent to last out, last in first out.
2, the analog queue
Reverse command queue can be simulated, such as lpush + rpop corresponds FIFO.
3, analog array
lindex + lset which are based on the standard operation, the array function can be achieved.
4, the analog blocking unicast queue
blpop, many real-world business scenarios there is also the use of simulation blpop blocking queue, similar to the simple mq function.
V. Summary
- Feature
- Positive and negative index
- Common api
- Analog data structure