Data Structures and Algorithms in Python
Miriam Antona
Software Engineer
Algorithm: set of instructions that solve a problem
Design
Code
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
insert_at_beginning()
remove_at_beginning()
insert_at_end()
remove_at_end()
insert_at()
remove_at()
search()
def insert_at_beginning(self, data): new_node = Node(data)
if self.head:
def insert_at_beginning(self, data): new_node = Node(data) if self.head: new_node.next = self.head
def insert_at_beginning(self, data): new_node = Node(data) if self.head: new_node.next = self.head self.head = new_node
def insert_at_beginning(self, data): new_node = Node(data) if self.head: new_node.next = self.head self.head = new_node else:
self.tail = new_node self.head = new_node
def insert_at_end(self, data): new_node = Node(data) if self.head:
self.tail.next = new_node
self.tail = new_node
else:
self.head = new_node self.tail = new_node
def search(self, data):
current_node = self.head
while current_node:
if current_node.data == data:
return True
def search(self, data):
current_node = self.head
while current_node:
if current_node.data == data:
return True
else:
current_node = current_node.next
def search(self, data): current_node = self.head while current_node: if current_node.data == data: return True else: current_node = current_node.next
return False
sushi_preparation = LinkedList()
sushi_preparation.insert_at_end("prepare")
sushi_preparation = LinkedList()
sushi_preparation.insert_at_end("prepare")
sushi_preparation.insert_at_end("roll")
sushi_preparation = LinkedList()
sushi_preparation.insert_at_end("prepare")
sushi_preparation.insert_at_end("roll")
sushi_preparation.insert_at_beginning("assemble")
sushi_preparation.search("roll")
True
sushi_preparation.search("mixing")
False
Data Structures and Algorithms in Python