Question

1.      Write a function called deleteNode, which accepts a pointer to a list, and a pointer...

1.      Write a function called deleteNode, which accepts a pointer to a list, and a pointer to the node to be deleted from the list, eg

a. void deleteNode(node *head, node *delNode );

2.      Write a function called insertNode, which accepts a pointer to a list, a pointer to a new node to be inserted, and a pointer to the node after which the insertion takes place, eg

a. void insertNode(node *head, node *newNode, node *prevNode );

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

Ques 1.

void deleteNode(node *head, node *delNode )

{

    // if list or the node to be deleted is empty

    if( !head || !delNode )

        return;

   

    node *trav = head;

   

    // go to the previous node to delNode

    // loop until the list ends

    // assuming next point to the next node

    // if there is a method that returns next, like getNext()

    // the use

    // trav->getNext() != NULL

    while( trav->next != NULL )

    {

        // assuming val stores the data element

        // if there is a method that returns next, like getVal()

        // the use

        // trav->getVal() != NULL

        //

        // assuming next point to the next node

        // if there is a method that returns next, like getNext()

        // the use

        // trav->getNext() != NULL

        if( trav->next->val == delNode->val )

        {

            // remove delNode

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

           

            // remove the memory of the node

            delete delNode;

           

            break;

        }

       

        // go to next node

        // assuming next point to the next node

        // if there is a method that returns next, like getNext()

        // the use

        // trav = trav->getNext()

        trav = trav->next;

    }

}

Ques 2.

void insertNode(node *head, node *newNode, node *prevNode )

{

    // if node i to be inserted at the beginning of the list

    if( !head || !prevNode )

    {

        // make newnode point to head of the list

        newNode->next = head;

       

        // make newNode as the new head

        head = newNode;

    }

    else

    {

        // adjust the pointers

        newNode->next = prevNode->next;

       

        prevNode->next = newNode;

    }

}

Add a comment
Know the answer?
Add Answer to:
1.      Write a function called deleteNode, which accepts a pointer to a list, and a pointer...
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
  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • Write a function insertNode which inserts the given value into a new LLNode at the head...

    Write a function insertNode which inserts the given value into a new LLNode at the head of the given linked list. Updates the head of the linked list using a double pointer. Example call to insert 10 at the head of the linked list: insertNode( &pHead, 10 ); Complete the function below. void insertNode(LLNode **ppHead, int x){

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Fill in the missing code in the following code segment to insert node into list.   ...

    Fill in the missing code in the following code segment to insert node into list.    void SortedType::PutItem(ItemType newItem) {                            NodePtr* newNode;                        // pointer to node being inserted                            NodePtr* predLoc;              // trailing pointer                            NodePtr* location;               // traveling pointer                            boolean moreToSearch;                            location = listData;                            predLoc = NULL;                            moreToSearch = (location != NULL);                            length++;                            // Find insertion point while (moreToSearch)                            { if (location->info < newItem) { predLoc = location; location = location->next; moreToSearch = (location != NULL); }                                        else                                                         moreToSearch = false;                            }                            // Prepare...

  • Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides...

    Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides to create a linked list of int's. Create a Node pointer called ptrHead that points to the head of the list. Make the list 5 nodes long. Generate random values to populate the list's data. All nodes should be in the heap. Add a function called addNodeFront that takes a pointer to the head of a list as an input parameter and that adds...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

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