In Python, there is no built-in data structure specifically called an "unordered list." However, we can implement an unordered list using a linked list which consists of nodes where each node contains a data element and a reference to the next node. Linked lists can be either ordered or unordered based on how elements are inserted and maintained.
Implementing an Unordered List Using a Linked List
Node Class
A typical node in a linked list can be represented as a Python class:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
- Purpose: Represents each element in the linked list.
- Attributes: data stores the value and next points to the next node.
UnorderedList Class
The UnorderedList class manages the nodes and provides methods to perform various operations on the unordered list. Here's the implementation of the class:
class UnorderedList:
def __init__(self):
self.head = None
- Purpose: Manages the linked list.
- Attributes:
headpoints to the first node in the list.
Operations in Unordered List
Now, let's define some key methods for our unordered list:
1. Check if the List is Empty:
To check whether the list is empty, we can simply check if the head of the list is None.
def is_empty(self):
return self.head is None
2. Add an Item:
The add() method adds a new node at the beginning of the list. Since it’s an unordered list, the new node is added without worrying about the order.
def add(self, item):
new_node = Node(item)
new_node.next = self.head
self.head = new_node
3. Get the Size of the List:
The size() method counts the number of nodes in the list.
def size(self):
current = self.head
cnt = 0
while current:
cnt += 1
current = current.next
return cnt
4. Search for an Item:
The search() method checks if a given item exists in the list.
def search(self, item):
current = self.head
while current:
if current.data == item:
return True
current = current.next
return False
5. Remove an Item:
The remove() method removes the first occurrence of a given item from the list.
def remove(self, item):
current = self.head
previous = None
while current:
if current.data == item:
if previous:
previous.next = current.next
else:
self.head = current.next
return True
previous = current
current = current.next
return False
6. Append an Item:
The append() method adds an item at the end of the list.
def append(self, item):
new_node = Node(item)
if self.is_empty():
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_node
7. Display the List:
The display() method prints all elements in the list.
def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
Using the UnorderedList Class
Here's how you can use the UnorderedList class to perform basic operations:
ul = UnorderedList()
ul.add(31)
ul.add(77)
ul.add(17)
ul.add(93)
ul.add(26)
ul.add(54)
ul.display()
print("Size:", ul.size())
print("Search 93:", ul.search(93))
print("Search 100:", ul.search(100))
ul.remove(93)
ul.display()
ul.append(100)
ul.display()
Output:
54 -> 26 -> 93 -> 17 -> 77 -> 31 -> None
Size: 6
Search 93: True
Search 100: False
54 -> 26 -> 17 -> 77 -> 31 -> None
54 -> 26 -> 17 -> 77 -> 31 -> 100 -> None
Explanation:
- add(item): Adds an item at the beginning of the list.
- size(): Returns the number of items in the list.
- search(item): Searches for an item and returns True if found, False otherwise.
- remove(item): Removes the first occurrence of an item from the list.
- append(item): Appends an item at the end of the list.
- display(): Displays all elements in the list.
Related Articles: