dllist class of llist module in Python

WBOY
Release: 2023-09-10 08:45:11
forward
1332 people have browsed it

dllist class of llist module in Python

dllistis a class of thellistmodule in Python, which is used to implement a doubly linked list and has the functions of inserting, deleting, and traversing elements. Thedllistclass provides methods for adding, removing, and iterating lists in both directions. In this article, we will take a closer look at thedllistclass and its methods.

Create dllist object

To create a dllist object, we need to first import the llist module from the pyllist package. We can then use the dllist class constructor to create a new instance of the doubly linked list. The following code will create an empty doubly linked list.

from pyllist import dllist # create an empty doubly-linked list my_list = dllist()
Copy after login

Add elements to dlllist

We can easily add elements to dllist using the append() method which takes the element as parameter and adds the element to the end of the list.

my_list.append(1) my_list.append(2) my_list.append(3)
Copy after login

Access elements in dlllist

We can access elements of dllist using bracket notation, similar to how we access list elements. Additionally, we can use a for loop to access all elements in the list.

from pyllist import dllist # create an empty doubly-linked list my_list = dllist() my_list.append(1) my_list.append(2) my_list.append(3) print(my_list[0]) print(my_list[2]) for item in my_list: print(item)
Copy after login

Output

1 3 1 2 3
Copy after login

Delete elements from dlllist

We can use thepopmethod to remove the element at the last index in the list. In addition, we can use theremovemethod to remove the first occurrence of a specific element in the list of a.

Example

from pyllist import dllist # create an empty doubly-linked list my_list = dllist() my_list.append(1) my_list.append(2) my_list.append(3) value = my_list.pop() print(value) node= my_list.nodeat(1) my_list.remove(node) print(my_list)
Copy after login

Output

3 dllist([1])
Copy after login

Other methods of dllist

In addition to the methods described above, the dllist class also provides several other useful methods -

  • first- Returns the first element of the list.

  • last- Returns the last element of the list.

  • index- Returns the index of the first occurrence of a specific element in the list.

from pyllist import dllist # create an empty doubly-linked list my_list = dllist() my_list.append(1) my_list.append(2) my_list.append(3) print(my_list.first) print(my_list.last)
Copy after login

Output

dllistnode(1) dllistnode(3)
Copy after login

in conclusion

In this article, we discussed the dllist class of the llist module in Python. The Dllist class is used to implement a doubly linked list in python. It provides various methods for adding, removing and iterating elements in a list.

The above is the detailed content of dllist class of llist module in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!