8 common data structures that every programmer must know

Yunqi Information: [ Click to view more industry information ]
Here you can find the first-hand Shangyun information in different industries, what are you waiting for, come soon!

Data structure is a special way of organizing and storing data, which allows us to perform operations on stored data more efficiently. Data structures have a wide variety of uses in computer science and software engineering.

1826B3D4_0FA5_4049_B4C2_CC8725898B5C

Almost all developed programs or software systems use data structures. In addition, the data structure belongs to the foundation of computer science and software engineering. This is a key topic when it comes to software engineering interview questions. Therefore, as developers, we must have a full understanding of the data structure.

In this article, I will briefly explain the 8 common data structures that every programmer must know.

1. Array

Arrays are fixed-size structures that can hold items of the same data type. It can be an array of integers, an array of floating-point numbers, an array of strings or even an array of arrays (such as a two-dimensional array). The array is indexed, which means random access is possible.

E1A184FD_A4D0_415d_B2C1_0682D67CD81D

Array operation

  • Traversal: Traverse all elements and print.
  • Insert: Insert one or more elements into the array.
  • Delete: delete elements from the array
  • Search: Search for elements in the array. You can search for elements by their value or index
  • Update: update the value of an existing element at a given index

Array application

  • Used as a basis for building other data structures, such as array lists, heaps, hash tables, vectors, and matrices.
  • Used for different sorting algorithms, such as insertion sort, quick sort, bubble sort and merge sort.

2. Linked list

A linked list is a sequential structure consisting of a sequence of linear sequential items linked to each other. Therefore, you must access the data sequentially, and random access is not possible. Linked lists provide a simple and flexible representation of dynamic sets.

Let us consider the following terms related to linked lists. You can get a clear idea by referring to Figure 2.

  • The elements in the linked list are called nodes.
  • Each node contains a key and a pointer to its successor node (called next).
  • The attribute named head points to the first element of the linked list.
  • The last element of the linked list is called the tail.

60167FAC_B2FF_4ab9_B8BB_0D329E6FB225

The following are the various types of linked lists available.

  • Single-chain list-can only traverse items in the forward direction.
  • Double linked list-can traverse items in forward and backward directions. The node consists of an additional pointer called the previous one, which points to the previous node.
  • Circular link list—a link list where the previous pointer to the head points to the tail and the next pointer to the head points to the head.

Linked list operation

  • Search: Find the first element with key k in a given linked list by simple linear search, and return a pointer to that element
  • Insert: Insert a key in the linked list. Insertion can be done in 3 different ways; insert at the beginning of the list, insert at the end of the list, and then insert in the middle of the list.
  • Delete: delete element x from the given linked list. You cannot delete a node in a single step. Deletion can be done in 3 different ways; delete from the beginning of the list, delete from the end of the list, and then delete from the middle of the list.

Application of linked list

  • Used for symbol table management in compiler design.
  • Used to switch between programs that use Alt Tab (implemented using a circular linked list).

3. Stack

The stack is a LIFO (last in first out-the last placed element can be accessed first) structure, which is usually found in many programming languages. This structure is called a "stack" because it is similar to a real-world stack-board stack.

Stack operation

Here are two basic operations that can be performed on the stack. Please refer to Figure 3 to better understand the stack operation.

  • Push: Push an element at the top of the stack.
  • Pop Pop: delete the top element and return.

3055A28C_072A_4abe_A939_6CB7CFBFCBE8

In addition, the following additional functions are provided for the stack to check its status.

  • Peep: Return to the top element of the stack without deleting it.
  • isEmpty: Check if the stack is empty.
  • isFull: Check if the stack is full.

Stack application

  • Used for expression evaluation (for example: yard algorithm for parsing and evaluating mathematical expressions).
  • Used to implement function calls in recursive programming.

4. Queue

The queue is a FIFO (first-in first-out-the element placed first can be accessed first) structure, which is usually found in many programming languages. This structure is called a "queue" because it is similar to a queue in the real world-people wait in the queue.

Queue operation

The two basic operations that can be performed on the queue are given below. Please refer to Figure 4 to better understand the stack operation.

  • Enqueue: Insert the element at the end of the queue.
  • Dequeue: Delete elements from the beginning of the queue.

0E133E1E_26B5_40e8_9430_9E777F80183D

Queue application

  • Used to manage threads in multiple threads.
  • Used to implement a queuing system (eg priority queue).

5. Hash table

A hash table is a data structure used to store the value of a key associated with each key. In addition, if we know the key associated with the value, it effectively supports lookup. Therefore, regardless of the size of the data, insertion and search are very effective.

When stored in a table, direct addressing uses a one-to-one mapping between values ​​and keys. However, this method has problems when there are a large number of key-value pairs. The table will have many records and is very large. Considering the available memory on a typical computer, the table may be impractical or even impossible to store. To avoid this problem, we use hash tables.

Hash function

A special function called hash function (h) is used to overcome the above problems in direct addressing.

