[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.1

Linked Lists

Asked by Dfig in C++ Builder, Microsoft Visual C++

Tags: C++

I am trying to understand user defined Linked Lists in C++.  I am trying to work through a lab exercise with class definitions and implementations posted by the instructor. Things were a little easier to follow when they were using the STD version. I need to store 10 strings in alphabetical order and then print them in descending order. I created the the list and stored the strings. II am  trying to figure out how to print them in descending order but can't get out the starting gate. I was checking  out the web for tutorials and how to but too much information out there. Could some kind soul lend some assistance? Here's what I got.

#include <cassert>
#include <iostream>
#include <string>
using namespace std;



class Node {
      public:
            Node(string s);

      private:
            string data;
            Node* previous;
            Node* next;
            friend class List;
            friend class Iterator;
};

class Iterator {
      public:
            Iterator();
            string get() const;
            void next();  
            void previous();  
            bool equals(Iterator b) const;
      private:
            Node* position;  
            Node* last;  
            friend class List;
};


class List {
      public:
            List();
            void push_back(string s);  
            void insert(Iterator pos, string s);
            Iterator erase(Iterator pos);
            Iterator begin();  
            Iterator end();  
      private:
            Node* first;
            Node* last;
};


Node::Node(string s) {
      data = s;
      previous = NULL;
      next= NULL;
};

Iterator::Iterator():position(NULL),last(NULL) { }
      string Iterator::get() const {
      assert(position != NULL);
      return position->data;
}

void Iterator::next() {
      assert(position != NULL);  
      position = position->next;
}
void Iterator::previous() {
      if (position == NULL)
            position = last;
      else
            position = position->previous;
            assert(position != NULL);  
}

bool Iterator::equals(Iterator b) const {
      return position == b.position;
}


List::List() {
      first = NULL;
      last = NULL;
}

void List::push_back(string s) {
      Node* newnode = new Node(s);
      if (last == NULL){
      first = newnode;
      last = newnode;
}
else {
      newnode->previous = last;
      last->next = newnode;
      last = newnode;
      }
}

void List::insert(Iterator iter, string s) {
      if (iter.position == NULL) {  
      push_back(s);
      return;
}

Node* after = iter.position;
Node* before = after->previous;
Node* newnode = new Node(s);
newnode->previous = before;
newnode->next = after;
after->previous = newnode;
if (before == NULL)  
      first = newnode;
else
      before->next = newnode;
}

Iterator List::erase(Iterator i) {
Iterator iter = i;
assert(iter.position != NULL);  
Node* remove = iter.position;
Node* before = remove->previous;
Node* after = remove->next;
if (remove == first)
      first = after;
else
      before->next = after;
if (remove == last)
      last = before;
else
      after->previous = before;
      iter.position = after;
delete remove;
return iter;
}

Iterator List::begin() {
      Iterator iter;
      iter.position = first;
      iter.last = last;
      return iter;
}

Iterator List::end() {
      Iterator iter;
      iter.position = NULL;
      iter.last = last;
      return iter;
}
int main()
{
    List soldier;
    soldier.push_back("Allen");
    soldier.push_back("Christ");
    soldier.push_back("Dog");
    soldier.push_back("Fiona");
    soldier.push_back("Jerry");
    soldier.push_back("John");
    soldier.push_back("Lion");
    soldier.push_back("Tiger");
    soldier.push_back("Tom");
    soldier.push_back("Wolf");

    cout << soldier.begin();

    system("pause");
    return 0;
}
   
[+][-]11/04/09 11:23 PM, ID: 25747360Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: C++ Builder, Microsoft Visual C++
Tags: C++
Sign Up Now!
Solution Provided By: CSD-GW
Participating Experts: 1
Solution Grade: A
 
[+][-]11/05/09 07:31 PM, ID: 25756400Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/06/09 11:14 AM, ID: 25762143Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/06/09 03:30 PM, ID: 25764003Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/08/09 11:25 PM, ID: 25773930Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625