data structure
Data structures are the way computers store and organize data. Refers to a collection of data elements that have one or more specific relationships with each other
. Usually, carefully selected data structures can lead to higher operating or storage efficiency.
Keep updating, learn one and remember one~
the stack
Stack model:
一端开口 ==栈顶==
| |
| |
| |
| |
| |
|____________________|
一端封闭 ==栈底==
- The process of data entering the stack model is called:push/push
| | | |
| | [数据 A] | [数据 D] | <- 栈顶元素
| | [数据 B] | [数据 C] |
| | [数据 C] | [数据 B] |
| | [数据 D] | [数据 A] | <- 栈底元素
|____________________| |____________________|
- The process of data leaving the stack model is called:pop/pop
The order of popping is from the top element of the stack to the bottom element of the stack
stack is afirst in last outmodel of
queue
Queue model:
一端开口 ==后端==
| |
| |
| |
| |
| |
| |
一端开头 ==前端==
- The process of data entering the queue model from the backend is called:queue
| |
| | [数据 A]
| | [数据 B]
| | [数据 C]
| | [数据 D]
| |
----------------------------------------------------------
入队列方向
\|/ 后端
| |
| [数据 D] |
| [数据 C] |
| [数据 B] |
| [数据 A] |
| |
\|/ 前端
出队列方向
- The process by which data leaves the queue model from the front end is called:out of queue
queue is afirst in first outmodel of
array
Array model:
___0___1___2___3___4___5___6___
| [A] [B] [C] [D] [E] [F] [G] |
|_____________________________|
- The query data is located through the index, and the query time for any data is the same.Queries are fast
- When deleting data, the original data should be deleted, and each subsequent data should be moved forward,Inefficient deletion
- When adding data, each data after the added position is moved backwards, and then the element is added,The addition efficiency is extremely low
Arrays are aFast query, slow addition and deletionmodel of
linked list
Each element of the linked list is called a node
Node model:
[地址1]
________________
| [数据] [地址2] |
|_______________|
-----------------------------------------
地址1:结点的==存储位置(地址)==
数据:存储具体的==数据==
地址2:下一个结点的==地址==
Head node model:
______________
| [head] [^] |
|____________|
-----------------------------------------
^:结点指向==空地址(表示结束)==
Linked list model link process example
- Store a data A for the head node at address 11:
[11]
_______________ ___________
| [head] [11] | --> | [A] [^] |
|_____________| |_________|
- Store another data C, save it at address 37
[11] [37]
_______________ ____________ ___________
| [head] [11] | --> | [A] [37] | --> | [C] [^] |
|_____________| |__________| |_________|
- Store another data D and save it at address 96
[11] [37] [96]
_______________ ____________ ____________ ____________
| [head] [11] | --> | [A] [37] | --> | [C] [96] | --> | [D] [^] |
|_____________| |__________| |__________| |__________|
Linked list model add data process example
Add a data B between data AC and save it at address 54
[11] [37] [96]
_______________ ____________ ____________ ____________
| [head] [11] | --> | [A] [37] | --> | [C] [96] | --> | [D] [^] |
|_____________| |__________| |__________| |__________|
[54]
___________
| [B] [^] |
|_________|
- The next data address corresponding to data B points to data C
[11] [37] [96]
_______________ ____________ ____________ ____________
| [head] [11] | --> | [A] [37] | --> | [C] [96] | --> | [D] [^] |
|_____________| |__________| |__________| |__________|
↑
[54]
____________
| [B] [37] |
|__________|
- The next data address corresponding to data A points to data B
[11] [37] [96]
_______________ ____________ ____________ ____________
| [head] [11] | --> | [A] [54] | | [C] [96] | --> | [D] [^] |
|_____________| |__________| |__________| |__________|
| ↑
| [54]
| ____________
|--------> | [B] [37] |
|__________|
Linked list model delete data process example
Delete data C between data BD
[11] [37] [96]
_______________ ____________ ____________ ____________
| [head] [11] | --> | [A] [54] | | [C] [96] | --> | [D] [^] |
|_____________| |__________| |__________| |__________|
| ↑
| [54]
| ____________
|--------> | [B] [37] |
|__________|
- The next data address corresponding to data B points to data D
[11] [37] [96]
_______________ ____________ ____________ ____________
| [head] [11] | --> | [A] [54] | | [C] [96] | --> | [D] [^] |
|_____________| |__________| |__________| |__________|
| ↑
| [54] |
| ____________ |
|--------> | [B] [96] |-------|
|__________|
- Data C delete
[11] [96]
_______________ ____________ ____________
| [head] [11] | --> | [A] [54] | | [D] [^] |
|_____________| |__________| |__________|
| ↑
| [54] |
| ____________ |
|--------> | [B] [96] |-------|
|__________|
- Query whether the data D exists, must start from the beginning (head) to start the query
- To query the nth data, you must also start from the beginning (head) to start the query
linked list is aFast addition and deletion, slow queryThe model (comparison array)
hash table
Before JDK 8, the bottom layer useddata + linked listRealization, it can be said that it is an array whose elements are linked lists
After JDK 8, when the length is relatively long, the bottom layer is optimized
An example of storing elements in a hash table (how to ensure the uniqueness of elements)
The elements to be stored are:
“hello”
“world”
“java”
“world”
“通信”
“软件”
For the convenience of demonstration, calculate the hash value of each element (the actual computer operation is an independent calculation for each addition):
“hello” ---- 99162322
“world” ---- 113318802
“java” ---- 3254818
“world” ---- 113318802
“通信” ---- 1179395
“软件” ---- 1179395
See the description of the empty parameter construction method of HashSet in the JDK documentation:
HashSet(): Constructs an empty collection; supports HashMap instances with default initial capacity (16) and load factor (0.75).
As for why this loading factor is 0.75, I checked a lot of information at night, and most of them said that it was directly related to the Poisson distribution. Until later, I saw a rebuttal blog, and the links were all placed here. Be curious, keep the spirit of questioning!
Support is directly related to Poisson distribution.
Refutation
According to this we get the model of HashSet:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
_________________________________________________________________________________
| | | | | | | | | | | | | | | | |
|____|____|____|____|____|____|____|____|____|____|____|____|____|____|____|____|
So how to store the hash value of each element in the hash table?
Take the remainder of the hash value of each element by 16, and store the remainder in the
“hello” ---- 99162322 % 16 => 2
“world” ---- 113318802 % 16 => 2
“java” ---- 3254818 % 16 => 2
“world” ---- 113318802 % 16 => 2
“通信” ---- 1179395 % 16 => 3
“软件” ---- 1179395 % 16 => 3
Obviously we only need to use the position numbered 2 and the position numbered 3, so the following demonstration omits the unused parts (actually there are also unused parts)
[2] [3]
___________________________
..... | | | .....
|____________|____________|
- Try adding "hello"
According to the hash value of "hello", its storage location is calculated as 2. At this time, there is no element in the 2nd position, so add the element directly:
[2] [3]
___________________________
| “hello” | |
|_____________|____________|
- Try adding "world"
According to the hash value of "world", its storage location is also 2. At this time, there is already an element in position 2, so it needs to be compared with the existing element, first compare the hash value:
99162322 != 113318802
“hello” “world”
The first step compares and finds that the hash values of the two are different, so the element is added directly, and the element is also stored in the 2nd position, but it is based onlinked listis stored in the form:
[2] [3]
___________________________
| “hello” | |
|______|______|____________|
↑
___________
| “world” |
|_________|
- Try to add "java" Same as the second step, compare the hash value of the element to be added and all existing elements
99162322 != 3254818
“hello” “java”
---------------------------
113318802 != 3254818
“world” “java”
After comparing all existing elements in turn, it is found that the hash values are different, so add elements directly, and also uselinked listis stored in bit 2 of the form:
[2] [3]
___________________________
| “hello” | |
|______|______|____________|
↑
___________
| “world” |
|_________|
↑
___________
| “java” |
|_________|
- Try adding "world"
Same as the second step, compare the hash values of the elements to be added and all existing elements
99162322 != 113318802
“hello” “world”
---------------------------
113318802 == 113318802
“world” “world”
---------------------------
3254818 != 113318802
“java” “world”
After comparing all existing elements in turn, it is found that the hash value has the same value, so continue to compare whether the elements with the same hash value have the same content:
113318802 == 113318802
“world” == “world”
After the comparison, the content is found to be consistent, soSkip adding。
- Try adding "communication"
According to the hash value of "communication", its storage location is calculated as 3. At this time, there is no element in position 3, so add the element directly:
[2] [3]
____________________________
| “hello” | “通信” |
|______|______|____________|
↑
___________
| “world” |
|_________|
↑
___________
| “java” |
|_________|
- Try adding "software"
According to the hash value of the "software", its storage location is also 3. At this time, there is already an element in position 3, so it needs to be compared with the existing element, first compare the hash value:
1179395 == 1179395
“通信” “软件”
After comparison, it is found that the hash values of the two are the same, so continue to compare whether the element contents are the same:
1179395 == 1179395
“通信” != “软件”
The content is found to be different, so elements are added in the form of a linked list:
[2] [3]
____________________________
| “hello” | “通信” |
|______|______|_____|______|
↑ ↑
___________ __________
| “world” | | “软件” |
|_________| |_______|
↑
___________
| “java” |
|_________|
So far, the operation of adding elements to the hash table has been demonstrated