Question

In: Computer Science

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 this… !!! Write in “// code”

int delete (int key) {

// code

}

Solutions

Expert Solution

SOLUTION:

int delete(int key){
//since we are not taking the head as a parameter so we are reading it from the global head
struct Node* temp=head,temp1=null;
  
//iterate through the linked list
while(*temp){
  
//check for the key
if(temp->data==key){
//if key found update the node address
temp1=temp;
}
//move onto the next node
temp=temp->next;
}
//intialy we have defined temp1 as null we are checking that wheher an element found or not if found delete that node
if(temp1){
//get the node address of previous node of the deleting node
struct Node* tmp=temp1->prev;
//link the previous node next to the dele node next node so that one side link will be established
tmp->next=temp1->next;
//for two side link we need to update the dele nodes next nodes previous address by placing the deleting nodes previous address
temp1->next->prev=tmp;
//free the deleting node
free(temp1);
  
}

return 0;

}


Related Solutions

You are asked to delete the last occurrence of an item from a linked list. So,...
You are asked to delete the last occurrence of an item from a linked list. So, for instance: Input: 2 -> 3 -> 2 -> 4 Delete last occurrence of 2, result: 2 -> 3 -> 4 Implement the following method to do the deletion. You may NOT use or implement helper methods - all your code must be implemented inside the given method. You may NOT use recursion. public class Node { public int data; public Node next; }...
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):
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
C# Programming create a Hash Function
C# Programming create a Hash Function
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
ASM Programming instructions: For this week the student’s goal is to write an occurrence finding function...
ASM Programming instructions: For this week the student’s goal is to write an occurrence finding function that builds off of the past two weeks of assembly programming. The requirements for this function are as follows. The user is allowed to type in 10 positive integers plus an occurrence value (meaning 11 inputs). The 10 integers should be handled in a loop where each value input by the user (as in week 1) is stored into memory in an array like...
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT