class Book: def __init__(self,id,bookName,authorName,nextNode=None):
self.id = id
self.bookName = bookName
self.authorName = authorName
self.nextNode = nextNode def getId(self): return self.id
def getBookName(self): return self.bookName def getAuthorName(self): return self.authorName def getNextNode(self): return self.nextNode
def setNextNode(self,val):
self.nextNode = val
class LinkedList: def __init__(self,head = None):
self.head = head
self.size = 0
def getSize(self): return self.size def AddBookToFront(self,newBook):
newBook.setNextNode(self.head)
self.head = newBook
self.size+=1
def DisplayBook(self):
curr = self.head while curr: print(curr.getId(),curr.getBookName(),curr.getAuthorName())
curr = curr.getNextNode() def RemoveBookAtPosition(self,n):
prev = None
curr = self.head
curPos = 0
while curr: if curPos == n: if prev:
prev.setNextNode(curr.getNextNode()) else:
self.head = curr.getNextNode()
self.size = self.size - 1
return True
prev = curr
curr = curr.getNextNode()
curPos = curPos + 1
return False
def AddBookAtPosition(self,newBook,n):
curPos = 1
if n == 0:
newBook.setNextNode(self.head)
self.head = newBook
self.size+=1
return
else:
currentNode = self.head while currentNode.getNextNode() is not None: if curPos == n:
newBook.setNextNode(currentNode.getNextNode())
currentNode.setNextNode(newBook)
self.size+=1
return
currentNode = currentNode.getNextNode()
curPos = curPos + 1
if curPos == n:
newBook.setNextNode(None)
currentNode.setNextNode(newBook)
self.size+=1
else: print("cannot add",newBook.getId(),newBook.getBookName(),"at that position") def SortByAuthorName(self): for i in range(1,self.size):
node1 = self.head
node2 = node1.getNextNode() while node2 is not None: if node1.authorName > node2.authorName:
temp = node1.id
temp2 = node1.bookName
temp3 = node1.authorName
node1.id = node2.id
node1.bookName = node2.bookName
node1.authorName = node2.authorName
node2.id = temp
node2.bookName = temp2
node2.authorName = temp3
node1 = node1.getNextNode()
node2 = node2.getNextNode()
myLinkedList = LinkedList()
nodeA = Book("#1","cool","Isaac")
nodeB = Book("#2","amazing","Alfred")
nodeC = Book("#3","hello","John")
nodeD = Book("#4","why","Chase")
nodeE = Book("#5","good","Mary")
nodeF = Book("#6","hahaha","Radin")
myLinkedList.AddBookToFront(nodeA)
myLinkedList.AddBookToFront(nodeB)
myLinkedList.AddBookToFront(nodeC)
myLinkedList.AddBookAtPosition(nodeD,1)
myLinkedList.AddBookAtPosition(nodeE,1)
myLinkedList.AddBookAtPosition(nodeF,1)
myLinkedList.RemoveBookAtPosition(2)
myLinkedList.RemoveBookAtPosition(2)
myLinkedList.DisplayBook()
myLinkedList.SortByAuthorName()print(myLinkedList.getSize())
myLinkedList.DisplayBook()We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
If I wanted to create a method called getNum(n) that should return the data item from the node in the nth spot in the list. I am stuck and getting errors when I write my method. I have two files already, the first file linkedlist.py has the code .... ''' class Node(object): def __init__(self, data=None, nextNode=None): self.data = data self.nextNode = nextNode class linkedList(object): def __init__(self, head=None): self.head = head self.size = 0 def insert(self, node): if not self.head: self.head...
Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...
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...
from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...
In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...
Below is Python code for logic gates. It is written for a specific Circuit. Do the following: 1) Fill in the appropriate If , Else statement for the OrGate ( Look at the AndGate for a hint ) 2) Create two new objects, the NandGate and the NorGate classes. 3) Look at the AndGate class. What does this class inherit and use from the BinaryGate class? class LogicGate: def __init__(self,n): self.name = n self.output = None def getName(self): return self.name...
Python 3+ Adjust the following code so the two errors below are non-present: 1. __init__() : Since Pizza object don't have it's own set() datastructure, the toppings in the pizza object are manimupated from out of the object. (-1.0) 2. __eq__() : How about "self.toppings == other.toppings" rather than "self.toppings - other.toppin == set())". Code: ## Program 1 ## --------------------------------- class Pizza: def __init__(self, s='M', top=set()): self.setSize(s) self.toppings = top def setSize(self, s): self.size = s def getSize(self): return self.size...
Please code in Python Revise the AbstractBag class so that it behaves as a subclass of AbstractCollection provided in the file abstractcollection.py. Abstractcollection.py code: class AbstractCollection(object): """An abstract collection implementation.""" # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" self.size = 0 if sourceCollection: for item in sourceCollection: self.add(item) # Accessor methods def isEmpty(self): """Returns True if len(self) == 0, or False otherwise.""" return len(self) == 0...
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...
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,...