Linked Lists
Advantages
Disadvantages
领英推荐
Real-Life Applications
Implementation
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
node = Node(data)
if self.head is None:
self.head = node
else:
runner = self.head
while runner.next is not None:
runner = runner.next
runner.next = node
def remove(self, data):
if self.head is None:
return False
if self.head.data == data:
self.head = self.head.next
return True
previous = None
runner = self.head
while runner is not None and runner.data != data:
previous = runner
runner = runner.next
# 'data' not found
if runner is None:
return False
previous.next = runner.next
return True
def display(self):
elements = []
current = self.head
while current is not None:
elements.append(current.data)
current = current.next
print(elements)
More code implementations of LinkedList (in all of your favourite languages - Java, Swift, C++) can be found here.
That's a wrap. Subscribe if you haven't already. Come back tomorrow for more.
And if you found this useful, share it with your friends.
Building AudioCity | Major in Computer Science @ VIT AP UNIVERSITY
7 个月Very helpful!