In direct access, the value with key k is stored in slot k. Using the hash function, we can calculate the index of the table (slot) to which each value points. The value calculated using the hash function of the given key is called the hash value, which represents the index of the table to which the value is mapped.

  • h: hash function
  • k: the key whose hash value should be determined
  • m: the size of the hash table (number of available slots). A prime number that is not close to an exact power of 2 is a good choice for m.

E9047EBD_6D35_4cb6_A216_687141B326A5

From the last two examples given above, we can see that when the hash function generates the same index for multiple keys, conflicts will occur. We can resolve the conflict by choosing the appropriate hash function h and using techniques such as linking and open addressing.

Hash table application

  • Used to implement database indexes.
  • Used to implement associative arrays.
  • Used to implement "set" data structures.

6. Tree

A tree is a hierarchical structure in which data is organized in layers and linked together. This structure is different from the linked list, where items are linked in a linear order.

Over the past few decades, various types of trees have been developed to suit certain applications and meet certain restrictions. Some examples are binary search trees, B trees, red-black trees, expanded trees, AVL trees, and n-ary trees.

Binary search tree

As the name implies, a binary search tree (BST) is a binary tree in which data is organized in a hierarchical structure. This data structure stores values ​​in sorted order, and we will study these values ​​in detail in this course.

Each node in the binary search tree contains the following attributes.

  • key: The value stored in the node.
  • left: Pointer to the left child.
  • Right: Pointer to the correct child.
  • p: pointer to the parent node.
    Binary search trees have unique attributes that distinguish them from other trees. This attribute is called the binary-search-tree attribute.

Let x be a node in the binary search tree.

  • If y is a node in the left subtree of x, then y.key≤x.key
  • If y is a node in the right subtree of x, then y.key≥x.key

BA848ABC_4916_4f8e_A120_F1EFFDEB0CFA

Application of tree

  • Binary tree: used to implement expression parser and expression solver.
  • Binary search tree: used in many search applications that continuously input and output data.
  • Heap: used by the JVM (Java Virtual Machine) to store Java objects.
  • Trap: Used for wireless networks.

7. Heap

Heap is a special case of binary tree, in which the value of parent node and its child nodes are compared and arranged accordingly.
Let's see how to represent the heap. Heaps can be represented using trees and arrays. Figures 7 and 8 show how we use binary trees and arrays to represent binary heaps.

01D40240_D0D3_4f49_885A_5E6C6647E225

The heap can have 2 types.

  • Minimum heap-The parent's key is less than or equal to the child's key. This is called the min-heap attribute. The root will contain the minimum value of the heap.
  • Maximum number of heaps-The parent's key is greater than or equal to the child's key. This is called the max-heap attribute. The root will contain the maximum value of the heap.

Heap application

  • Used to implement priority queues, because priority values ​​can be sorted according to heap attributes.
  • You can use the heap in O (log n) time to implement the queue function.
  • Used to find the k smallest (or largest) values ​​in a given array.
  • Used for heap sorting algorithms.

8. Picture

A graph consists of a limited set of vertices or nodes and a set of edges connecting these vertices.
The order of the graph is the number of vertices in the graph. The size of the graph is the number of edges in the graph.
If two nodes are connected to each other through the same edge, they are called adjacent nodes.

Directed graph

If all edges of the graph G have directions indicating what is the starting vertex and what is the ending vertex, the graph is called a directed graph.
We say that (u, v) enters or leaves vertex u from vertex u, and then enters or enters vertex v.
Self-loop: From the vertex to its own edge.

Undirected graph

If all edges of graph G have no direction, it is called an undirected graph. It can spread between two vertices in two ways.
If the vertex is not connected to any other node in the graph, the vertex is said to be isolated.

47701E44_A860_436c_9A3F_2B6662BDFBF0

Application of graph

  • Used to represent social media networks. Each user is a vertex, and an edge is created when the user connects.
  • Used to represent web pages and links of search engines. Web pages on the Internet are linked to each other through hyperlinks. Each page is a vertex, and the hyperlink between the two pages is an edge. Used to rank pages in Google.
  • Used to represent the location and route in GPS. The location is the vertex, and the route connecting the locations is the edge. Used to calculate the shortest path between two locations.

[Yunqi Online Classroom] Every day, product technology experts share!
Course address: https://yqh.aliyun.com/zhibo

Join the community now, face-to-face with experts, and keep abreast of the latest developments in the course!
【Yunqi Online Classroom Community】https://c.tb.cn/F3.Z8gvnK

If you find that there is suspected plagiarism in this community, please send an email to: [email protected] to report and provide relevant evidence. Once verified, the community will immediately delete the suspected infringing content.

Original link
This article is the original content of the Yunqi community and may not be reproduced without permission.

Published 2315 original articles · 2062 thumbs up · 1.58 million views

Guess you like

Origin blog.csdn.net/yunqiinsight/article/details/105398378