Home  >  Article  >  Backend Development  >  dllist class of llist module in Python

dllist class of llist module in Python

WBOY
WBOYforward
2023-09-10 08:45:111337browse

dllist class of llist module in Python

dllist is a class of the llist module in Python, which is used to implement a doubly linked list and has the functions of inserting, deleting, and traversing elements. The dllist class provides methods for adding, removing, and iterating lists in both directions. In this article, we will take a closer look at the dllist class 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()

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)

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)

Output

1
3
1
2
3

Delete elements from dlllist

We can use the pop method to remove the element at the last index in the list. In addition, we can use the remove method 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)

Output

3
dllist([1])

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)

Output

dllistnode(1)
dllistnode(3)

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete