Question

In: Computer Science

In Python, I've created a Node class for implementing a singly linked list. My Code: class...

In Python, I've created a Node class for implementing a singly linked list.

My Code:

class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext

class SinglyLinkedList:

def __init__(self):
self.head = None

def add(self,key):
addkey = Node(key)
addkey.setNext(self.head)
self.head = addkey

Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding another instance variable. You will need to make a modification to the add method as well.

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

Please see the BOLD code for updated code.

class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
  
class SinglyLinkedList:
  
def __init__(self):
self.head = None
# Add a tail instance variable for the linked list
# So that we can know the end of the linked list.
self.tail = self.head
  
def add(self,key):
addkey = Node(key)
# We have to set the head's next to the addKey Node.
self.head.setNext(addKey)
# Make the tail point to newly created node.
self.tail = addKey
  
# Append at the last
def append(self,key):
# create a node
appendKey=Node(key)
  
# append it to the tail of the SinglyLinkedList
self.tail.next=appendKey
  
# make this appendKey node as the new tail
self.tail=appendKey
  

  

=======


Related Solutions

I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT