列表数据结构2025年3月17日 | 阅读18分钟 列表可以定义为一个抽象数据类型,其中元素以有序的方式存储,以便更轻松高效地检索元素。列表数据结构允许重复,这意味着单个数据可以在列表中出现不止一次。对于同一数据的多个条目,该重复数据的每个条目都应被视为一个单独的项目或条目。它与数组非常相似,但数组和列表数据结构之间的主要区别在于,数组仅存储同构数据,而列表(在某些编程语言中)可以在其对象中存储异构数据项。列表数据结构也称为序列。 列表可以称为动态大小的数组,这意味着当我们继续向其中添加数据时,它们的尺寸会增大,并且我们无需预先定义列表的静态大小。 例如: numbers = [ 1, 2, 3, 4, 5] 在此示例中,'numbers' 是列表数据结构对象的名称,它存储了五个项。在名为 numbers 的对象中,我们存储了所有数值类型的元素。在列表中,索引从零开始,这意味着如果我们想访问或检索此列表的第一个元素,则需要使用索引零,同样,当我们想从名为 numbers 的列表中访问任何元素时。换句话说,我们可以说元素 1 位于索引 0,元素 2 位于索引 1,依此类推,所有后续元素。 Mixed_data = [205, 'Nirnay', 8.56] 在此第二个示例中,mixed_data 是存储不同类型数据的列表对象的名称。在 mixed_data 列表中,我们存储了三种类型的数据,第一种是整数类型,即 id '205',在整数数据之后,我们存储了一个字符串类型的数据,值为 'Nirnay',存储在索引 1 处,最后在索引值为 2 处,我们存储了一个浮点类型的数据,值为 '8.56'。 要访问 mixed_data 列表的元素,我们需要遵循与上一个示例中定义的相同方法。 我们可以向这些定义的列表对象添加更多数据,这些数据将被附加到列表的末尾。例如,如果我们向 mixed_data 列表添加另一个数据,它将被附加到值为 '8.56' 的浮点值对象之后。并且我们可以向这些列表对象添加重复的值。 列表数据结构的各种操作 在列表数据结构或序列上执行的各种操作是
现在,让我们看看列表数据结构或序列在不同编程语言中的用法。 Python一个用于在列表数据结构或序列对象上执行所有基本四种操作(创建、更新、删除和更新)的示例 Python 代码。 代码 输出 Add Data to the List Object:: Enter the roll no that you want to add to the list: 101 Enter the roll no that you want to add to the list: 103 Enter the roll no that you want to add to the list: 107 Enter the roll no that you want to add to the list: 108 Enter the roll no that you want to add to the list: 124 Enter the roll no that you want to add to the list: 112 Contents of the Roll No List are : 101 103 107 108 124 112 Update Data from the List Object:: Enter the old roll no that you want to update: 103 Enter the new roll no that you want to add: 104 The result after updating the List Object:: Contents of the Roll No List are : 101 104 107 108 124 112 Delete Data from the List Object:: Enter the roll no that you want to delete or remove from the list: 124 The result after delete operation on the List Object:: Contents of the Roll No List are : 101 104 107 108 112 在上面的代码中,我们可以看到所有基本函数,如向列表对象插入数据、从列表对象读取数据、删除列表对象以及更新列表对象中的数据都已成功执行。所写代码的流程是,首先,我们将六个学号添加到名为 roll_nos 的列表对象中。成功向列表对象插入数据后,我们通过打印列表对象的所有元素来确认插入。打印后,列表对象的内容或元素将被更新。在本例中,我们将索引为 0 的数据(值为 103)更新并替换为 104,然后打印更新后的列表对象内容。更新后,我们从列表对象中删除了一个元素。在本例中,我们删除了值为 124 的元素,然后打印了剩余的列表。 Java一个用于在列表数据结构或序列对象上执行所有基本四种操作(创建、更新、删除和更新)的示例 Java 代码。 代码 输出 Enter data in the Data in the List Object:: Enter the name to be added in the list object : Andrew Enter the name to be added in the list object : Simon Enter the name to be added in the list object : Paul Enter the name to be added in the list object : Nirnay Enter the name to be added in the list object : Dexteer The data in the List Object is : Andrew Simon Paul Nirnay Dexteer Update the Data that is present in the List Object:: Enter the old data that needs to be updated : Dexteer Enter the new data that will be updated : Dexter The Result after the Update Operation:: The data in the List Object is : Andrew Simon Paul Nirnay Dexter Delete a data that is present in the List Object:: Enter the name to be deleted from the list object : Paul The Result after the Delete Operation:: The data in the List Object is : Andrew Simon Nirnay Dexter 在上面的代码中,我们可以看到所有基本函数,如向列表对象插入数据、从列表对象读取数据、删除列表对象以及更新列表对象中的数据都已成功执行。所写代码的流程是,首先,我们将五个人的名字添加到名为 names 的列表对象中。成功向列表对象插入数据后,我们通过打印列表对象的所有元素来确认插入。打印后,列表对象的内容或元素将被更新。在本例中,我们将最后一个索引处的数据(值为 'Dexteer')更新并替换为 'Dexter',然后通过调用 print_names() 函数打印更新后的列表对象内容。更新后,我们从列表对象中删除了一个元素。在本例中,我们删除了名为 paul 的元素,在成功删除名为 'Paul' 的字符串对象后,我们再次通过调用 print_names() 函数打印更新后的列表来确认结果。 C++一个用于在列表数据结构或序列对象上执行所有基本四种操作(创建、更新、删除和更新)的示例 C++ 代码。 代码 输出 上述代码的输出是 Enter Data in the List Object:: Enter the name to be added to the List Object : Alex Enter the name to be added to the List Object : Andrew Enter the name to be added to the List Object : Pete Enter the name to be added to the List Object : Samuel Enter the name to be added to the List Object : Pauil The data in the List Object:: Alex Andrew Pete Samuel Pauil Enter the already existing data that you want to update from the List Object:: Pauil Enter the data that you want to add to the List Object:: Paul The result after the Update operation:: Alex Andrew Pete Samuel Paul Enter the element from the List Object that you want to delete or remove:: Andrew Result after the Delete or Remove operation:: Alex Pete Samuel Paul 在上面的代码中,我们可以看到所有基本函数,如向列表对象插入数据、从列表对象读取数据、删除列表对象以及更新列表对象中的数据都已成功执行。所写代码的流程是,首先,我们将五个人的名字添加到名为 names 的列表对象中。成功向列表对象插入数据后,我们通过打印列表对象的所有元素来确认插入。打印后,列表对象的内容或元素将被更新。在本例中,我们将最后一个索引处的数据(值为 'Pauil')更新并替换为 'Paul',然后通过调用 print_names() 函数打印更新后的列表对象内容。更新后,我们从列表对象中删除了一个元素。在本例中,我们删除了名为 paul 的元素,在成功删除名为 'Paul' 的字符串对象后,我们再次通过调用 print_names() 函数打印更新后的列表来确认结果。 JavaScript一个用于在列表数据结构或序列对象上执行所有基本四种操作(创建、更新、删除和更新)的示例 JavaScript 代码。 代码 输出 上述代码的输出是 The Data in the list after the sucessful Insertion Operation is :: [ 'BMW', 'Ferrari', 'Aston Martin', 'Land Rover', 'Audii' ] The Data in the list after the sucessful Update Operation is :: [ 'BMW', 'Ferrari', 'Aston Martin', 'Land Rover', 'Audi' ] The Data in the list after the sucessful Delete Operation is :: [ 'Ferrari', 'Aston Martin', 'Land Rover', 'Audi' ] 在上面的代码中,我们可以看到所有基本函数,如向列表对象插入数据、从列表对象读取数据、删除列表对象以及更新列表对象中的数据都已成功执行。所写代码的流程是,首先,我们将五个汽车名称添加到名为 car_names 的列表对象中。成功向列表对象插入数据后,我们通过打印列表对象的所有元素来确认插入。打印后,列表对象的内容或元素将被更新。更新后,我们从列表对象中删除了一个元素。我们再次通过调用 print_data() 函数打印更新后的列表来确认结果。 因此,本文介绍了列表数据结构以及我们可以在列表数据结构对象上执行的基本函数或操作。我们还理解了列表数据结构在 Java、Python 和 C++ 等各种编程语言中的用法,以及执行此数据结构基本操作所需的功能。除了这些示例,还有各种可以使用列表数据结构的场景。最理想的场景是需要将数据存储在单个序列对象中。 下一主题树在数据结构中的类型 |
我们请求您订阅我们的新闻通讯以获取最新更新。