Simple array of data structures

What is a data structure

Baidu Encyclopedia explains it this way:

A data structure is the way a computer stores and organizes data. A data structure is a collection of data elements that have one or more specific relationships with each other.

In fact, if we summarize it, it is really too simple, there is memory, and it is used to store data, it can be understood as a data structure.

Classification of Data Structures

In our development, we also often use data structures, but we don’t care about this term very much, but directly use their other terms, such as:

array

  • linked list
  • heap
  • the stack

The above four number structures can be collectively referred to as a linear table. In addition to linear tables, we also have other data structures, such as hash tables, trees, and graphs.

The hash table has:

  • hash
  • bitmap

The tree has:

  • binary tree
  • multiway tree
  • heap

The graph contains:

  • directed graph
  • Undirected graph
  • weighted graph

Let's not analyze too much content today, let's start with the most familiar ones, so what are we most familiar with? There is no doubt that it is the content in the linear list, because arrays, linked lists, heaps, and stacks are what we often encounter in interviews, and they are also often seen in code, and some students will If you say that you haven't used it at all, then this is a bit unclear. After all, you are using the upper-level encapsulation class, not the specific bottom layer.

Let's talk about arrays first:

array

Array (Array) is an ordered collection of finite variables of the same type, and each variable in the array is called an element. Arrays are the simplest and most commonly used data structure.
insert image description here
Arrays use a set of contiguous memory spaces to store a set of data of the same type.

That is to say, the positions in our memory are all next to each other.
insert image description here
For example, in the figure below, the gray mark is the used memory, while the red grid represents the continuous memory occupied by the array, and the yellow grid represents the free memory. Memory.

The above picture is a schematic diagram of the storage space of the array in the memory. In fact, the main purpose is still amazing. I want to tell you that when the array is stored in the memory, it is a continuous memory space, and there will be no one every other grid.

As for the array, the data can be randomly accessed according to the subscript. In fact, it is random, and this random is not random.

When he said random, he just said random element addressing, not random data fetching.

For example, our int is 4 bytes, that is (32 bits), in fact, the memory stores bits

If random element addressing at this time,

int a[5]

With random element addressing, then

a[i]_address=a[0]_address+i*4

Then we have to know what the memory space of this operation is doing.

The first step is to read the element

The second step is to update the element

Both reading and updating can be accessed randomly. You can guess the time complexity. Welcome to reply at the end of the article.

Then there will be three situations when inserting elements, tail insertion, middle insertion, and out-of-range insertion.

So what is tail insertion?

tail insertion

In fact, the tail teapot is the simplest way to realize it.

When the actual number of elements in the data is less than the length of the array: simply put the inserted element in the free position at the end of the array, which is equivalent to the operation of updating the element

insert in the middle

When the actual number of elements in the data is less than the length of the array: Since each element of the array has its fixed subscript, first move the insertion position and the following elements backward to make room for the element to be inserted, and then place the element to be inserted in to the corresponding array position.

out of bounds insertion

If there is an array full of elements now, and you want to insert a new element, or the insertion position is out of bounds, then you need to expand the original array: you can create a new array with twice the length of the old array , and then copy all the elements in the old array, thus realizing the expansion of the array.

So what are the pros and cons of using arrays?

Advantages and disadvantages of arrays

advantage:

The array has a very efficient random access capability, as long as the subscript is given, the corresponding element can be found in constant time

shortcoming:

Insertion and deletion of elements. Since array elements are stored in memory continuously and tightly, inserting and deleting elements will cause a large number of elements to be forced to move, affecting efficiency. (ArrayList LinkedList) The requested space must be continuous, that is to say, even if there is space, the creation may fail because there is not enough continuous space

If it exceeds the range, you need to re-apply for memory for storage, and the original space will be wasted

Generally speaking, array is the basic data structure, which is widely used. ArrayList, Redis, message queue, etc. all use the data structure of array. This data structure is often combined with other data during interviews. The structure is used for comparison. Have you learned about the array of this data structure?

Guess you like

Origin blog.csdn.net/KRYST4L123/article/details/129799508