Sequnce container in c++

Discussion in 'Programmer's Corner' started by coolroose, Mar 9, 2012.

  1. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    Can anyone help me with this? pleaseeeeeeeeeeeee
    I really need help with this program: data structure using c++:


    All sequence containers have some common member functions. The list container provides some additional functions such as: push_front, pop_front, remove, remove_if, reverse, sort among others.

    Write an App that uses the STL list container to store the strings: "Hammer", "Screw driver", "Copper Pipe", and "Ladder". Then add to the front of the list the string "Bulbs" and to the end of the list the strings "Wrench" and "Ladder". Display the list. Sort the list by calling the sort function. To use the sort function you have to include the <algorithm> header file. Call the unique() function on the list to delete consecutive elements. Display the list.
     
  2. vpoko

    Member

    Jan 5, 2012
    258
    47
    How far have you gotten? Which part are you stuck on?
     
  3. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    The part that say: call the unique function() on the list to delete consecutive elements
     
  4. vpoko

    Member

    Jan 5, 2012
    258
    47
    Deleting non-unique elements shouldn't be hard (especially after sorting, which will ensure that non-unique elements are always adjacent).

    A conceptual solution could go like this: Traverse your list. As you go through each element, save that element's value (or even better, a reference to it) into a temporary variable. Then, after going onto the next element, compare that element's value with the one stored in your temporary variable. If they're the same, delete the current element. (Assuming you have a linked list implementation, don't forget to update the previous element's "next" pointer to point to the current element's "next" - this will be easier if you either save a reference to the previous element in your temporary variable, or if your linked list is doubly linked, or if you're using an implementation that allows random access).

    If you're still having trouble implementing this, post your attempt.
     
    Last edited: Mar 9, 2012
  5. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    HERE IS HOW I STARTED, SO PLEASE HELP ME FINISH IT RIGHT. thank you

    /*
    * Write an App that uses the STL list container to store the strings:
    * "Hammer", "Screw driver", "Copper Pipe", and "Ladder". Then add to
    * the front of the list the string "Bulbs" and to the end of the list
    * the strings "Wrench" and "Ladder". Display the list. Sort the list
    * by calling the sort function. To use the sort function you have to
    * include the <algorithm> header file. Call the unique() function on
    * the list to delete consecutive elements. Display the list.
    */

    /*
    * Includes
    */
    #include <iostream>
    #include <list>
    #include <string>
    #include <algorithm>


    void UsingList()
    {
    using std::list;
    using std::string;
    using std::cout;
    using std::endl;

    list<string> myStrList;// list container
    myStrList.push_front(string("Hammer"));
    myStrList.push_front("Screw driver");
    myStrList.push_front("Copper Pipe");
    myStrList.push_front("Ladder");

    myStrList.push_front("Bulbs");

    myStrList.push_back("Wrench");
    myStrList.push_back("Ladder");

    cout << "Original List:" << endl;// dispaly the containers content
    unsigned int index = 0;
    for(auto it = myStrList.begin(); it != myStrList.end(); ++it) {
    cout << "myStrList[" << index++ << "] = " << *it << endl;
    }
     
  6. vpoko

    Member

    Jan 5, 2012
    258
    47
    I don't see where you've attempted to implement the unique() function at all.

    Your next steps would be to 1) sort your list 2) traverse it, searching for duplicates, as I explained above.

    If you write that code and it's not quite working how you expect, post that code and I'll help you with it.
     
Loading...