Today in programming, there are various types of data structures, and a self-respecting specialist should know about each. This will improve the quality of work and save a lot of time.
One or another type of data structure is chosen depending on what goal is to be achieved. Some formats for organizing and storing data are used in solving simple problems, while others are more complex.
Concept of data structure
All people use different data in the course of their life. They are used in a wide variety of professions and fields.
Within programming, data structures are special containers. They store information in a specific format that gives the structure certain properties. It is these qualities that distinguish one data structure from another. In addition, they determine its suitability for certain application scenarios.
You can interact with the structure in many ways: add data, retrieve it, and process it (modify, analyze, sort, etc.).
Each structure has its algorithm, so the programmer must choose an already created one or develop his own.
Structures differ in that any data unit can be found in a certain place. To determine this place, you need to know the nuances of a particular structure.
As a rule, elements can be added to and removed from the structure. However, this is only sometimes the case. Some structures cannot be adjusted after creation.
The structure can include a set of data and an array of one element.
Classification of data structures
There are physical and logical structures. Physical reflects the way data is presented in the computer memory. Because of this, they are sometimes called internal.
Types of data structures by composition:
- Simple. They are not divided into parts that are larger than bits. The size and layout of the structure in the device memory are well-defined for a simple type.
- Complex (integrated). They include other structures, which can be simple or complex.
Types of data structures by the presence of a connection:
- disconnected structures: arrays, vectors, strings, stacks (Last In, First Out), queues (First In, First Out);
- linked structures (for example, linked lists).
It is impossible not to mention the so-called variability. We are talking about changing the number of elements or relationships between them. Depending on the level of variability, there are:
Depending on the sign of the ordering of elements, two types of data organization structures are distinguished:
- Non-linear: trees, graphs, multi-linked lists.
- Linear. Depending on the type of components in the device memory, they can have a sequential distribution (strings, vectors, arrays, stacks, queues) and an arbitrary connected distribution (singly and doubly linked lists).
When specifying a data type, the following parameters are defined:
- the amount of memory that is needed for a particular structure;
- how the structure is placed in the memory;
- values that can be applied to this data type;
- supported operations.
Now let’s talk about the most important data structures with which you can solve certain problems.
Basic types of data structures
An array is a very common structure, distinguished by its simplicity. Each element in the array corresponds to a positive integer – an index. It indicates the location of the element. In almost all programming languages, indexes start from zero (this approach is called zero-based numbering).
There are two types of arrays:
- One-dimensional – the simplest linear structures.
- Multidimensional – nested structures that consist of other arrays.
How are they used? Due to their simplicity, these constituent elements of more complex structures (stacks, queues, etc.) are used to store simple related data. They are also needed for various sorting algorithms (insert sort, bubble sort, etc.).
The size of an ordinary array is specified in advance. Thanks to this, a person immediately understands how many indices it includes. A dynamic array is different because its size is not predetermined; it can change.
You must set the maximum value and number of filled elements during the creation process. If new elements are added, they are first filled to the maximum. If the allowed value is exceeded, a new array with an increased size limit is generated.
You can add as many elements as you like to a dynamic array. The location is also chosen arbitrarily. But keep in mind that if you insert them in the middle, the rest of the elements need to be moved. This usually takes a lot of time. Based on this, adding elements to the end of such an array is recommended.
How are they used? Dynamic arrays play the role of blocks for data structures. They are used to store an indefinite number of elements.
The description of this type of data structure will be as follows. A linked list is a set of elements (nodes) in a linear sequential structure. A node is a simple object with two properties: variables to store data and the memory address of the next node in the list. A node stores information about the type of data it contains, as well as it’s neighbor. Thanks to this, you can create linked lists where all nodes will be connected.
There are several types of lists:
- Single-linked. Elements can only be traversed in the forward direction.
- Biconnected. In addition to the direct direction, the reverse direction is also allowed. Nodes include an additional pointer (prev) that points to the previous node.
- Circular connected. This is the name of linked lists in which the previous (prev) pointer of the “head” points to the “tail,” and the next pointer of the “tail” points to the “head.”
How are they used? Linked lists play the role of building blocks of complex data structures (queues, stacks, and some varieties of graphs). In dynamic structures, they are needed to allocate memory and, in the OS, simplify tab switching. In addition, they are used in image slideshows since the pictures go one by one.
A stack is a linear data structure formed from arrays or linked lists. The stack works on the principle of LIFO (from Last-In-First-Out – “first to enter – last to exit”). Simply put, the first element to leave the stack will be the last element to enter it. The name “stack” is translated as “stack.” The fact is that this structure can be visualized as a stack of books lying on a table.
A queue is a linear data structure in the same way that a stack is. It is formed based on arrays or linked lists. It works on the FIFO (First-In-First-Out) principle. In other words, the first element to leave the queue will be the one that first entered it.
How are they used? Queues are used to process multiple requests on a single resource. In addition, they can be used for flow control in multi-threaded environments and load throttling.
A bunch of
The data is not ordered in a set and stored as a group. However, they need to be structured and sometimes even sorted. However, they can be used like ordinary mathematical sets, such as joining, finding intersections, calculating differences, and seeing if one set is a subset of another.
As a rule, such structures contain objects that have certain common features. In this case, the order of objects can be any.
How are they used? Sets are used to maintain a large number of unique objects. They also store data that does not need to be sorted. In addition, sets are needed to compare, combine data sets, and perform other operations.
Inside maps, data is stored in a key/value pair. Therefore, such structures are called associative arrays or dictionaries. Each key is unique, unlike values that can be used multiple times. If you know the key, data lookup will be faster than other structures.
How are they used? Maps are used to create bases that store unique correspondences between two sets of values. They are located in the key. At the same time, the structure controls the absence of repetitions of these keys.
Consider a vivid example of such a data structure – hash-map (hash table). Keys and values are placed in it, and indexes are added to implement them. With the help of a hash function, you can calculate the index, knowing the key. This allows you to find the necessary data. If this or that information is inserted into the hash table, then a key and data are added. The function then hashes the key, converts it to a number, and writes the data to the cell that corresponds to that number. If you want to query for data, you must enter the key again so that the hash function performs a lookup.
Binary search tree
This type of data structure is a structure whose data is placed in nodes. Each node can have one or more children and only one parent. Thus, outwardly they resemble a tree. There are several types of such structures. The most popular are search trees, which have the following:
- each node has no more than two children;
- if the new value is less, then it becomes the left child (or the child of the left child);
- if the value is greater, then it becomes the right child (or the child of the right child).
How are they used? Trees are used to find data and quickly store it in a sorted way. However, they can be quickly added and removed.
Prefix tree (boron, loaded tree)
It differs in the data storage sequence (each node is a prefix with which you can find the next nodes).
How are they used? Prefix trees store data that needs to be issued along the chain. For example, words for the autocomplete function on a mobile device: the user types in one letter, and the tree suggests the next. In addition, prefix trees store data with repeating sections (IP addresses).
A graph is a data structure consisting of several nodes (vertices) connected. A pair (x,y) is called an edge. Thus vertex x is connected to vertex y. An edge can define a weight/cost (traveling along a path between two vertices).
Graphs are more general cases of trees, which in turn are often called acyclic graphs. Two features distinguish one structure from another:
- There can be cycles in a graph (when the “child” is the “parent” for the same object).
- Edges can carry a semantic load (it is necessary to save their values).
There are two types of graphs: directed and undirected. In the first case, the edges between nodes have a direction. Therefore, for directed graphs, the order of elements matters. The undirected varieties have no directions. Therefore, they can be read and bypassed as you like.
It is worth noting that graphs are often represented as adjacency matrices. Each row or column is a node. If the value “1” appears in the cell, there is a connection between the nodes, and if “0”, there is no connection. Please note that if links (graph edges) have a weight, they can be placed in a cell.
How are they used? Graphs are useful for storing data that are related to complex relationships. In addition, these structures are used to form routes from point A to point B and perform analysis of related information.
How to determine the desired data type of the structure? It is necessary to focus on specific goals. For example, arrays can solve simple problems, while binary trees are suitable for more complex ones (when creating a queue, requesting history, etc.). You can always choose the best option if you know all the data types.
Structuring data in programming can be done in a variety of ways. If you know how the structures work, you can improve the software. In addition, the process of writing code will be much faster. Even at the stage of formation of specifications and requirements, attention should be paid to the features of the tasks. This will allow you to choose the correct data format.