class Node(object):
def __init__(self, data, next=None):
self.data = data
self.next = next
class List(object):
def __init__(self):
self.head = None
self.tail = None
Implement the following functions for Linked List in Python and use the constructors above :
Copy(lList) Builds and returns a copy of list
ItemAt(List,i) Returns the data item at position i in list Pop(List,i=0)
Remove item at position i in list. If i is not specified, it removes the first item in list
Count(List,x) Returns the number of times x appears in list.
Index(List,x) Returns the index of the first item whose value is equal to x list
Clear(List) Removes all items from list.
Sublist(list,start=0,end=GetLength(List)) Builds and returns a sublist of list, from element start to element end (not inclusive).
all functions must use pass by reference
CODE :
class Node(object):
def __init__(self,data,next=None):
self.data = data
self.next = next
def __repr__(self):
return str(self.data);
class List(object):
def __init__(self):
self.head = None
self.tail = None
def add(self,x):
if self.head is None:
self.head = Node(x)
self.tail = self.head
return
temp = self.head
while temp.next is not None:
temp = temp.next
temp.next = Node(x)
self.tail = temp.next
def print(self):
temp = self.head
while temp is not None:
print(temp.data,'->',end='')
temp = temp.next
print()
def Copy(self):
iList = self
if iList.head is None:
return None
temp = iList.head
newhead = Node(iList.head.data)
temp2 = newhead
prevv = newhead
while temp.next is not None:
temp = temp.next
temp2.next = Node(temp.data)
prevv = temp
temp2 = temp2.next
ans = List()
ans.head = newhead
ans.tail = prevv
return ans
def ItemAt(self,i):
iList = self
if i==0:
return iList.head
temp = iList.head
for i in range(i):
temp = temp.next
return temp
def Pop(self,i=0):
iList = self
if i==0:
iList.head = iList.head.next
return
prev = iList.head
temp = iList.head
for x in range(i):
prev = temp
temp = temp.next
cnext = temp.next
prev.next = cnext
return temp
def Count(self,x):
iList = self
temp = iList.head
ans = 0
while temp is not None:
if temp.data == x:
ans+=1
temp = temp.next
return ans
def Index(self,x):
iList = self
ind = 0
temp = iList.head
while temp is not None:
if temp.data==x:
return ind
ind+=1
temp = temp.next
def Clear(self):
iList = self
iList.head = None
iList.tail = None
def GetLength(self):
iList = self
ans = 0
temp = iList.head
while temp is not None:
ans += 1
temp = temp.next
return ans
def SubList(self,start=0,end=0):
iList = self
temp = iList.head
for x in range(start):
temp = temp.next
print(temp)
ans = Node(temp.data)
temp2 = ans
prev = ans
for i in range(end-start):
temp = temp.next
temp2.next = Node(temp)
prev = temp2
temp2= temp2.next
#temp = temp.next
fans = List()
fans.head = ans
fans.tail = prev
return fans
l = List()
l.add(2)
l.add(3)
l.add(4)
l.add(5)
l.add(6)
print('List is ')
l.print()
print('Copied')
cpy = l.Copy()
cpy.print()
print('sublist 1-4')
l.SubList(1,4).print()
OUTPUT :
List is
2 ->3 ->4 ->5 ->6 ->
Copied
2 ->3 ->4 ->5 ->6 ->
sublist 1-4
3
3 ->4 ->5 ->6 ->
class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object): def...
""" Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python class. Name of list is LList class node(object): def __init__(self, data, next=None): self.data = data self.next = next # Note: use the attributes directly; no setters or getters! class LList(object): def __init__(self): self._size = 0 self._head = None self._tail = None """ def set_data_at_index(self, idx, val): """ The value stored at index idx changes to val return True if the index was valid otherwise...
PYTHON -------------------------------------------------------- class LinkedList: def __init__(self): self.__head = None self.__tail = None self.__size = 0 # Return the head element in the list def getFirst(self): if self.__size == 0: return None else: return self.__head.element # Return the last element in the list def getLast(self): if self.__size == 0: return None else: return self.__tail.element # Add an element to the beginning of the list def addFirst(self, e): newNode = Node(e) # Create a new node newNode.next = self.__head # link...
Finish each function python 3 LList.py class node(object): """ A version of the Node class with public attributes. This makes the use of node objects a bit more convenient for implementing LList class. Since there are no setters and getters, we use the attributes directly. This is safe because the node class is defined in this module. No one else will use this version of the class. ''' def __init__(self, data, next=None): """ Create a new node for...
Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...
Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...
Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Question 2.1. Empty Node In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None. Add...
Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...
Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...
# Declaring Node class class Node: def __init__(self, node_address): self.address = node_address self.next = None # Creating empty list forwarding_list = Node() # Recurssive Method for add address def addAddress(node): if node.address == old_address: # Address is same if node.next == None: # Next is None node.next = Node(new_address) # Add Next print("Added") # Addedd else: # Next is not none print("Entry already exists") elif node.next != None: # Check Next Node addAddress(node.next, old_address, new_address) def addEntry(forwarding_list):# Method for add...
class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right def insert_left(self, new_data): if self.__left == None: self.__left = BinaryTree(new_data) else: t = BinaryTree(new_data, left=self.__left) self.__left = t def insert_right(self, new_data): if self.__right == None: self.__right = BinaryTree(new_data) else: t = BinaryTree(new_data, right=self.__right) self.__right = t def get_left(self): return self.__left def get_right(self): return self.__right def set_data(self, data): self.__data = data def get_data(self): return self.__data def set_left(self, left): self.__left = left def set_right(self, right): self.__right...