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 the new node with the head
self.__head = newNode # head points to the new node
self.__size += 1 # Increase list size
if self.__tail == None: # the new node is the only node in list
self.__tail = self.__head
# Add an element to the end of the list
def addLast(self, e):
newNode = Node(e) # Create a new node for e
if self.__tail == None:
self.__head = self.__tail = newNode # The only node in list
else:
self.__tail.next = newNode # Link the new with the last node
self.__tail = self.__tail.next # tail now points to the last node
self.__size += 1 # Increase size
# Same as addLast
def add(self, e):
self.addLast(e)
# Insert a new element at the specified index in this list
# The index of the head element is 0
def insert(self, index, e):
if index == 0:
self.addFirst(e) # Insert first
elif index >= self.__size:
self.addLast(e) # Insert last
else: # Insert in the middle
current = self.__head
for i in range(1, index):
current = current.next
temp = current.next
current.next = Node(e)
(current.next).next = temp
self.__size += 1
# Remove the head node and
# return the object that is contained in the removed node.
def removeFirst(self):
if self.__size == 0:
return None # Nothing to delete
else:
temp = self.__head # Keep the first node temporarily
self.__head = self.__head.next # Move head to point the next node
self.__size -= 1 # Reduce size by 1
if self.__head == None:
self.__tail = None # List becomes empty
return temp.element # Return the deleted element
# Remove the last node and
# return the object that is contained in the removed node
def removeLast(self):
if self.__size == 0:
return None # Nothing to remove
elif self.__size == 1: # Only one element in the list
temp = self.__head
self.__head = self.__tail = None # list becomes empty
self.__size = 0
return temp.element
else:
current = self.__head
for i in range(self.__size - 2):
current = current.next
temp = self.__tail
self.__tail = current
self.__tail.next = None
self.__size -= 1
return temp.element
# Remove the element at the specified position in this list.
# Return the element that was removed from the list.
def removeAt(self, index):
if index < 0 or index >= self.__size:
return None # Out of range
elif index == 0:
return self.removeFirst() # Remove first
elif index == self.__size - 1:
return self.removeLast() # Remove last
else:
previous = self.__head
for i in range(1, index):
previous = previous.next
current = previous.next
previous.next = current.next
self.__size -= 1
return current.element
# Return true if the list is empty
def isEmpty(self):
return self.__size == 0
# Return the size of the list
def getSize(self):
return self.__size
def __str__(self):
result = "["
current = self.__head
for i in range(self.__size):
result += str(current.element)
current = current.next
if current != None:
result += ", " # Separate two elements with a comma
else:
result += "]" # Insert the closing ] in the string
return result
# Clear the list */
def clear(self):
self.__head = self.__tail = None
# Return true if this list contains the element o
def contains(self, e):
print("Implementation left as an exercise")
return True
# Remove the element and return true if the element is in the list
def remove(self, e):
print("Implementation left as an exercise")
return True
# Return the element from this list at the specified index
def get(self, index):
print("Implementation left as an exercise")
return None
# Return the index of the head matching element in this list.
# Return -1 if no match.
def indexOf(self, e):
print("Implementation left as an exercise")
return 0
# Return the index of the last matching element in this list
# Return -1 if no match.
def lastIndexOf(self, e):
print("Implementation left as an exercise")
return 0
# Replace the element at the specified position in this list
# with the specified element. */
def set(self, index, e):
print("Implementation left as an exercise")
return None
# Return elements via indexer
def __getitem__(self, index):
return self.get(index)
# Return an iterator for a linked list
def __iter__(self):
return LinkedListIterator(self.__head)
# The Node class
class Node:
def __init__(self, element):
self.element = element
self.next = None
class LinkedListIterator:
def __init__(self, head):
self.current = head
def __next__(self):
if self.current == None:
raise StopIteration
else:
element = self.current.element
self.current = self.current.next
return element
Please find below the completed code:
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 the new node with the head self.__head = newNode # head points to the new node self.__size += 1 # Increase list size if self.__tail == None: # the new node is the only node in list self.__tail = self.__head # Add an element to the end of the list def addLast(self, e): newNode = Node(e) # Create a new node for e if self.__tail == None: self.__head = self.__tail = newNode # The only node in list else: self.__tail.next = newNode # Link the new with the last node self.__tail = self.__tail.next # tail now points to the last node self.__size += 1 # Increase size # Same as addLast def add(self, e): self.addLast(e) # Insert a new element at the specified index in this list # The index of the head element is 0 def insert(self, index, e): if index == 0: self.addFirst(e) # Insert first elif index >= self.__size: self.addLast(e) # Insert last else: # Insert in the middle current = self.__head for i in range(1, index): current = current.next temp = current.next current.next = Node(e) (current.next).next = temp self.__size += 1 # Remove the head node and # return the object that is contained in the removed node. def removeFirst(self): if self.__size == 0: return None # Nothing to delete else: temp = self.__head # Keep the first node temporarily self.__head = self.__head.next # Move head to point the next node self.__size -= 1 # Reduce size by 1 if self.__head == None: self.__tail = None # List becomes empty return temp.element # Return the deleted element # Remove the last node and # return the object that is contained in the removed node def removeLast(self): if self.__size == 0: return None # Nothing to remove elif self.__size == 1: # Only one element in the list temp = self.__head self.__head = self.__tail = None # list becomes empty self.__size = 0 return temp.element else: current = self.__head for i in range(self.__size - 2): current = current.next temp = self.__tail self.__tail = current self.__tail.next = None self.__size -= 1 return temp.element # Remove the element at the specified position in this list. # Return the element that was removed from the list. def removeAt(self, index): if index < 0 or index >= self.__size: return None # Out of range elif index == 0: return self.removeFirst() # Remove first elif index == self.__size - 1: return self.removeLast() # Remove last else: previous = self.__head for i in range(1, index): previous = previous.next current = previous.next previous.next = current.next self.__size -= 1 return current.element # Return true if the list is empty def isEmpty(self): return self.__size == 0 # Return the size of the list def getSize(self): return self.__size def __str__(self): result = "[" current = self.__head for i in range(self.__size): result += str(current.element) current = current.next if current != None: result += ", " # Separate two elements with a comma else: result += "]" # Insert the closing ] in the string return result # Clear the list */ def clear(self): self.__head = self.__tail = None # Return true if this list contains the element o def contains(self, e): # if linked list is empty if not self.__head: return False temp = self.__head while temp: if temp.element == e: return True temp = temp.next return False # Remove the element and return true if the element is in the list def remove(self, e): if not self.__head: return False index = 0 search = False p = self.__head while p: # searching for element if p.element == e: search = True break index += 1 p = p.next if search: # if element found calling the remove method return self.removeAt(index) return None # Return the element from this list at the specified index def get(self, index): if not self.__head: return False temp = self.__head i = 0 while temp: if i == index:# returning element if found return temp.element temp = temp.next i += 1 return None # Return the index of the head matching element in this list. # Return -1 if no match. def indexOf(self, e): if not self.__head: return -1 temp = self.__head i = 0 while temp: if temp.element == e: return i temp = temp.next i += 1 return -1 # Return the index of the last matching element in this list # Return -1 if no match. def lastIndexOf(self, e): if not self.__head: return -1 temp = self.__head last_index = -1 i = 0 while temp: if temp.element == e: # updating last index last_index = i temp = temp.next i += 1 return last_index # Replace the element at the specified position in this list # with the specified element. */ def set(self, index, e): if not self.__head: return None temp = self.__head i = 0 while temp: if i == index: temp.element = e return True temp = temp.next i += 1 return None # Return elements via indexer def __getitem__(self, index): return self.get(index) # Return an iterator for a linked list def __iter__(self): return LinkedListIterator(self.__head) # The Node class class Node: def __init__(self, element): self.element = element self.next = None class LinkedListIterator: def __init__(self, head): self.current = head def __next__(self): if self.current == None: raise StopIteration else: element = self.current.element self.current = self.current.next return element
I have tested also and its working expected, if u want me to post the test code also please comment i will do it
# please do let me know if u have any concern...
PYTHON -------------------------------------------------------- class LinkedList: def __init__(self): self.__head = None self.__tail = None
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. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList: def __init__(self): self.__head = None self.__tail = None self.__size = 0 def insert(self, i, data): if self.isEmpty(): self.__head = listNode(data) self.__tail = self.__head elif i <= 0: self.__head = listNode(data, self.__head) elif i >= self.__size: self.__tail.setNext(listNode(data)) self.__tail = self.__tail.getNext() else: current = self.__getIthNode(i - 1) current.setNext(listNode(data,...
Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you...
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. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...
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):...
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...
The add(index, e) method inserts an element into the list at the specified index. It can be implemented as follows: public void add(int index, E e) { if (index == 0) addFirst(e); // Insert first else if (index >= size) addLast(e);// Insert last else { // Insert in the middle Node<E> current = head; for (int i = 1; i < index; i++) current = current.next; Node<E> temp = current.next; current.next...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
# 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...