List Data StructureThe list can be defined as an abstract data type in which the elements are stored in an ordered manner for easier and efficient retrieval of the elements. List Data Structure allows repetition that means a single piece of data can occur more than once in a list. In the case of multiple entries of the same data, each entry of that repeating data is considered as a distinct item or entry. It is very much similar to the array but the major difference between the array and the list data structure is that array stores only homogenous data in them whereas the list (in some programming languages) can store heterogeneous data items in its object. List Data Structure is also known as a sequence. The list can be called Dynamic size arrays, which means their size increased as we go on adding data in them and we need not to pre-define a static size for the list. For example, numbers = [ 1, 2, 3, 4, 5] In this example, 'numbers' is the name of the List Data Structure object and it has five items stored in it. In the object named numbers, we have stored all the elements of numeric type. In the list, the indexing starts from zero, which means if we want to access or retrieve the first element of this list then we need to use index zero and similarly whenever we want to access any element from this list named numbers. In other words, we can say that element 1 is on the index 0 and element 2 is on index 1 and similarly for further all elements. Mixed_data = [205, 'Nirnay', 8.56] In this second example, mixed_data is the name of the list object that stores the data of different types. In the mixed_data list, we have stored data of three types, first one is the integer type which is id '205', after the integer data we have stored a string type data having the value 'Nirnay' stored at index 1 and at last the index value 2, we have stored a float type data having the value '8.56'. To access the elements of the mixed_data list, we need to follow the same approach as defined in the previous example. And we can add more data to these defined List objects and that will get appended at the last of the list. For example, if we add another data in the mixed_data list, it will get appended after the float value object having value '8.56'. And we can add repeating values to these list-objects. Various operations on the List Data Structure: The various operations that are performed on a List Data Structure or Sequence are:
Now, let us see the usage of the List Data Structure or Sequence in different programming languages. Python:A sample python code to perform all the basic four operations like create, update, delete and update on the List Data Structure or Sequence object. Code: Output: 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 In the code that is written above, we can see that all the basic functions like inserting data to the list object, reading data from the list object, deleting the list object and updating data in the list object is performed successfully. The flow of the code written is like, first, we added six roll numbers to the list object named roll_nos. After the successful insertion of data in the list object, we confirmed the insertion by printing all the elements of the list object. After printing, the content or element of the list object is updated. In this case, we updated the data present at the first index having value 103 and replaced or updated that value by 104, and then printed the updated contents of the list object. After updation, we deleted an element from the list object. In this case, we deleted the element having value 124 and then printed the remaining list. Java:A sample java code to perform all the basic four operations like create, update, delete and update on the List Data Structure or Sequence object. Code: Output: 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 In the code that is written above, we can see that all the basic functions like inserting data to the list object, reading data from the list object, deleting the list object and updating data in the list object is performed successfully. The flow of the code written is like, first, we added five names of the persons to the list object named names. After the successful insertion of data in the list object, we confirmed the insertion by printing all the elements of the list object. After printing, the content or element of the list object is updated. In this case, we updated the data present at the last index having the value 'Dexteer' and replaced or updated that value by 'Dexter', and then printed the updated contents of the list object by calling the print_names() function. After updation, we deleted an element from the list object. In this case, we deleted the element named paul and after the successful deletion of the string object named 'Paul,' we again confirmed the results by printing the updated list by giving a call to the print_names() function. C++:A sample C++ code to perform all the basic four operations like create, update, delete and update on the List Data Structure or Sequence object. Code: Output: The output of the above code is: 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 In the code that is written above, we can see that all the basic functions like inserting data to the list object, reading data from the list object, deleting the list object and updating data in the list object is performed successfully. The flow of the code written is like, first, we added five names of the persons to the list object named names. After the successful insertion of data in the list object, we confirmed the insertion by printing all the elements of the list object. After printing, the content or element of the list object is updated. In this case, we updated the data present at the last index having the value 'Pauil' and replaced or updated that value by 'Paul', and then printed the updated contents of the list object by calling the print_names() function. After updation, we deleted an element from the list object. In this case, we deleted the element named paul and after the successful deletion of the string object named 'Paul,' we again confirmed the results by printing the updated list by giving a call to the print_names() function. JavaScript:A sample JavaScript code to perform all the basic four operations like create, update, delete and update on the List Data Structure or Sequence object. Code: Output: The output of the above code is: 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' ] In the code that is written above, we can see that all the basic functions like inserting data to the list object, reading data from the list object, deleting the list object and updating data in the list object is performed successfully. The flow of the code written is like, first, we added five car names to the list object named car_names. After the successful insertion of data in the list object, we confirmed the insertion by printing all the elements of the list object. After printing, the content or element of the list object is updated. After updation, we deleted an element from the list object. We again confirmed the results by printing the updated list by giving a call to the print_data() function. So, this article explains the List Data Structure and what are the basic functions or operations that we can perform on a List Data Structure object. We also understood the usage of the List Data Structure in various programming languages like Java, Python, and C++ along with their functionalities that are required to perform the basic operations on this Data Structure. Other than these examples, there are various scenarios where we can use the List Data Structure. The most ideal scenario for using the List Data Structure is where we need to store our data in a single sequence object. Next TopicTypes of Tree in Data Structure |