Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
dllist class of llist module in Python
The dllist is a class of the llist module in Python that is used to implement a doubly linked list with functionality for insertion, deletion, and traversal of elements. The dllist class provides methods for adding, removing, and iterating over the list in both directions.
Creating a 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 a doubly-linked list ?
from pyllist import dllist
# create an empty doubly-linked list
my_list = dllist()
print("Empty dllist:", my_list)
Adding Elements to a dllist
We can add elements to the dllist using the append() method which takes the element as an argument and adds it to the end of the list ?
from pyllist import dllist
my_list = dllist()
my_list.append(1)
my_list.append(2)
my_list.append(3)
print("After adding elements:", my_list)
Accessing Elements in a dllist
We can access the elements of the dllist using bracket notation, similar to how we access elements of a regular list. We can also iterate through all elements using a for loop ?
from pyllist import dllist
# create and populate the list
my_list = dllist()
my_list.append(1)
my_list.append(2)
my_list.append(3)
print("First element:", my_list[0])
print("Third element:", my_list[2])
print("All elements:")
for item in my_list:
print(item)
Removing Elements from dllist
We can use the pop() method to remove and return the element at the last index. We can also use the remove() method to remove a specific node from the list ?
from pyllist import dllist
# create and populate the list
my_list = dllist()
my_list.append(1)
my_list.append(2)
my_list.append(3)
# remove last element
value = my_list.pop()
print("Popped value:", value)
print("List after pop:", my_list)
# remove specific node
node = my_list.nodeat(1)
my_list.remove(node)
print("List after removing node at index 1:", my_list)
Other Useful Methods
The dllist class provides several additional methods for working with doubly linked lists ?
from pyllist import dllist
# create and populate the list
my_list = dllist()
my_list.append(10)
my_list.append(20)
my_list.append(30)
print("First node:", my_list.first)
print("Last node:", my_list.last)
print("Size of list:", my_list.size)
# Insert at beginning
my_list.appendleft(5)
print("After appendleft(5):", my_list)
Common Methods Summary
| Method | Description |
|---|---|
append(item) |
Add item to the end |
appendleft(item) |
Add item to the beginning |
pop() |
Remove and return last element |
popleft() |
Remove and return first element |
first |
Return first node |
last |
Return last node |
size |
Return number of elements |
Conclusion
The dllist class provides an efficient implementation of doubly linked lists in Python. It offers methods for adding, removing, and traversing elements in both directions, making it useful for applications requiring frequent insertions and deletions.
