动态数据结构2024 年 8 月 28 日 | 阅读 9 分钟 动态数据结构是一种在运行时改变其大小的数据结构。存储在数据结构中的值可以轻松更改,无论是静态数据结构还是动态数据结构。但动态数据结构的设计方式使得数据和数据结构的大小都可以在运行时轻松更改。 动态数据结构定义的主要用例是为了在运行时轻松方便地更改数据结构的大小,而不会影响到在该数据结构增加或减少大小之前与之相关的其他操作。 与静态数据结构相比,在静态数据结构中我们只能在运行时更改数据结构中的数据,而在动态数据结构中,数据结构中的数据和数据结构的大小都可以轻松地相应地更改。在静态数据结构中,我们需要预定义或计算数据结构的大小。由于数据结构的大小已经固定,因此我们只能在静态数据结构中存储特定数量的数据。这个问题由动态数据结构解决,我们可以轻松存储在运行时生成的任何数量的数据,即在动态数据结构中,我们可以在程序执行期间的运行时轻松增加数据结构的大小。 除了这些好处之外,动态数据结构也存在一些缺点。动态数据结构的主要缺点之一是内存消耗问题。正如我们所知,动态数据结构不需要分配固定数量的内存,数据结构总是存在内存溢出的可能性。或者另一方面,如果数据结构在程序执行期间的某个操作中变为空,也可能发生内存不足。可以通过严格检查动态数据结构消耗的内存量来防止这些问题,在内存溢出的情况下,需要添加一个检查;在内存不足的情况下,需要添加一个对动态数据结构中存储的数据的检查。 动态数据结构的一些主要例子是
现在让我们详细看看双向链表,以便更好地理解动态数据结构及其工作原理。现在让我们编写一个双向链表的示例代码。 代码输出 Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 1 Enter the data that you want to add to the Doubly Linked List:: 12 Data Entered Successfully at the End of the Doubly Linked List. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 4 Data in the Doubly Linked List is:: 12<==>NULL Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 2 Enter the data that you want to add to the Doubly Linked List:: 20 Data Entered Successfully at the Front of the Doubly Linked List. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 4 Data in the Doubly Linked List is:: 20<==>12<==>NULL Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 3 Enter the data that you want to add after the head node of the Doubly Linked List:: 15 Data Entered Successfully after the head node of the Doubly Linked List. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 4 Data in the Doubly Linked List is:: 20<==>15<==>12<==>NULL Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 1 Enter the data that you want to add to the Doubly Linked List:: 67 Data Entered Successfully at the End of the Doubly Linked List. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data at the End of the Doubly Linked List. 2. To Insert Data in the Front of the Doubly Linked List. 3. To Insert Data after the head node in the Doubly Linked List. 4. To Display Data in the Doubly Linked List. 4 Data in the Doubly Linked List is:: 20<==>15<==>12<==>67<==>NULL Type [N or n] to terminate the program. Type [Y or y] to continue the program. n 在上面写的代码中,我们首先在双向链表的末尾插入了值。然后我们在双向链表的开头添加了数据。然后,在双向链表的头节点之后添加了一个数据。并且写有不同的函数,如 insert_end()、insert_front() 和 insert_After(),分别用于在双向链表的末尾、开头和头节点之后添加数据。 并且在每次插入操作后都会打印双向链表的内容。 在双向链表中,我们可以通过调用其中一个写入的插入函数来存储任意数量的数据,并且在程序执行时双向链表的大小会自动增加。所以,我们可以说双向链表满足动态数据结构的所有条件。 因此,本文清楚地介绍了什么是动态数据结构,以及它相对于静态数据结构的优势。我们还看到了 C++ 编程语言中一种动态数据结构——双向链表的实现。 下一个主题数据结构中的哈希函数 |
我们请求您订阅我们的新闻通讯以获取最新更新。