More C++

Discussion in 'OT Technology' started by mrburner, Sep 28, 2003.

  1. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    ok...i have a class that has a list within it and in the application window, you can type "+ 3 4" to add the point (3,4) to the end of the list and you can type "# 5 6" to add (5,6) at the beginning of the list (or any other numbers you want to fill in)

    now i am using a cursor in order to add the points at the end and beginning of the list...just telling the cursor to move to the beginning and for all the points to move forwards to insert this new point...now how would i have to change the class if i didn't use a cursor...as in how would i be able to add to this list without using this method of going about it
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Is this an STL list, or some sort of linked list of your own design?

    An STL list has push_front() and push_back() member functions which would do what you want without using cursors (iterators?).
     
  3. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    its not an stl list...do you want me to post the code up for you?
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Well, it would help if you could explain how your list is implemented. The fact that you have to move all your items over to make room for an item at the head of the list sounds like it's an array. You'd be much better off using an STL list.
     
  5. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    here is the header file

    //-------------------------------------------------------------------- //

    // Laboratory 2 ptlist.h

    //

    // Class declaration for the array implementation of the Point // List ADT //

    //--------------------------------------------------------------------

    using namespace std;

    const int maxListSize = 10; // Default maximum list size

    //const int maxListSize = 5000; // Maximum list size for inlab 1

    //--------------------------------------------------------------------

    class Point

    {

    public:

    Point ( float x0 = 0, float y0 = 0 ) // Constructor

    { x = x0; y = y0; }

    float x, y; // Point coordinates (can be accessed directly)

    };

    //--------------------------------------------------------------------

    class PointList

    {

    public:

    // Constructor

    PointList ();

    // List manipulation operations

    void append ( Point newPoint ); // Append point to list

    void clear (); // Clear list

    // List status operations

    bool isEmpty () const; // List is empty

    bool isFull () const; // List is full

    // List iteration operations

    void gotoBeginning (); // Go to beginning

    void gotoEnd (); // Go to end

    bool gotoNext (); // Go to next point

    bool gotoPrior (); // Go to prior point

    Point getCursor () const; // Return point

    // Output the list structure -- used in testing/debugging

    void showStructure () const;

    // In-lab operations

    bool isTranslation ( const PointList &otherList ); // Translation

    void insertAtBeginning ( Point newPoint ); // Insert begin.

    private:

    // Data members

    int size, // Actual number of points in the list

    cursor; // Cursor array index

    Point points[maxListSize]; // Array containing the points

    };







    and here is the cpp file

    //--------------------------------------------------------------------

    //

    // Laboratory 2 ptlist.cpp

    //

    // SOLUTION: Array implementation of the Point List ADT //

    //--------------------------------------------------------------------

    #include <iostream>

    #include <cmath>

    #include "ptlist.h"

    using namespace std;

    //--------------------------------------------------------------------

    PointList:: PointList ()

    // Creates an empty list. Allocates enough memory for maxNumber // points (defaults to defMaxListSize).

    : size(0),

    cursor(-1)

    {

    }

    //--------------------------------------------------------------------

    void PointList:: append ( Point newPoint )

    // Appends newPoint to a list. If the list is empty, then newPoint is // inserted as the first (and only) point in the list. Moves the // cursor to newPoint.

    {

    cursor = size++;

    points[cursor] = newPoint;

    }

    //--------------------------------------------------------------------

    void PointList:: clear ()

    // Removes all the points from a list.

    {

    size = 0;

    }

    //--------------------------------------------------------------------

    bool PointList:: isEmpty () const

    // Returns true if a list is empty. Otherwise, returns false.

    {

    return ( size == 0 );

    }

    //--------------------------------------------------------------------

    bool PointList:: isFull () const

    // Returns true if a list is full. Otherwise, returns false.

    {

    return ( size == maxListSize );

    }

    //--------------------------------------------------------------------

    Point PointList:: getCursor () const

    // Returns the point marked by the cursor.

    {

    return points[cursor];

    }

    //--------------------------------------------------------------------

    void PointList:: gotoBeginning ()

    // Moves the cursor to the beginning of the list.

    {

    cursor = 0;

    }

    //--------------------------------------------------------------------

    void PointList:: gotoEnd ()

    // Moves the cursor to the end of the list.

    {

    cursor = size - 1;

    }

    //--------------------------------------------------------------------

    bool PointList:: gotoNext ()

    // If the cursor is not at the end of a list, then moves the cursor // to the next point in the list and returns true. Otherwise, returns false.

    {

    bool result; // Result returned

    if ( cursor != size-1 )

    {

    cursor++;

    result = true;

    }

    else

    result = false;

    return result;

    }

    //--------------------------------------------------------------------

    bool PointList:: gotoPrior ()

    // If the cursor is not at the beginning of a list, then moves the // cursor to the preceeding point in the list and returns true.

    // Otherwise, returns false.

    {

    bool result; // Result returned

    if ( cursor != 0 )

    {

    cursor--;

    result = true;

    }

    else

    result = false;

    return result;

    }

    //--------------------------------------------------------------------

    void PointList:: showStructure () const

    // Outputs the points in a list. If the list is empty, outputs // "Empty list". This operation is intended for testing/debugging // purposes only.

    {

    int j; // Loop counter

    if ( size == 0 )

    cout << "Empty list" << endl;

    else

    {

    cout << "size = " << size

    << " cursor = " << cursor << endl;

    // maxListSize is very large. Only output first 10 positions.

    if ( size > 10 )

    cout << "Only printing first 10 points." << endl;

    for ( j = 0 ; j < 10 ; j++ )

    cout << j << "\t";

    cout << endl;

    for ( j = 0 ; j < (size>10 ? 10 : size) ; j++ )

    cout << "(" << points[j].x << ","

    << points[j].y << ")\t";

    cout << endl;

    }

    }

    //--------------------------------------------------------------------

    //

    // In-lab operations

    //

    //--------------------------------------------------------------------

    bool PointList:: isTranslation ( const PointList &otherList )

    // Tests whether otherList is a translation of current list

    {

    return true;

    }

    //--------------------------------------------------------------------

    void PointList:: insertAtBeginning ( Point newPoint )

    // Inserts newPoint at the beginning of a list. If the list is // empty, then newPoint is inserted as the first (and only) point // in the list. Moves the cursor to newPoint.

    {

    for(cursor = size - 1; cursor >= 0; cursor--)

    {

    points[cursor + 1] = points[cursor];

    }

    size++;

    cursor = 0;

    points[cursor] = newPoint;



    }



    how would i change the class to do the same thing without using a cursor? thanks

     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Well, change to do what? Do you want to preserve the gotoBeg, End, Next, Prior functionality? If I were you, I'd just derive PointList from std::list<Point> and then just implement showstructure() and istranslation(). Use iterators in place of the goto...() members.
     

Share This Page