Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.

Question
For algorithms, in python, if you have a Node class, such as:

class Node(object):
def __init__(self, data):
self.data = data
self.next = None
Why is it still better to create a seperate SinglyLinkedList class, as below, rather than just constructing the link list directly from the objects of the Node class?

class SinglyLinkedList:
def __init__(self):
self.first = None
def append(self, data):
node = Node(data)
if self.first == None:
self.first = node
else:
current = self.first
while current.next:
current = current.next
current.next = node
Answer
To abstract away the internals of the Node class from the programmer/user (e.g. the programmer using the SinglyLinkedList class's append method, just adds items to the linked list without having to know about the details of the Node class and all the pointer, etc or even knowing that this is a linked list used to append/hold the items, aside from the name of the class)

Question
For algorithms, in python, if you have a Node class, such as:

class Node(object):
def __init__(self, data):
self.data = data
self.next = None
Why is it still better to create a seperate SinglyLinkedList class, as below, rather than just constructing the link list directly from the objects of the Node class?

class SinglyLinkedList:
def __init__(self):
self.first = None
def append(self, data):
node = Node(data)
if self.first == None:
self.first = node
else:
current = self.first
while current.next:
current = current.next
current.next = node
Answer
?

Question
For algorithms, in python, if you have a Node class, such as:

class Node(object):
def __init__(self, data):
self.data = data
self.next = None
Why is it still better to create a seperate SinglyLinkedList class, as below, rather than just constructing the link list directly from the objects of the Node class?

class SinglyLinkedList:
def __init__(self):
self.first = None
def append(self, data):
node = Node(data)
if self.first == None:
self.first = node
else:
current = self.first
while current.next:
current = current.next
current.next = node
Answer
To abstract away the internals of the Node class from the programmer/user (e.g. the programmer using the SinglyLinkedList class's append method, just adds items to the linked list without having to know about the details of the Node class and all the pointer, etc or even knowing that this is a linked list used to append/hold the items, aside from the name of the class)
If you want to change selection, open document below and click on "Move attachment"

#### pdf

owner: kkhosravi - (no access) - PYTHON_DATA_STRUCTURES_AND_ALGORITHMS.pdf, p108

#### Summary

status measured difficulty not learned 37% [default] 0

No repetitions

### Discussion

Do you want to join discussion? Click here to log in or create user.