Question

In: Computer Science

In C++ please. 3. splice() is a specialized method that inserts one list into another in...

In C++ please.

3. splice() is a specialized method that inserts one list into 
   another in constant time but destroys the first list. Explain
   why this may be implemented for the list but not for any other
   sequential container. Give an example usage of splice()

Solutions

Expert Solution

3. splice() is a specialized method that inserts one list into 
   another in constant time but destroys the first list. Explain
   why this may be implemented for the list but not for any other
   sequential container. Give an example usage of splice()

Ans:-

The list::splice() may be a built-in function in C++ STL which is employed to transfer elements from one list to a different .
We can use splice() function in 3 ways:
1.Transfer all the weather of list x into another list at some position.
2.Transfer only the element pointed by i from list x into another list at some position.
3.Transfers the range [first, last] from list x into another list at some position.

The splice() function move the element of list from one list to another list. But also at the time it's remove from one list to another list. splice() is a specialized method that inserts one list into another in constant time but destroys the first list. Rather it's change or empty the first lilst.

It's implement only for list because it's use list's index values for use splice function or we can say that to pass the parameters to the splice() function.

Syntax:

list1Name.splice (iterator position, list2)
                or 
listName.splice (iterator position, list2, iterator i)
                or 
list1Name.splice (iterator position, list2, iterator first, iterator last)

Parameters:

  • position – Specifies the position where the weather are to be transfered.
  • list2 – It specifies an inventory object of an equivalent type which is to be transfered.
  • i – It specifies an iterator to the position of a component in list2 which is to be transfered.
  • first, last – Iterators specifying a variety of elements in list2 which is to be transferred in list1. The range includes all the weather between first and last, including the element pointed by first but not the one pointed by last.
 usage of splice():-

The use of splice() function to remove element from one list at constant time and add it to anther list.

Example:-

// list::splice() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // init lists 
    list<int> list1 = { a, b, c }; 
    list<int> list2 = { d, e }; 
    list<int> list3 = { f, g, h }; 
  
    // transfer all the elements of list2 
    list1.splice(list1.begin(), list2); 
  
    // at the beginning of list1 
    cout << "list list1 after splice operation" << endl; 
    for (auto x : list1) 
        cout << x << " "; 
  
    // transfer all the elements of list1 
    list3.splice(list3.end(), list1); 
  
    // at the end of list3 
    cout << "\nlist list3 after splice operation" << endl; 
    for (auto x : list3) 
        cout << x << " "; 
    return 0; 
} 

Output:-

list list1 after splice operation
d e a b c
list list3 after splice operation
f g h d e a b c

Related Solutions

please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
please modify the method public void addList(SinglyLinkedList<E> s) that add list to another with addlast() and...
please modify the method public void addList(SinglyLinkedList<E> s) that add list to another with addlast() and remove() methods without changing the parameter of addList method. ////////////////////////////////////////////////// public class SinglyLinkedList<E> {      private class Node<E> {            private E element;            private Node<E> next;            public Node(E e, Node<E> n) {                 element = e;                 next = n;            }            public E getElement() {                 return element;            }            public void setElement(E element) {                 this.element = element;...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
Please list and briefly describe a current therapy method for cancer treatment (general method) and a...
Please list and briefly describe a current therapy method for cancer treatment (general method) and a current research area in cancer treatment relevant to nanotechnology (specific technique).
Science of Nutrition 5) List one example of a Mineral and another example of a vitamin...
Science of Nutrition 5) List one example of a Mineral and another example of a vitamin that are important for our immune system 6) What are the teratogens? Please list two examples of food items that should be avoided during pregnancy. 7) What is Bio availability ? Please list two examples of increased bio availability and another two examples of decreased bio availability. ***please be specific/detailed
Another technique used to estimate the cost of inventory is the Retail Inventory Method. Please define...
Another technique used to estimate the cost of inventory is the Retail Inventory Method. Please define and describe this method. Is this method more accurate than the Gross Profit Method? Can this method be used in preparing financial statements in accordance with GAAP guidelines?
*In C++ Please! This problem uses the concept of linked list What is a C++ structure?...
*In C++ Please! This problem uses the concept of linked list What is a C++ structure? Store product-id, product-name, and price per unit in a C++ structure. Use a C++ class with a member variable of that structure and provide read and write member functions for the product details. Suppose the product names are stored in alphabetical order, Write the C++ insert function to insert a new product ‘tooth brush’ in that linked list. Suppose the product names are stored...
Your textbook (Microeconomics 9th Edition) gives 3 “motives” for holding money. Please list one of these...
Your textbook (Microeconomics 9th Edition) gives 3 “motives” for holding money. Please list one of these motives and give an example, not the definition, of why you might hold money using the rationale of motive that you have selected.
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT