Question

How do you remove a value that has occurred multiply times in a doubly linked list...

How do you remove a value that has occurred multiply times in a doubly linked list , the value is in the front , middle and end of the linked list and is also given as a parameter in your method
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Struct Node
{
int data; // represents the data present in the node;
Struct Node * prev; // points to the previous node
Struct Node * next; // points to the next node

}

void deleteDuplicateElements(struct Node *head, int duplicateValue)
{
   if( head == NULL || head->next == NULL) return; // if list is empty  
   struct Node *ptr, *temp;
   ptr = head;
  
   // if duplicate value present in first or middle nodes
   // this loop runs till last but one
   for(; ptr->next != NULL; ptr= ptr->next)
   {
       if(ptr->data == duplicateValue)
       {
           temp = ptr; // temp is the current node now
           ptr = ptr->next; // ptr which was pointing to the current node, now points to the next node
           ptr->prev = temp->prev;
           free(temp); // free the current node having the duplicate value
       }
   }
   if( ptr->data == duplicateValue )   // if the last node has the duplicateValue
   {
    ptr->prev->next = NULL; // make the last but one node the last node of the double linked list
       free(ptr); return;
   }
   else
   return;
}

Add a comment
Know the answer?
Add Answer to:
How do you remove a value that has occurred multiply times in a doubly linked list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubl...

    3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks) 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks)

  • JAVA please Java problem that has to be in doubly linked list. It is game problem...

    JAVA please Java problem that has to be in doubly linked list. It is game problem that I have to keep of the players' scores. this should have a Java blueprint class named player   It should have two fields, the name and score and include the constructors, toString() method, and getters and setters. Scores must be kept in ascending order (smallest at the front ) in a doubly linked list. It has menu as well: Load original data Print the...

  • 1) Create a portfolio class which stores stocks in doubly-linked list. 2) Portfolio class has load...

    1) Create a portfolio class which stores stocks in doubly-linked list. 2) Portfolio class has load and store functions to keep all its stocks on files. 3) How do you prove that the portfolio is indeed linked correctly in both directions? (Want one answer? highlight next line) how about print and reverse print? 4) Create your own test data such that you can demonstrate stock insertion and deletion at the beginning / middle / end of the portfolio.

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

  • When deleting an element in the middle of a doubly linked list how many pointers need...

    When deleting an element in the middle of a doubly linked list how many pointers need to be reset Select one O a. 2 O.O Oc1 nion

  • Recursion and Doubly Linked Lists Problem #1: Assume you have a doubly linked list of single...

    Recursion and Doubly Linked Lists Problem #1: Assume you have a doubly linked list of single characters. Plan the algorithm to insert character 'a' immediately before each character 'b' that is encountered. 3. What is the simple case: (what case does not require a loop?) How do we get to the next a smaller sub-problem? What information is needed for the function call: 4. If we insert ‘a, PRIOR to the recursive call, what do we need to be careful...

  • 1. How many pointers are contained in the nodes of a circularly doubly-linked list with a...

    1. How many pointers are contained in the nodes of a circularly doubly-linked list with a sentinel node of five data nodes? 2.given a circularly doubly-linked list with a sentinel node where each node has two references(forw and back); one that points to the next node and another that points to the previous node. Assume the linked list below: SENTINEL - 30 - 70 - 90 - 50 - 10 Provide the output for the following statement Print( head->forw->forw->forw->forw->back->data) 3.given...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT