通用程序算法和数据结构
In this article, I am going to walk you through the concepts of the common Data Structures that every student, colleague working with computers should be aware of. Data Structure forms an integral part of any system or database design. It is a very interesting and intuitive concept that you can apply anywhere. Through this article, I aim to introduce the beginners to the concepts of Data Structures and brush up the same for colleagues who have already been associated with the industry for years. This will also help you understand some database concepts more easily once you have a grasp over these concepts.
在本文中,我将带您了解通用数据结构的概念,每个使用计算机的学生和同事都应该了解这些概念。 数据结构构成任何系统或数据库设计的组成部分。 这是一个非常有趣且直观的概念,您可以将其应用于任何地方。 通过这篇文章,我旨在向初学者介绍数据结构的概念,并为已经与该行业联系多年的同事们提炼它们。 一旦掌握了这些概念,这也将帮助您更轻松地理解一些数据库概念。
In this article, I am going to cover the most important and frequently used Data Structures in day-to-day life of a software developer. Please note other data structures might not be covered in this article and I will explain those in some other article.
在本文中,我将介绍软件开发人员日常工作中最重要和最常用的数据结构。 请注意,本文可能未涵盖其他数据结构,我将在其他一些文章中进行解释。
- Arrays 数组
- Linked Lists 链表
- Stacks 堆栈
- Queues Queue列
Let us now begin understanding these, one by one.
现在让我们开始一个一个地理解这些。
数组 (Arrays)
An array is a collection of elements that are stored at adjacent memory locations. These are structures that can hold data for the same data type and are fixed in length. Be it an array of integers, strings, or even an array of array. These arrays have indexes which means they can be accessed randomly by providing the index of the position. Each of the elements in the array is stored in the memory with a specific address that can be used to retrieve the element back. The last element in an array is always NULL.
数组是存储在相邻内存位置的元素的集合。 这些结构可以保存相同数据类型的数据,并且长度固定。 可以是整数,字符串或什至是数组的数组。 这些数组具有索引,这意味着可以通过提供位置索引来随机访问它们。 数组中的每个元素都有一个特定的地址存储在内存中,该地址可用于取回元素。 数组中的最后一个元素始终为NULL 。
Figure 1 – Data Structures – Arrays
图1 –数据结构–数组
Considering an analogy, you may think of a multi-story building as an array. In that building, all the floors have the same floor plan (same data type) and your friend might reside on the second floor (index = 2).
考虑一个类比,您可能会将多层建筑物视为一个数组。 在该建筑物中,所有楼层都具有相同的楼层平面图( 相同的数据类型 ),并且您的朋友可能位于第二层(索引= 2)。
Figure 2 – Array Analogy by using a building (Source)
图2 –使用建筑物进行数组类比( 来源 )
Basically, we can perform the following operations on an array.
基本上,我们可以对数组执行以下操作。
- Traverse – Navigate through the elements in the array and display each of them 遍历 –浏览数组中的元素并显示每个元素
- Search – We can also search for a specific element by providing the index of the element 搜索 –我们还可以通过提供元素的索引来搜索特定元素
- Update – Values at certain specified indexes can also be updated 更新 –某些指定索引处的值也可以更新
A limitation of an array is that we cannot insert or remove elements from the array dynamically. This is because the arrays are mostly fixed in size. If you need to add an element to the array, then you might need to create a new array with length greater than that of the current one and then copy the elements from one array to the other. Same goes for deletion as well, you need to create a new array with reduced size.
数组的局限性在于我们不能动态地从数组中插入或删除元素。 这是因为数组的大小通常是固定的。 如果需要向数组添加元素,则可能需要创建一个长度大于当前数组长度的新数组,然后将元素从一个数组复制到另一个数组。 删除同样适用,您需要创建一个尺寸减小的新阵列。
Arrays are mostly used to build or develop other complex data structures such as heaps, hash tables, array lists, etc.
数组主要用于构建或开发其他复杂的数据结构,例如堆,哈希表,数组列表等。
链表 (Linked Lists)
Linked Lists are sequential data structures which are connected to one another in a linear fashion. For this reason, you cannot access a linked list randomly, but only sequential access is possible. There are three types of linked lists. All the elements in a linked list are known as nodes. Each node comprises of a key and a pointer to the next node. The Head and Tail are the starting and ending nodes of the linked list, respectively.
链表是顺序数据结构,它们以线性方式相互连接。 因此,您不能随机访问链接列表,而只能进行顺序访问。 链接列表有三种类型。 链表中的所有元素都称为节点。 每个节点都包含一个密钥和一个指向下一个节点的指针 。 头和尾分别是链表的开始和结束节点。
Figure 3 – Data Structures – Linked Lists
图3 –数据结构–链接列表
- Singly Linked Lists – In these, we can only navigate only forward
- 单链接列表 –在这些列表中 ,我们只能向前浏览
- Doubly Linked Lists – Here, we can traverse both forward and reverse. This is done by an additional pointer known as the 双链表 –在这里,我们可以遍历正向和反向。 这是由被称为previous 先前的其他指针完成
- Circular Linked Lists – In this case, the next pointer of the tail is connected to the head and the previous pointer of the head is connected to the tail of the list 循环链接列表 –在这种情况下,尾部的下一个指针连接到头部,头的前一个指针连接到列表的尾部
The following operations can be performed on a linked list.
可以在链接列表上执行以下操作。
- Search – You can find the first element with a key 搜索 –您可以找到带有键kk
- Insert – You can insert elements either at the beginning of the list, in the end, or in the middle of the linked list 插入 –您可以在列表的开头,结尾或中间插入元素
- Delete – Similarly, you can remove elements from either beginning of the list, from the middle, or the end of the list 删除 –同样,您可以从列表的开头,列表的中间或结尾删除元素
An important application of a circular linked list can be seen in Windows while switching between multiple programs using the Alt + Tab keys.
在Windows中可以看到循环链接列表的重要应用,同时使用Alt + Tab键可以在多个程序之间切换。
堆栈 (Stacks)
Stacks are also very commonly used data structures used in most of the major applications around the world. It follows the common acronym LIFO, which means Last In First Out. You can resemble it to something like a stack of plates, so, the one which is kept at the last is taken at the first.
堆栈也是世界上大多数主要应用程序中非常常用的数据结构。 它遵循通用首字母缩写词LIFO ,意思是后进先出。 您可以将其类似于一堆盘子,因此,最后存放的是第一个。
Figure 4 – Stack of plates (Source)
图4 –板叠( 来源 )
There are two prime operations that you can perform on a stack – Push and Pop.
您可以在堆栈上执行两个主要操作-推入和弹出。
- Push – Add an element to the top of the stack 推 –将元素添加到堆栈顶部
- Pop – Remove an element from the top of the stack and return it 弹出 -从堆栈顶部删除一个元素并返回
Apart from these two, you can also use the following operations on a stack.
除了这两个以外,您还可以在堆栈上使用以下操作。
- Peek – It is used to return the top element of the stack without removing or deleting it 窥视 –用于返回堆栈的顶部元素而不删除或删除它
- isFull – Used to check if the stack is full isFull –用于检查堆栈是否已满
- isEmpty – Used to check if the stack is empty isEmpty –用于检查堆栈是否为空
The most common applications of stacks are used in implementing function calls in recursive programming. This also gives rise to a very common and frequent error, known as the stack overflow error.
堆栈的最常见应用程序用于在递归编程中实现函数调用。 这也会引起一个非常常见的错误,即堆栈溢出错误。
Queue列 (Queues)
A queue is another data structure that follows FIFO or First In First Out. You can resemble this with a real-life queue and hence the name. It is also a dynamic data structure which means you can only add elements to the end of the queue and remove only from the beginning. Unlike a stack, a queue is open at both ends, and thus it allows the addition of elements at the end of the queue while removal of the items can be done from the beginning of the queue.
队列是遵循FIFO或先进先出的另一种数据结构。 您可以将它与现实生活中的队列和名称类似。 它也是一个动态数据结构,这意味着您只能将元素添加到队列的末尾,而只能从队列的开头删除。 与堆栈不同,队列的两端都是开放的,因此它允许在队列末尾添加元素,同时可以从队列开始处删除项目。
Figure 5 – Example of a real-life queue
图5 –真实队列的示例
The following operations can be performed in a queue.
可以在队列中执行以下操作。
Figure 6 – Data Structures – Queue in programming (Source)
图6 –数据结构–编程队列( 源 )
- Enqueue – Used to add an item to the end of the queue. If the queue is already full, then it will throw an Overflow condition 入队 –用于将项目添加到队列的末尾。 如果队列已满,则将引发溢出条件
- Dequeue – Used to remove an item from the beginning of the queue. If the queue is empty, then it will throw an Underflow condition 出队 –用于从队列的开头删除项目。 如果队列为空,则将引发下溢条件
- Front – Used to return the item at the front of the queue 前 –用于返回队列前的项目
- Rear – Used to return the item at the end of the queue 后方 –用于在队列末尾返回项目
Queues are mostly used while designing multi-threaded applications such that the first thread gets executed first and the last executes at the last. Also, another application would be using priority queues while designing microservice architecture systems. Amazon SQS and Azure Service Bus are examples of a cloud-based message queue system which implements the concept of queues to send and receive messages.
在设计多线程应用程序时,通常使用队列,以便第一个线程首先执行,最后一个线程最后执行。 同样,另一个应用程序在设计微服务体系结构系统时将使用优先级队列。 Amazon SQS和Azure Service Bus是基于云的消息队列系统的示例,该系统实现了队列概念来发送和接收消息。
结论 (Conclusion)
In this article, we have seen how important it is to understand the concepts of the common Data Structures. These concepts will help you when you start designing a new system from scratch or while trying to understand some of the existing data systems. I remember during my days back in college, it was one of the most important topics that we had to learn thoroughly. Yet today, these are one of the most frequently asked questions in any software development interview. I would strongly recommend everyone to have a good understanding of these concepts before appearing for any technical discussion.
在本文中,我们已经了解了理解通用数据结构的概念有多么重要。 当您从头开始设计新系统或尝试了解一些现有数据系统时,这些概念将为您提供帮助。 我记得在上大学的那一天,它是我们必须彻底学习的最重要的主题之一。 时至今日,这些已成为所有软件开发面试中最常见的问题之一。 我强烈建议大家在出现任何技术讨论之前对这些概念有一个很好的了解。
翻译自: https://www.sqlshack.com/understanding-common-data-structures/
通用程序算法和数据结构