Re: Airplane Program with Linked Lists. The linked list portion is very confusing to me.
In article <fr019s$hd4$1@news.Stanford.EDU>, Seungbeom Kim
<musiphil@bawi.org> wrote:
Michael.Boehnisch@gmail.com wrote:
I had a look at both your programs and I would like to encourage you
to dump them and start anew. I assume this is a C++ course, and I
would give you low marks for your solution even if everything works
because you essentially designed them ancient C style not object
oriented C++ style.
Just because the program is not in a object-oriented style doesn't mean
it is in a bad C++ style.
Consider making "Seat" a class, making data members private and add
public accessor methods (separate implementation details from
interface!). Everything in your program that acts on a single seat
should become a member function.
I agree that it should probably be a class at some point in the future,
but what invariants should be enforce by the class at this time?
struct SEAT {
bool occupied; // initially, false
string first,last; // passenger name
int numBags; // max of 4
char mealType; // (r)egular, (v)egetaria, (o)ther
};
I don't see any. (Except that the restrictions upon numBags and mealType
could be enforced by separate classes.) Therefore I don't see an
immediate need to make this into a real "class" (i.e. with private
parts), though adding a constructor would certainly help.
// I'd do it using an 2D array, as before, or a
// std::vector< std::vector<Seat> >.
A std::vector of std::vector is hardly a 2D array; it's rather a
hierarchy than a table.
The questions I have are
1) is this supposed to be a programming exercise or
2) an exercise in writing classical containers, or ,,
3) and how much is the student supposed to know?
Last one is extremely important since no one studies calculus before
elementary algebra.:)
And I agree that a linked list is hardly a good data structure for this
problem.
Agreed as I see random access or at least O(log N) access of a plane
or seat.
I might have a class/struct for planes since then I could add
members and other things to integrate the solution with the C++
standard library, Even
struct Plane
{
static const int n_seats = 256;
SEAT seats[n_seats];
SEAT & operator [] (std::ptrdiff_t n)
{ return seats[n];}
const SEAT & operator [] (std::ptrdiff_t) const
{ return seats[n];}
SEAT * begin() {return &seats[0];}
const SEAT * begin() const {return &seats[0];}
// similiar for end();
};
then we can access the k-th seat on j-th plane with the usual
planes[k][j], from a 1D array of Plane, and provide easy iterator
access to use with <algorithm>
Is this too much, maybe, but it makes the coding using standard
library especially since <algorithm> provides search algorithms and
these are to be used extensively in this type of program.
we can have a variable # of planes with fixed seats, and other combos
using std::vector instead of C style arrays if desired now or in the
future, requiring at most a recompile of much of the code.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]