In: Computer Science
(Python) Define a function makeTwoWay that expects a singly linked structure as its argument. The function builds and returns a doubly linked structure that contains the items in the singly linked structure. (Note: The function should not alter the argument's structure.)
node.py source code:
"""
File: node.py
Node classes for one-way linked structures and two-way
linked structures.
"""
class Node(object):
def __init__(self, data, next = None):
"""Instantiates a Node
with default next of None"""
self.data = data
self.next = next
class TwoWayNode(Node):
def __init__(self, data, previous = None,
next = None):
Node.__init__(self,
data, next)
self.previous =
previous
# Just an empty link
node1 = None
# A node containing data and an empty link
node2 = Node("A", None)
# A node containing data and a link to node2
node3 = Node("B", node2)
#Phython code
class Node(object):
def __init__(self, data, next = None):
"""Instantiates a Node
with default next of None"""
self.data = data
self.next = next
class TwoWayNode(Node):
def __init__(self, data, previous = None,
next = None):
Node.__init__(self,
data, next)
self.previous =
previous
def makeTwoWay(sll):
head = None
prev = None
while sll != None:
if head == None:
head = TwoWayNode(sll.data , prev)
prev = head
else:
prev.next = TwoWayNode(sll.data , prev)
prev = prev.next
sll = sll.next
return head
# Just an empty link
node1 = None
# A node containing data and an empty link
node2 = Node("A", None)
# A node containing data and a link to node2
node3 = Node("B", node2)
head = makeTwoWay(node3)
#Screeshot