Question

In: Computer Science

The programming language is Python Instructions: Create a function that will delete a node in a...

The programming language is Python

Instructions:

Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange).

myLinkedList = LinkedList()

myLinkedList.append("Banana")

myLinkedList.append("Cherry")

myLinkedList.append("Grapes")

myLinkedList.append("Orange")

myLinkedList.prepend("Apple")

myLinkedList.deleteByPositionNum(2)

node = myLinkedList.head

while node:

print(node.value, " ")

node = node.next_node

You may start with the function head:

def deleteByPositionNum(self, positionNum):

Solutions

Expert Solution

class Node:
def __init__(self, value):
self.value = value
self.next_node= None
  
class LinkedList:
  
def __init__(self):
self.head = None
  
def append(self, new_data):
  
new_node = Node(new_data)
  
  
if self.head is None:
self.head = new_node
return
  
last = self.head
while (last.next_node):
last = last.next_node
  
last.next_node = new_node
  
def deleteByPositionNum(self, positionNum):
i=1
temp=self.head
prev=self.head
while(i<=positionNum ):
if (i==1):
i=i+1
continue
prev = temp
temp = temp.next_node
i=i+1
  
if(temp==self.head) :
self.head = temp.next_node
temp = None
return
if(temp == None):
return
  
prev.next_node = temp.next_node
  
temp = None   

if __name__=='__main__':
myLinkedList = LinkedList()
myLinkedList.append("Apple")
myLinkedList.append("Banana")
myLinkedList.append("Cherry")
myLinkedList.append("Grapes")
myLinkedList.append("Orange")
myLinkedList.append("Apple")
myLinkedList.deleteByPositionNum(2)
  
node = myLinkedList.head

while node:

print(node.value)

node= node.next_node

OUTPUT :


Related Solutions

1- Function 1: to delete a node in the head of the list. 2- Function 2:...
1- Function 1: to delete a node in the head of the list. 2- Function 2: to delete a node in the end of the list. 3- Function 3: to delete a node in the middle of the list. Ask the user the value of the node to delete. 4- Test the three functions in the main() and display the new list after each delete. #include <iostream> using namespace std; struct node { int num; node * nextptr; }*head,*curnode; node...
C PROGRAMMING Create the int delete(int key) function so that it deletes the LAST occurrence of...
C PROGRAMMING Create the int delete(int key) function so that it deletes the LAST occurrence of a given number in the linked list Make sure the parameter for this delete function is (int key). Also, use these variables and global Nodes BELOW as this is a DOUBLY LINKED LIST!!! #include #include typedef struct node {             int data;             struct node *next;             struct node *prev; } Node; Node *head; Node *tail; ----------------------- So, the function has to look like...
R programming language A) Create a function that accepts two arguments, an integer and a vector...
R programming language A) Create a function that accepts two arguments, an integer and a vector of integers. It returns the count of the number of occurrences of the integer in the input vector. 1]Input: num_count <-function ??? 2]Input: num_count(2,c(1,1,2,2,3,3)) 2] Output: 2 3] Input: num_count(1,c(1,1,2,2,3,1,4,5,5,2,2,1,3)) 3] Output : 4 B) Create a function that accepts 3 integer values and returns their sum. However, if an integer value is evenly divisible by 3, then it does not count towards the...
PYTHON: This is the posted problem: General Instructions Create a function called DeterminePrice that will determine...
PYTHON: This is the posted problem: General Instructions Create a function called DeterminePrice that will determine the cost of purchased software. The price is of the software is $350 per license. However, when purchased in larger quantities a discount is given. For quantites less than 10 copies, there is no discount. For quantities greater than 10 and less than and including 20, a 10% discount is given. For quantities greater than 20 and less than and including 30, a discount...
How to create a divide function in (Dr Racket) programming language without using the built in...
How to create a divide function in (Dr Racket) programming language without using the built in function " / " ?
C# Programming create a Hash Function
C# Programming create a Hash Function
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 =...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 = 5. Also suppose, Jane iterates the number from 1 to 15. At the beginning, Jane sets count to 0, and then proceeds iterating the number from 1 to 15 and for each iteration does the following: for 1, count is increased by 1 because it is not divisible by 3, 4, and 5; count is now: 1 for 2, count is increased by 2...
Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a...
Programming language is python 3 For this project, you will import the json module. Write a...
Programming language is python 3 For this project, you will import the json module. Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you. The...
a summary explaining the basic understanding of the following programming concepts using a language of python:...
a summary explaining the basic understanding of the following programming concepts using a language of python: •Variables •Arithmetic and Logical operations •Sequential coding (Structured programming •Decision structure (If statements) •Repetition structure •Functions with some coding demos inside visual studio code python IDE which can be sent as screenshot. preferably a typed summary please which can be written into powerpoint pleaseeeee ???
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT