课程: Advanced Go Programming: Data Structures, Code Architecture, and Testing
Common data structures - Go教程
课程: Advanced Go Programming: Data Structures, Code Architecture, and Testing
Common data structures
- [Instructor] Data structures offer us efficient ways to store and access data. Let's dive right in by exploring the behavior of five of the most common data structures. Understanding how these structures behave is an important part of leveling up your programming skills. The simplest data structures are arrays and slices. We'll be using these built-in types often. Arrays are fixed size, contiguous blocks of memory that contain a single data type. Elements are accessed by index, starting with zero. Once the array is full, you need to create a new array and copy the elements to the new array in order to extend it. Slices are dynamic, flexible views of arrays. They are specified by passing a start and stop index to an existing array. Just like arrays, elements are accessed by index but resizing is done automatically when elements are added. we'll have a closer look at the intricacies of slices and arrays a little later. Arrays and slices are contiguous blocks of memory, which makes it possible to access elements by index. While accessing elements by index can be convenient. Copying and expanding the underlying array when it's full is expensive. Linked lists do not have this issue. Linked lists are composed of nodes and each holds a reference to the next node in the list. Lists are traversed from the top, known as the head. The list ends with a null next node reference signaling that there are no more elements in the list. Hash tables make use of a hash function to map up value to its corresponding table index. They're essentially a collection of key value pairs that are stored in an array at the index computed by the hash function. If two keys hash to the same index, a collision occurs, and the hash table needs to resolve it using a collision resolution technique. This data structure provides fast access to elements because it doesn't require almost any traversal to find a particular element. Graphs consist of a set of vertices or nodes and a set of edges that connect pairs of vertices. Graphs are used to represent relationships between data in a more flexible way than was possible with the data structures we have seen so far. They're useful for modeling a variety of problems such as networks and any kind of path modeling. Now that you have an overview of some of the common data structures and their behavior, I'll help you choose which one to use based on the data we are using and the problems we are trying to solve.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。