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

Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
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...
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
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
“Computer programming is creating a sequence of very precise instructions written in a language a computer...
“Computer programming is creating a sequence of very precise instructions written in a language a computer understands, to perform a specified task with a computer.” Discuss in detail the concept of extreme precision in computer programming.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT