Answer to Question #268754 in C++ for Roe

Question #268754

1. Create the list. The list is initialized to an empty state.


2. Determine whether the list is empty.


3. Determine whether the list is full.


4. Find the size of the list.


5. Destroy, or clear, the list.


6. Determine whether an item is the same as a given list element.


7. Insert an item in the list at the specified location.


8. Remove an item from the list at the specified location.


9. Replace an item at the specified location with another item.


10. Retrieve an item from the list at the specified location.


11. Search the list for a given item

1
Expert's answer
2021-11-19T07:51:07-0500
#include <bits/stdc++.h>
using namespace std;
int main() 
{
  typedef list< std::string > Cont;
//1. Creating an empty list
   std::list<int>::iterator it;
   int key = 3, i;
   list<int> myList; 
// 2. Determine whether the list is empty 
   if (myList.empty())
      cout << "my list is empty\n";
//3. Determine if the list is full
   else
      cout << "my list isn’t empty\n";
   //push_back() is used to insert element in a list
   myList.push_back(1);
   myList.push_back(2);
   myList.push_back(3);
   myList.push_back(4);
   
// 4. Find the size of the list
int size = myList.size();
// 5. Destroy, or, clear the list 
   myList.clear();
//6. Determine whether an item is the same as a given list element.
   if (s.find(key) != s.end()) {
        std::cout << "Element is present in the set" << std::endl;
    }
    else {
        std::cout << "Element not found" << std::endl;
    }
//7. Insert an item in the list at the specified location.
      mylist.insert (it,4);
//8. Remove an item from the list at the specified location.
   myList.remove(20);
   
//9. Replace an item at the specified location with another item.
    for( Cont::iterator i = myList.begin(); i != myList.end(); ++i ) {
    // dereference the iterator to get a reference to the element
    std::string & s( *i );
    // test it
    if( s == "something" ) 
        s = "something else";         // replace
    if( s == "another something" ) 
        s = "another something else"; // replace
} 
//10.  Retrieve an item from the list at the specified location.
    cout<<* it;
//11.  Search the list for a given item
    // Fetch the iterator of element with value 'the'
it = std::find(listOfStrs.begin(), listOfStrs.end(), "the");
// Check if iterator points to end or not
if(it != listOfStrs.end())
    std::cout<<"'the' exists in list "<<std::endl;
      cout << *it;
      
// Finally, checking if the list is empty or not
   if (myList.empty())
      cout << "my list is empty\n";
   else
      cout << "my list is not empty\n";
   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS