Question

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 a node to be remove it will be, at the latest, the third to last node.

Node*deleteTwo(Node*list, int valal ){

/*example usage might be:

int main() {
    Node* head;
    Node* tail;
    //Some code to create a list with head 3, 2, 1 tail
    head = deleteTwo(head,3);
    //The list is now head 1 tail

}*/

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

void deleteTwo(Node *list, int val)

{

    // if te list is empty

    if( list == NULL )

        return;

   

    // if the node to be deleted is the first node in the list

    if( list->data == val )

    {

        // remove the node

        list = list->next;

       

        // delete the previous head node

        delete list->prev;

       

        list->prev = NULL;

       

        // if there is a node after the previous head of the list

        if( list != NULL )

        {

            // remove the node

            list = list->next;

           

            // delete the previous head node

            delete list->prev;

           

            list->prev = NULL;

        }

    }

    else

    {

        // point trav to the first node of the list

        Node *trav = list;

       

        // traverse the list untill the list ends or trav points to

        // the node with value val

        while( trav != NULL && trav->data != val )

            // go to next node

            trav = trav->next;

           

        // if the list doesn't contain val

        if( trav == NULL )

            return;

       

        // remove the node

        trav->prev->next = trav->next;

       

        // make trav point to the next node of th etraget node

        trav = trav->next;

       

        // make the target node point by temp

        Node *temp = trav->prev;;

       

        // adjust the prev pointer

        trav->pre = trav->pre->pre;

       

        // delete the previous head node

        delete temp;

       

       // if there is a node after the previous target node

        if( list != NULL )

        {

            // remove the node

            trav->prev->next = trav->next;

           

            // make trav point to the next node of th etraget node

           trav = trav->next;

           

            // make the target node point by temp

            Node *temp = trav->prev;;

           

            // adjust the prev pointer

            trav->pre = trav->pre->pre;

           

            // delete the previous head node

            delete temp;

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...
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
  • Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has...

    Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • 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...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • 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...

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