Introduction to Swift data structure
Understanding the advantages and disadvantages of different data structures will be very helpful for our future programming work (for any help, you can go to Baidu or go through the book ). As the first blog in this series, we will review several built-in basic data structures in Swift.
In general, from the relationship between elements, we are used to dividing the data structure into two basic types: sequential storage data structure and sequential storage data structure. The data structure of sequential storage is characterized by two adjacent elements in the logical relationship, which are physically adjacent, that is, the data is stored in the continuous or adjacent memory, for example, arrays, heaps, matrices, and hash tables, while chained storage data structures can have adjacent or non-adjacent storage units. In addition to the data information of the element, the chain stores the information of the next (or previous) element. Typical chain storage structures include linked lists, trees, and graphs. You can combine these two types of data structures to create a more efficient data structure.
I. Ordered Storage Data Structure
The first thing we need to learn is the sequential storage data structure. This data structure is a typical linear data structure, usually based on indexes, and the access to data elements is executed in a certain order.
1. Array
Arrays are the most common data structures that exist in almost all programming languages. A linear array, also known as a one-dimensional array, is the simplest array form. In Swift, arrays are Collection types with indexes starting from 0, stored in sequence, and can be accessed randomly. In addition to one-dimensional arrays, Arrays can also exist in the form of multi-dimensional arrays. For example, a matrix is a typical multi-dimensional array.
(1) array declaration
In Swift, there are three main methods to declare an array: ①. The complete array declaration method, for example, using the generic syntax.Array (2) Use Quick and concise methods, such [Type]; ③ the last method is to use Type inference, that is, the literal syntax of the array. Generally, we use the second method most. Examples of the three declared array methods are as follows:
// Use the complete generic syntax to declare an Array let fullSyntaxForm: Array = ["Guan Yu", "Zhang Fei", "Zhao Yun", "Ma Chao", "Huang Zhong"] // use the quick syntax to declare an array let shorthandSyntaxForm: [String] = ["Guan Yu", "Zhang Fei", "Zhao Yun", "Ma Chao ", "Huang Zhong"] // declare an array using the literal syntax of the array (you do not need to specify the type of the array. The variable type compiler automatically deduce Based on the Value assignment) let typeInferanceSyntaxForm = ["Guan Yu", "Zhang Fei", "Zhao Yun", "Ma Chao", "Huang Zhong"] // The Compiler automatically deduce it as a string array.
Note that the premise of using the type inference function is to explicitly assign an initialization value to the array during the declaration process. In addition, if you do not want to assign values to an array while declaring an empty array, you can use the following syntax:
// Use the complete generic syntax to declare an empty Array var fullSyntaxFor: Array = Array () // The array type is not explicitly specified, but it is specified as String type during initialization var shorthandSyntaxForm = [String] () // when declaring, the array type is specified to be String. during initialization, the type and brackets var shorthandSyntaxForm2 can be declared: [String] = []
(2) accessing elements in the array
There are many ways to access elements in an array. The simplest is to use an array subscript. If you want to retrieve a piece of data in an array, you can also use a range to retrieve the subsequences in it; even if you want to retrieve all the elements in the array, you can also use... In or forEach. Note that when using the array tag, do not cross-border, otherwise the compilation error will be caused:
Var fiveTigerGeneral = ["Guan Yu", "Zhang Fei", "Zhao Yun", "Ma Chao ", "Huang Zhong"] // uses subscript to access the element let item = fiveTigerGeneral [2] in the array. // retrieves the data marked as 2 in the array. // retrieves a subsequence in the array. let items = fiveTigerGeneral [1... 3] // retrieve Zhang Fei, Zhao Yun, and Ma Chao at a time // use... in loops traverse the entire array for item in fiveTigerGeneral {print (item)} // use forEach to traverse the entire array fiveTigerGeneral. forEach {(tiger) in print (tiger )}
(3) add elements to the array
Like accessing elements in an array, there are also many ways to add elements to an array. The specific method depends on where you want to add new elements to the array. If you only want to add the new element to the end of the array, use the append () method. If you want to add the new element to the position specified by the array, USE insert (, :) method:
// Add the fiveTigerGeneral element to the end of the array. append ("Liu Bei") // Add the new element Liu Bei to the end of the array // Add the new element to the position specified by the array fiveTigerGeneral. insert ("Zhuge Liang", at 1) // Add the new element Zhuge Liang at the position where the array index is 1
(4) Delete elements from the array
Likewise, there are several ways to delete an element from an array, which also depends on where you want to delete the element from the array. For example, if you want to delete the first element in the array, you can use the removeFirst () method. If you want to delete the element at the end of the array, you can use the removeLast () method; if you want to delete the element at the specified position in the array, you can use the remove (at:) method. If you want to delete the array, you can use the removeAll () method:
// Delete fiveTigerGeneral, the last element of the prime group. removeLast () // Delete the first element of the array, fiveTigerGeneral. removeFirst () // Delete the fiveTigerGeneral element whose index position is 1. remove (at: 1) // clear the entire array fiveTigerGeneral. removeAll ()
Arrays are widely used. In addition to basic daily storage, they can be used to implement other complex data structures, such as stacks, queues, heaps, and hash tables.
Ii. Chain Storage Data Structure
In a chained storage structure, each data element not only stores data information, but also stores the relationship with the previous or next element. In general, we call the elements in the chain storage structure as nodes. A node generally contains two parts of information. The part that stores the data element information is called the data domain, the part that stores the data directly or the former is the pointer domain, and the information stored in the pointer domain is called the pointer or chain.
Linked lists are further divided into single-chain tables and double-chain tables. The pointer field of a single-chain table stores the information of the next node. the pointer field of a double-chain table not only stores the information of the next node, there is also information about the previous node. Shows the single-link table and double-Link Table nodes:
Node S and N are single-linked table nodes, where node N is the end node of the single-linked table. Because it does not indicate any node, the pointer field is empty. Node D is the node in the double-stranded table. Its pointer field not only indicates the information of the next node, but also the information of the previous node. The structure of a single-chain table in Swift is as follows:
Class program list {// Data field var item: T // pointer field of the node in the single-link table (indicating the information of the next node) var next: Getting list }
This is just a simple description of a Single-linked table node. The complete implementation will be shown in a series of blogs later.