Question

In: Computer Science

Add the operation Insert to the linkedListClass. An Insert operation inserts a new item after a...

Add the operation Insert to the linkedListClass. An Insert operation inserts a new item after a given key in the linked list. The method headline can be void Insert(int item, int key), where the first parameter is the new item, and the second parameter is the key of the item before the new item.

Solutions

Expert Solution

void Insert(int item, int key) {
        
        Node *start = head; // Assuming class has instance member head which points to first node.
        
        if(start == NULL) {
                // list has no nodes till now. Create a new node with item
                // as this is the first node in list, its next should be null.
                head = new Node();
                head->data = item;
                head->next = NULL;
                return;
        }

// The list is not empty at this point, Hence, We need to find a node which has the key data
// or else if the key node does not exist in the list, we should reach to last node
// and insert the new node after that

        // add new node after the given key
        while(start->next != NULL && start->data != key) {
                start = start->next;
        }
        
// making the new node next node as the start node where key data exists
        Node *tmp = new Node();
        tmp->data = item;
        tmp->next = start->next;
        start->next = tmp;
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Add a new method called insert_in_order() which accepts a number as its parameter and inserts that...
Add a new method called insert_in_order() which accepts a number as its parameter and inserts that number in the linked list in an ascending (sorted) order. Note that this method should be called for all the numbers (1, 5, 19, 7, 23, 17, 2) and the resulting linked list should come out sorted. (1, 2, 5, 7, 17, 19, 23) JAVA CODE BELOW class aNode { char data; aNode next; aNode(char mydata) { // Constructor data = mydata; next =...
JAVA Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
A fast food chain plans to add a new item to its menu. There are three...
A fast food chain plans to add a new item to its menu. There are three possible campaigns for the new menu item. They introduced the product at locations in several randomly selected markets. A different promotion was used at each location, and the weekly sales of the new item are recorded for the first four weeks. Management is interested in determining the differences in the promotion campaigns with regard to total sales. In addition, it wants to know if...
AVL tree; Insert and Range Minimum operation in Java code.
AVL tree; Insert and Range Minimum operation in Java code.
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
“We really need to get this new material-handling equipment in operation just after the new year...
“We really need to get this new material-handling equipment in operation just after the new year begins. I hope we can finance it largely with cash and marketable securities, but if necessary we can get a short-term loan down at MetroBank.” This statement by Beth Davies-Lowry, president of Intercoastal Electronics Company, concluded a meeting she had called with the firm’s top management. Intercoastal is a small, rapidly growing wholesaler of consumer electronic products. The firm’s main product lines are small...
“We really need to get this new material-handling equipment in operation just after the new year...
“We really need to get this new material-handling equipment in operation just after the new year begins. I hope we can finance it largely with cash and marketable securities, but if necessary we can get a short-term loan down at MetroBank.” This statement by Beth Davies-Lowry, president of Intercoastal Electronics Company, concluded a meeting she had called with the firm’s top management. Intercoastal is a small, rapidly growing wholesaler of consumer electronic products. The firm’s main product lines are small...
“We really need to get this new material-handling equipment in operation just after the new year...
“We really need to get this new material-handling equipment in operation just after the new year begins. I hope we can finance it largely with cash and marketable securities, but if necessary we can get a short-term loan down at MetroBank.” This statement by Beth Davies-Lowry, president of Intercoastal Electronics Company, concluded a meeting she had called with the firm’s top management. Intercoastal is a small, rapidly growing wholesaler of consumer electronic products. The firm’s main product lines are small...
“We really need to get this new material-handling equipment in operation just after the new year...
“We really need to get this new material-handling equipment in operation just after the new year begins. I hope we can finance it largely with cash and marketable securities, but if necessary we can get a short-term loan down at MetroBank.” This statement by Beth Davies-Lowry, president of Intercoastal Electronics Company, concluded a meeting she had called with the firm’s top management. Intercoastal is a small, rapidly growing wholesaler of consumer electronic products. The firm’s main product lines are small...
“We really need to get this new material-handling equipment in operation just after the new year...
“We really need to get this new material-handling equipment in operation just after the new year begins. I hope we can finance it largely with cash and marketable securities, but if necessary we can get a short-term loan down at MetroBank.” This statement by Beth Davies-Lowry, president of Intercoastal Electronics Company, concluded a meeting she had called with the firm’s top management. Intercoastal is a small, rapidly growing wholesaler of consumer electronic products. The firm’s main product lines are small...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT