Eight common data structures

Eight common data structures

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 range of uses in the fields of computer science and software engineering.

Almost all programs or software systems that have been developed use data structures. In addition, data structures belong to the foundation of computer science and software engineering. This is a key theme when it comes to software engineering interview questions. Therefore, if you need to use programming skills, you should have a full understanding of data structures.

1. Array

An array is a fixed-size structure 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.

Fig 1. Visualization of basic Terminology of Arrays

Array operations

  • Traverse: Traverse all elements and print them.
  • 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 the 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 items linked to each other in a linear order. Therefore, you must access the data sequentially, and random access is not possible. The linked list provides a simple and flexible representation of the Dynaset.

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.


Fig 2. Visualization of basic Terminology of Linked Lists The
following are various types of linked lists available:

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

Linked list operation

  • Search: Find the first element with key k in a given linked list through a 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 at the middle of the list.
  • Delete: delete element x from the given linked list. You cannot delete nodes 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,
    • 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 element placed 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—a stack of boards.

Stack operation

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

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


Fig 3. Visualization of basic Operations of Stacks

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

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

Stack application

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

4. Queue

A 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 a queue.

Queue operation

Below are two basic operations that can be performed on the queue. Please refer to Figure 4 for a better understanding of stack operation.

  • Advancing: Insert the element at the end of the queue.
  • Dequeue: Remove elements from the beginning of the queue.


Fig 4. Visualization of Basic Operations of Queues

Queue application

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

##5. Hash table A
hash table is a data structure used to store values ​​that have a key associated with each key. Furthermore, if we know the key associated with the value, it effectively supports lookups. 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, when there are a large number of key-value pairs, this method has problems. 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 a hash table.

Hash function

A special function named hash function (h) is used to overcome the above-mentioned 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) that each value points to. The value calculated using the hash function of a 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.


Fig 5. Representation of a Hash Function
1→1→1
5→5→5
23→23→3
63→63→3
From the last two examples given above, we can see that when the hash function is more When two keys generate the same index, conflicts occur. We can resolve conflicts by choosing the appropriate hash function h and using techniques such as linking and open addressing.

Application of Hash Table

  • Used to implement database indexes.
  • Used to implement associative arrays.
  • Used to implement the "settings" data structure.

6. Tree

A tree is a hierarchical structure in which data is organized hierarchically and linked together. This structure is different from a linked list, where items are linked in a linear order.
In 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: The pointer to the left child.
  • Right: Pointer to the correct child.
  • p: Pointer to the parent node.

Binary search tree has unique properties that can distinguish it 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

    Fig 6. Visualization of Basic Terminology of Trees.

Tree application

  • 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 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 values ​​of the parent node and its child nodes are compared and arranged accordingly.
Let us see how to represent the heap. The heap can be represented by trees and arrays. Figures 7 and 8 show how we use binary trees and arrays to represent binary heaps.

Fig 7. Binary Tree Representation of a Heap

Fig 8. Array Representation of a Heap
Heap can have 2 types:

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

###Heap of Applications

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

8. Figure

A graph consists of a finite 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 by 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, then 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: the edge from the vertex to itself.
###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 a vertex is not connected to any other node in the graph, it is said to be isolated.

Fig 9. Visualization of Terminology of Graphs

Graph application

  • 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 for search engines. Web pages on the Internet are linked to each other through hyperlinks. Each page is a vertex, and the hyperlink between two pages is an edge. Used for page ranking in Google.
  • Used to indicate location and route in GPS. The positions are vertices, and the routes connecting the positions are edges. Used to calculate the shortest path between two locations.

Guess you like

Origin blog.csdn.net/GodfatherTye/article/details/109081186