Airplane Program with Linked Lists. The linked list portion is veryconfusing to me.

Discussion in 'C++' started by jawdoc, Mar 7, 2008.

  1. jawdoc

    jawdoc Guest

    Earlier I posted up an Airplane Program using 2 dimensional arrays,
    and I totally appretiate all of the help I received from everyone. Now
    I have this program and we are instructed to use Linked Lists. I
    understand that in my struct I need a struct that is a pointer that is
    in the struct. In mine it is called *next. In my updated code passing
    through some of the functions with the plane struct causes errors on
    the clearPlane function. The Linked list version has an array of bools
    showing if the seat is taken or not instead of having the bool in the
    array. I just need some direction for using the linked lists instead
    of the 2 dimensional arrays. Both versions are provided. Any help is
    greatly appretiated.

    Thanks
    Jawdoc

    2 Dimensional Airplane

    http://www.chiefgamer.com/tman/cpp/airplanearray.cpp

    Linked List Airplane

    http://www.chiefgamer.com/tman/cpp/airplanelist.cpp
     
    jawdoc, Mar 7, 2008
    #1
    1. Advertising

  2. jawdoc

    Christopher Guest

    Re: Airplane Program with Linked Lists. The linked list portion isvery confusing to me.

    On Mar 7, 2:33 pm, jawdoc <> wrote:
    > Earlier I posted up an Airplane Program using 2 dimensional arrays,
    > and I totally appretiate all of the help I received from everyone. Now
    > I have this program and we are instructed to use Linked Lists. I
    > understand that in my struct I need a struct that is a pointer that is
    > in the struct. In mine it is called *next. In my updated code passing
    > through some of the functions with the plane struct causes errors on
    > the clearPlane function. The Linked list version has an array of bools
    > showing if the seat is taken or not instead of having the bool in the
    > array. I just need some direction for using the linked lists instead
    > of the 2 dimensional arrays. Both versions are provided. Any help is
    > greatly appretiated.
    >
    > Thanks
    > Jawdoc
    >
    > 2 Dimensional Airplane
    >
    > http://www.chiefgamer.com/tman/cpp/airplanearray.cpp
    >
    > Linked List Airplane
    >
    > http://www.chiefgamer.com/tman/cpp/airplanelist.cpp


    Readers of the NG may not wish to follow links for security reasons.
    Post a minimal working demonstration of the problem in code directly.

    Also, have you followed what the code does with paper and pen?
    When learning linked lists in school, it is very helpful to draw boxes
    representing what the pointers point to and illustrate step by step
    what happens.
     
    Christopher, Mar 7, 2008
    #2
    1. Advertising

  3. jawdoc

    jawdoc Guest

    Re: Airplane Program with Linked Lists. The linked list portion isvery confusing to me.

    Yes I know what the code is supposed to do, my diagrams have been
    drawn out and everything its just a bit confusing to me how I can
    match the seat number and row of the bool double array to the linked
    list information a long with my errors. The full code is really needed
    to show what the program actually does and is supposed to do. In the
    "clearPlane" calling in main() it is saying that my calling of the
    struct is changing its reference from SEAT *& to SEAT .

    This is the code:

    #include<iostream>
    #include<string>
    #include<cctype>

    using namespace std;

    struct SEAT {
    // bool occupied; // initially, false
    string first,last; // passenger name
    int numBags; // max of 4
    char mealType; // (r)egular, (v)egetaria, (o)ther
    SEAT *next;
    };

    const int ROWS= 30;
    const int COLS= 6;

    int menuchoice();
    void clearPlane(SEAT *&p , bool o[][COLS]);
    void showPlane(bool o[][COLS]);
    bool getSeat(SEAT *&p, int &row, int &col, bool o[][COLS]);
    void getSeatFromUser(int &row, int &col, bool o[][COLS]);
    void getInfo(SEAT *&s, int, int);
    void showInfo(SEAT *&s, int, int);
    void makeReservation(SEAT p, bool o[][COLS]);
    void cancelReservation(SEAT p, bool o[][COLS]);
    void viewReservations(SEAT p, bool o[][COLS]);
    void orderedInsert(SEAT *&p, int &row, int &col);

    int main()
    {
    int choice; // user's menu choice
    SEAT plane; // reservation info for entire plane
    bool occupied[ROWS][COLS];
    clearPlane( plane, occupied);
    showPlane( occupied);
    do
    {
    cin.clear();
    choice= menuchoice();
    cin.ignore();
    switch (choice) {
    case 1: makeReservation(plane, occupied); break;
    case 2: cancelReservation(plane, occupied); break;
    case 3: viewReservations(plane, occupied); break;
    case 4: break;
    }
    }while(choice !=4);



    return 0;
    }



    /* menuchoice
    **-----------------
    ** displays menu and get's user's choice
    **--------------------------------------------------------------------
    */
    int menuchoice()
    {
    int choice;

    cout << endl << endl;
    cout << "+------------------------+\n";
    cout << "| Main Menu |\n";
    cout << "+------------------------+\n";
    cout << "| [1] Reserve a Seat |\n";
    cout << "| [2] Cancel Reservation |\n";
    cout << "| [3] List Reservations |\n";
    cout << "| [4] Exit Program |\n";
    cout << "+------------------------+\n";
    do {
    cout << "Choice: ";
    cin >> choice;
    if(!cin)
    cin.clear();

    } while (choice < 1 || choice > 4);

    return choice;
    }


    /* showPlane
    **-----------------
    ** displays grid showing open/taken seats
    **--------------------------------------------------------------------
    */
    void showPlane(bool o[][COLS] )
    {
    int i,j;
    cout << " 1 2
    3\n";
    cout << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
    0\n";
    for (j=0; j<COLS; j++) {
    cout << (char) (j+65);
    for (i=0; i<ROWS; i++)
    if (o[j]==true)
    cout << " x";
    else
    cout << " ";
    cout << endl;
    }
    }


    /* clearPlane
    **-----------------
    ** empties all reservations in airplane
    **--------------------------------------------------------------------
    */
    void clearPlane(SEAT *&p, bool o[][COLS])
    {
    int i,j;

    for (i=0; i<ROWS; i++)
    for (j=0; j<COLS; j++)
    o[j]= false;
    }


    /* getSeat
    **-----------------
    ** obtains desired row and column number for a seat; the seat can be
    ** selected by the user or can be selected automatically by the
    ** computer; the function return true if an open seat was found;
    ** false otherwise.
    **--------------------------------------------------------------------
    */
    bool getSeat(SEAT *&p, int &row, int &col, bool o[][COLS])
    {
    bool allTaken; // true if all seats on plane are occupied
    char method; // (a)utomatic or (m)anual seat selection

    allTaken= true; // make sure at least
    one
    for (row=0; row<ROWS && allTaken; row++) // seat is available
    for (col=0; col<COLS && allTaken; col++)
    if (!o[row][col])
    allTaken= false;
    if (allTaken) return false; // if not return false

    do {
    cout << "Would you like to select a seat (a)utomatically or
    (m)anually: ";
    cin >> method;
    } while (tolower(method)!='a' && tolower(method)!='m');

    switch (tolower(method)) {
    case 'a': for (row=0; row<ROWS; row++) // (a)utomatic seat
    selection
    for (col=0; col<COLS; col++)
    if (!o[row][col])
    return true;
    break; // this break is redundant, we hope
    case 'm': getSeatFromUser(row,col,o); // (m)anual seat
    selection
    return !o[row][col];
    }

    return false; // we should never get here
    }


    /* getSeatFromUser
    **-----------------
    ** obtains desired row and column number for a seat; this function
    ** will be called from getSeat when requesting a seat for making
    ** reservations and will be called directly when selecting a
    ** seat for cancellation.
    **--------------------------------------------------------------------
    */

    void getSeatFromUser(int &row, int &col, bool o[][COLS])
    {
    char let;

    showPlane (o);
    do {
    cout << "Enter row (1.." << ROWS << "): ";
    cin >> row;
    } while (row < 1 || row > ROWS);

    do {
    cout << "Enter col (A.." << (char) (COLS+64) << "): ";
    cin >> let;
    let= toupper(let);
    } while (let < 'A' || let > (COLS+64));

    col= let-65;
    row--;

    }

    /* getInfo
    * --------------------------------------------------------
    * Accepts the SEAT struct then the user enters in their
    * information.
    * ------------------------------------------------------*/
    void getInfo(SEAT *&s, int row, int col)
    {
    cin.ignore(100,'\n');
    string answer;
    do{

    cout<<"\nEnter in this information:\n\n";

    cout<<"Enter your first and last name: ";

    cin>>s->first;

    cin>>s->last;

    cout<<"\n";
    do{
    cin.clear();
    cout<<"How many bags will you be bringing?(limit of 4) ";
    cin>>s->numBags;
    cout<<"\n";
    }while(s->numBags<0||s->numBags>4);

    do{
    cin.clear();
    cout<<"What type of meal would you like?((r)egular, (v)egatarian,
    or (o)ther) ";
    cin>>s->mealType;
    s->mealType=tolower(s->mealType);
    cout<<"\n";
    }while(s->mealType!='r' && s->mealType!='v' && s->mealType!= 'o');


    showInfo(s, row, col);
    getline(cin,answer);
    do{
    cout<<"Is this information correct?(yes,no)";
    getline(cin,answer);
    cout<<"\n";
    }while(answer!="yes" && answer!="no");

    cin.clear();


    }while(answer=="no");
    }

    /*--------------------------------------------------------
    * Displays the information they just entered.
    * ------------------------------------------------------*/

    void showInfo(SEAT *&s, int row, int col)
    {
    string regular="Regular";
    string vegetarian="Vegetarian";
    string other="Other";

    cout<<"\nSeat: "<<row+1<<", "<<(char)(col+65)<<"\n";
    cout<<"Name: "<<s->first<<" "<<s->last<<"\n";
    cout<<"Number of bags: " <<s->numBags<<"\n";

    if(s->mealType=='r')
    cout<<"Meal Type: "<<regular<<"\n\n";
    if(s->mealType=='v')
    cout<<"Meal Type: "<<vegetarian<<"\n\n";
    if(s->mealType=='o')
    cout<<"Meal Type: "<<other<<"\n\n";
    }

    /*--------------------------------------------------------
    * Calls showPlane and then will ask what seat they want
    * and will call getInfo
    * ------------------------------------------------------*/

    void makeReservation(SEAT *&p, bool o[][COLS])
    {
    int row;
    int col;
    do{
    cout<<"\n";
    getSeat(p,row, col,o);
    }while(o[row][col]==true);

    o[row][col]=true;

    orderedInsert(p,row,col);
    //getInfo(p,row,col,o);
    }

    /*--------------------------------------------------------
    * User enters in a seat row and column and if it is empty
    * it will display that. If it is occupied then it will
    * make the array empty.
    * ------------------------------------------------------*/

    void cancelReservation(SEAT *&p, bool o[][COLS])
    {
    int row;
    int col;
    getSeatFromUser(row, col, o);
    if(o[row][col]==false)
    cout<<"\nThe reservation is empty.";
    else
    o[row][col]=false;

    }

    /*--------------------------------------------------------
    * For loop that will go through each seat and if it is
    * occupied it will display the SEAT struct for that one
    * then move on.
    * ------------------------------------------------------*/

    void viewReservations(SEAT *&p, bool o[][COLS])
    {
    int i;
    int n;
    for(i=0; i<30; i++)
    for(n=0; n<6; n++)
    if(o[n]==true)
    {
    showInfo(p, i, n);
    }


    }

    void orderedInsert(SEAT *&p, int &row, int &col)
    {
    SEAT *nextSEAT= new SEAT;
    getInfo(nextSEAT, row, col);


    }
     
    jawdoc, Mar 7, 2008
    #3
  4. jawdoc

    Christopher Guest

    Re: Airplane Program with Linked Lists. The linked list portion isvery confusing to me.

    On Mar 7, 4:44 pm, jawdoc <> wrote:
    [snip]
    > In the "clearPlane" calling in main() it is saying that my calling of the
    > struct is changing its reference from SEAT *& to SEAT .
    >
    > This is the code:

    [snip]
    > struct SEAT {
    > // bool occupied; // initially, false
    > string first,last; // passenger name
    > int numBags; // max of 4
    > char mealType; // (r)egular, (v)egetaria, (o)ther
    > SEAT *next;
    >

    [snip]

    > void clearPlane(SEAT *&p , bool o[][COLS]);

    [snip]

    > int main()
    > {

    [snip]
    > SEAT plane; // reservation info for entire plane

    [snip]
    > clearPlane( plane, occupied);

    [big snip]

    Ok, that part of the problem is easy. You declared the function
    clearPlane as taking a parameter that is a reference to a pointer to a
    SEAT. However, when you call it, you are passing a SEAT. The easy fix
    and probably not the best, is pass the address of the seat instead:

    clearPlane( &plane, occupied);

    However, if you are using a linked list, you would most likely rather
    have a SeatList class that has some CreateSeat Method, which take a
    const reference to a Seat, then allocates memory for a copy of it,
    creates the node, and inserts it properly.

    As far as I can tell you simply made a node structure and not the list
    itself, which leaves you to the problems of handling memory allocation
    and release. That defeats the purpose of the linked list in the first
    place.

    Secondly, I do not see why you chose to pass references to pointers in
    all these functions. Why would passing a pointer not suffice?

    Thirdly, I would not suggest making pointers to nodes publically
    accessable to a user of your code. You should hide the implementation
    details in a List class, which is the only place the pointers would be
    used. The List class would simply make copies of the data into nodes
    and return copies of data from the nodes. While handling all the nodes
    privately.

    Hope that helps.
     
    Christopher, Mar 7, 2008
    #4
  5. jawdoc

    Christopher Guest

    Re: Airplane Program with Linked Lists. The linked list portion isvery confusing to me.

    On Mar 7, 4:44 pm, jawdoc <> wrote:
    > Yes I know what the code is supposed to do, my diagrams have been
    > drawn out and everything its just a bit confusing to me how I can
    > match the seat number and row of the bool double array to the linked
    > list information a long with my errors. The full code is really needed
    > to show what the program actually does and is supposed to do. In the
    > "clearPlane" calling in main() it is saying that my calling of the
    > struct is changing its reference from SEAT *& to SEAT .
    >
    > This is the code:
    >
    > #include<iostream>
    > #include<string>
    > #include<cctype>
    >
    > using namespace std;
    >
    > struct SEAT {
    > // bool occupied; // initially, false
    > string first,last; // passenger name
    > int numBags; // max of 4
    > char mealType; // (r)egular, (v)egetaria, (o)ther
    > SEAT *next;
    >
    > };
    >
    > const int ROWS= 30;
    > const int COLS= 6;
    >
    > int menuchoice();
    > void clearPlane(SEAT *&p , bool o[][COLS]);
    > void showPlane(bool o[][COLS]);
    > bool getSeat(SEAT *&p, int &row, int &col, bool o[][COLS]);
    > void getSeatFromUser(int &row, int &col, bool o[][COLS]);
    > void getInfo(SEAT *&s, int, int);
    > void showInfo(SEAT *&s, int, int);
    > void makeReservation(SEAT p, bool o[][COLS]);
    > void cancelReservation(SEAT p, bool o[][COLS]);
    > void viewReservations(SEAT p, bool o[][COLS]);
    > void orderedInsert(SEAT *&p, int &row, int &col);
    >
    > int main()
    > {
    > int choice; // user's menu choice
    > SEAT plane; // reservation info for entire plane
    > bool occupied[ROWS][COLS];
    > clearPlane( plane, occupied);
    > showPlane( occupied);
    > do
    > {
    > cin.clear();
    > choice= menuchoice();
    > cin.ignore();
    > switch (choice) {
    > case 1: makeReservation(plane, occupied); break;
    > case 2: cancelReservation(plane, occupied); break;
    > case 3: viewReservations(plane, occupied); break;
    > case 4: break;
    > }
    > }while(choice !=4);
    >
    > return 0;
    >
    > }
    >
    > /* menuchoice
    > **-----------------
    > ** displays menu and get's user's choice
    > **--------------------------------------------------------------------
    > */
    > int menuchoice()
    > {
    > int choice;
    >
    > cout << endl << endl;
    > cout << "+------------------------+\n";
    > cout << "| Main Menu |\n";
    > cout << "+------------------------+\n";
    > cout << "| [1] Reserve a Seat |\n";
    > cout << "| [2] Cancel Reservation |\n";
    > cout << "| [3] List Reservations |\n";
    > cout << "| [4] Exit Program |\n";
    > cout << "+------------------------+\n";
    > do {
    > cout << "Choice: ";
    > cin >> choice;
    > if(!cin)
    > cin.clear();
    >
    > } while (choice < 1 || choice > 4);
    >
    > return choice;
    >
    > }
    >
    > /* showPlane
    > **-----------------
    > ** displays grid showing open/taken seats
    > **--------------------------------------------------------------------
    > */
    > void showPlane(bool o[][COLS] )
    > {
    > int i,j;
    > cout << " 1 2
    > 3\n";
    > cout << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
    > 0\n";
    > for (j=0; j<COLS; j++) {
    > cout << (char) (j+65);
    > for (i=0; i<ROWS; i++)
    > if (o[j]==true)
    > cout << " x";
    > else
    > cout << " ";
    > cout << endl;
    > }
    >
    > }
    >
    > /* clearPlane
    > **-----------------
    > ** empties all reservations in airplane
    > **--------------------------------------------------------------------
    > */
    > void clearPlane(SEAT *&p, bool o[][COLS])
    > {
    > int i,j;
    >
    > for (i=0; i<ROWS; i++)
    > for (j=0; j<COLS; j++)
    > o[j]= false;
    >
    > }
    >
    > /* getSeat
    > **-----------------
    > ** obtains desired row and column number for a seat; the seat can be
    > ** selected by the user or can be selected automatically by the
    > ** computer; the function return true if an open seat was found;
    > ** false otherwise.
    > **--------------------------------------------------------------------
    > */
    > bool getSeat(SEAT *&p, int &row, int &col, bool o[][COLS])
    > {
    > bool allTaken; // true if all seats on plane are occupied
    > char method; // (a)utomatic or (m)anual seat selection
    >
    > allTaken= true; // make sure at least
    > one
    > for (row=0; row<ROWS && allTaken; row++) // seat is available
    > for (col=0; col<COLS && allTaken; col++)
    > if (!o[row][col])
    > allTaken= false;
    > if (allTaken) return false; // if not return false
    >
    > do {
    > cout << "Would you like to select a seat (a)utomatically or
    > (m)anually: ";
    > cin >> method;
    > } while (tolower(method)!='a' && tolower(method)!='m');
    >
    > switch (tolower(method)) {
    > case 'a': for (row=0; row<ROWS; row++) // (a)utomatic seat
    > selection
    > for (col=0; col<COLS; col++)
    > if (!o[row][col])
    > return true;
    > break; // this break is redundant, we hope
    > case 'm': getSeatFromUser(row,col,o); // (m)anual seat
    > selection
    > return !o[row][col];
    > }
    >
    > return false; // we should never get here
    >
    > }
    >
    > /* getSeatFromUser
    > **-----------------
    > ** obtains desired row and column number for a seat; this function
    > ** will be called from getSeat when requesting a seat for making
    > ** reservations and will be called directly when selecting a
    > ** seat for cancellation.
    > **--------------------------------------------------------------------
    > */
    >
    > void getSeatFromUser(int &row, int &col, bool o[][COLS])
    > {
    > char let;
    >
    > showPlane (o);
    > do {
    > cout << "Enter row (1.." << ROWS << "): ";
    > cin >> row;
    > } while (row < 1 || row > ROWS);
    >
    > do {
    > cout << "Enter col (A.." << (char) (COLS+64) << "): ";
    > cin >> let;
    > let= toupper(let);
    > } while (let < 'A' || let > (COLS+64));
    >
    > col= let-65;
    > row--;
    >
    > }
    >
    > /* getInfo
    > * --------------------------------------------------------
    > * Accepts the SEAT struct then the user enters in their
    > * information.
    > * ------------------------------------------------------*/
    > void getInfo(SEAT *&s, int row, int col)
    > {
    > cin.ignore(100,'\n');
    > string answer;
    > do{
    >
    > cout<<"\nEnter in this information:\n\n";
    >
    > cout<<"Enter your first and last name: ";
    >
    > cin>>s->first;
    >
    > cin>>s->last;
    >
    > cout<<"\n";
    > do{
    > cin.clear();
    > cout<<"How many bags will you be bringing?(limit of 4) ";
    > cin>>s->numBags;
    > cout<<"\n";
    > }while(s->numBags<0||s->numBags>4);
    >
    > do{
    > cin.clear();
    > cout<<"What type of meal would you like?((r)egular, (v)egatarian,
    > or (o)ther) ";
    > cin>>s->mealType;
    > s->mealType=tolower(s->mealType);
    > cout<<"\n";
    > }while(s->mealType!='r' && s->mealType!='v' && s->mealType!= 'o');
    >
    > showInfo(s, row, col);
    > getline(cin,answer);
    > do{
    > cout<<"Is this information correct?(yes,no)";
    > getline(cin,answer);
    > cout<<"\n";
    > }while(answer!="yes" && answer!="no");
    >
    > cin.clear();
    >
    > }while(answer=="no");
    >
    > }
    >
    > /*--------------------------------------------------------
    > * Displays the information they just entered.
    > * ------------------------------------------------------*/
    >
    > void showInfo(SEAT *&s, int row, int col)
    > {
    > string regular="Regular";
    > string vegetarian="Vegetarian";
    > string other="Other";
    >
    > cout<<"\nSeat: "<<row+1<<", "<<(char)(col+65)<<"\n";
    > cout<<"Name: "<<s->first<<" "<<s->last<<"\n";
    > cout<<"Number of bags: " <<s->numBags<<"\n";
    >
    > if(s->mealType=='r')
    > cout<<"Meal Type: "<<regular<<"\n\n";
    > if(s->mealType=='v')
    > cout<<"Meal Type: "<<vegetarian<<"\n\n";
    > if(s->mealType=='o')
    > cout<<"Meal Type: "<<other<<"\n\n";
    >
    > }
    >
    > /*--------------------------------------------------------
    > * Calls showPlane and then will ask what seat they want
    > * and will call getInfo
    > * ------------------------------------------------------*/
    >
    > void makeReservation(SEAT *&p, bool o[][COLS])
    > {
    > int row;
    > int col;
    > do{
    > cout<<"\n";
    > getSeat(p,row, col,o);
    > }while(o[row][col]==true);
    >
    > o[row][col]=true;
    >
    > orderedInsert(p,row,col);
    > //getInfo(p,row,col,o);
    >
    > }
    >
    > /*--------------------------------------------------------
    > * User enters in a seat row and column and if it is empty
    > * it will display that. If it is occupied then it will
    > * make the array empty.
    > * ------------------------------------------------------*/
    >
    > void cancelReservation(SEAT *&p, bool o[][COLS])
    > {
    > int row;
    > int col;
    > getSeatFromUser(row, col, o);
    > if(o[row][col]==false)
    > cout<<"\nThe reservation is empty.";
    > else
    > o[row][col]=false;
    >
    > }
    >
    > /*--------------------------------------------------------
    > * For loop that will go through each seat and if it is
    > * occupied it will display the SEAT struct for that one
    > * then move on.
    > * ------------------------------------------------------*/
    >
    > void viewReservations(SEAT *&p, bool o[][COLS])
    > {
    > int i;
    > int n;
    > for(i=0; i<30; i++)
    > for(n=0; n<6; n++)
    > if(o[n]==true)
    > {
    > showInfo(p, i, n);
    > }
    >
    > }
    >
    > void orderedInsert(SEAT *&p, int &row, int &col)
    > {
    > SEAT *nextSEAT= new SEAT;
    > getInfo(nextSEAT, row, col);
    >
    > }


    P.S.
    Also, as far as the confusion you have about the rows and columns:
    Do not keep track of this in an array. Keep track of it in the List
    class that you make. It looks like your instructor intended for you to
    make a List of Lists, but I cannot know without seeing the
    requirements. At any rate, when you make the List class, it should
    have some kind of size method that returns the number of Nodes in the
    List. So, you would have a List that represents the rows, with each
    row being a List of Seats. Depending on the requirements, the List
    might be some static number of Nodes, one for each seat, and the seat
    could have some boolean saying if it is empty or not.
     
    Christopher, Mar 7, 2008
    #5
  6. Re: Airplane Program with Linked Lists. The linked list portion is very confusing to me.

    "Christopher" <> wrote in message
    news:...
    [snip
    and return copies of data from the nodes.
    [snip]

    edit: return references to the data in the nodes

    I never understood why professor teach students to make a linked list from
    scratch before showing them the STL Linked List first, so the student at
    least has some clues to how it should work. They seem to make the student
    create a linked list from scratch, leaving them to discover for themselves
    the STL, and most likely never having to do such a thing again.

    Anyway, maybe you would be well served to consult a C++ reference on
    std::list and look over its interface and then use your textbook and lecture
    notes to implement the methods. That's how I passed data structures.

    GL
     
    Christopher Pisz, Mar 7, 2008
    #6
  7. Re: Airplane Program with Linked Lists. The linked list portion is very confusing to me.

    "jawdoc" <> wrote in message
    news:...
    [...]

    > I just need some direction for using the linked lists instead
    > of the 2 dimensional arrays. Both versions are provided. Any help is
    > greatly appretiated.


    This should help get you started:
    ___________________________________________________________
    class airplane {
    struct seat {
    seat* next;
    // [...]
    };

    struct row {
    row* next;
    seat* seats;
    };

    row* m_rows;

    seat* find_seat(int row, int col) const {
    row* r = m_rows;
    while (r && row > 1) {
    row--;
    r = r->next;
    }
    if (r && row == 1) {
    seat* s = r->seats;
    while (s && col > 1) {
    col--;
    s = s->next;
    }
    if (s && col == 1) {
    return s;
    }
    }
    return NULL;
    }

    public:
    // [...]
    };

    ___________________________________________________________
     
    Chris Thomasson, Mar 8, 2008
    #7
  8. jawdoc

    jawdoc Guest

    Re: Airplane Program with Linked Lists. The linked list portion isvery confusing to me.

    Thanks that was great man. The linked list algorithm you provided in
    your last message, that helped me figure out how to set up my linked
    lists but in my instructions, to keep up with if the seats are
    occupied or not my instructor told me to use a 2dimensional array for
    the bool. The linked list is supposed to hold the information for the
    people and whatever they have is supposed to correspond with the row
    and column in the array. I am still a bit confused but your help
    definatly cleared my mind on most of what I was having trouble with on
    how to make this work.


    Now that I have the clearPlane() working, when I try and compile I get
    an error stating:

    C:\djgpp\bin>gxx prog10.cpp
    c:/djgpp/tmp/cc7nsxYP.o:prog10.cpp:(.text+0x1252): undefined reference
    to `makeReservation(SEAT, bool (*) [6])'
    c:/djgpp/tmp/cc7nsxYP.o:prog10.cpp:(.text+0x12b5): undefined reference
    to `cancelReservation(SEAT, bool (*) [6])'
    c:/djgpp/tmp/cc7nsxYP.o:prog10.cpp:(.text+0x1318): undefined reference
    to `viewReservations(SEAT, bool (*) [6])'
    collect2: ld returned 1 exit status

    I checked and all of my exits and I couldn't find anything that would
    do this. What excactly does this error mean and is there another easy
    fix?


    Your help is greatly appretiated.

    Thanks
    Jawdoc
     
    jawdoc, Mar 8, 2008
    #8
  9. jawdoc

    Daniel T. Guest

    Re: Airplane Program with Linked Lists. The linked list portion is very confusing to me.

    jawdoc <> wrote:

    > Earlier I posted up an Airplane Program using 2 dimensional arrays,
    > and I totally appretiate all of the help I received from everyone. Now
    > I have this program and we are instructed to use Linked Lists. I
    > understand that in my struct I need a struct that is a pointer that is
    > in the struct. In mine it is called *next. In my updated code passing
    > through some of the functions with the plane struct causes errors on
    > the clearPlane function. The Linked list version has an array of bools
    > showing if the seat is taken or not instead of having the bool in the
    > array. I just need some direction for using the linked lists instead
    > of the 2 dimensional arrays. Both versions are provided. Any help is
    > greatly appretiated.
    >
    > Thanks
    > Jawdoc
    >
    > 2 Dimensional Airplane
    >
    > http://www.chiefgamer.com/tman/cpp/airplanearray.cpp
    >
    > Linked List Airplane
    >
    > http://www.chiefgamer.com/tman/cpp/airplanelist.cpp


    In the first version, you have a SEAT struct and create 30 * 6 seats in
    the main() function calling them 'plane'. In the second version you have
    essentially the same SEAT struct but you only create *one* seat and call
    it 'plane'. I suspect this is the crux of your problem.

    I suggest you do this: first convert your 2D array program so that it
    uses a 1D array "seat[180]" instead of "seat[30][6]", then try to do the
    conversation from the 1D array to the list. That will be much easier.

    Remember, to make the list using the struct you have, you will have to
    create 180 SEAT objects and link them all together properly. You should
    probably make a class that handles the creation and linking (maybe
    called PLANE? :)
     
    Daniel T., Mar 8, 2008
    #9
  10. Re: Airplane Program with Linked Lists. The linked list portion is very confusing to me.

    "Chris Thomasson" <> wrote in message
    news:D...
    >
    > "jawdoc" <> wrote in message
    > news:...
    > [...]
    >
    >> I just need some direction for using the linked lists instead
    >> of the 2 dimensional arrays. Both versions are provided. Any help is
    >> greatly appretiated.

    >
    > This should help get you started:
    > ___________________________________________________________
    > class airplane {

    [...]
    >
    > struct row {
    > row* next;
    > seat* seats;
    > };


    [...]

    > seat* find_seat(int row, int col) const {

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    WHOOPS! Humm, I think I should rename the row variable in
    'airplane::find_seat()'!
    ___________________________________________________________
    class airplane {
    struct seat {
    seat* next;
    // [...]
    };

    struct row {
    row* next;
    seat* seats;
    };


    row* m_rows;


    seat* find_seat(
    std::size_t idx_row,
    std::size_t idx_col
    ) const {
    row* r = m_rows;
    while (r && idx_row > 1) {
    idx_row--;
    r = r->next;
    }
    if (r && idx_row == 1) {
    seat* s = r->seats;
    while (s && idx_col > 1) {
    idx_col--;
    s = s->next;
    }
    if (s && idx_col == 1) {
    return s;
    }
    }
    return NULL;
    }


    public:
    // [...]
    };

    ___________________________________________________________


    Sorry about that non-sense. ;^)
     
    Chris Thomasson, Mar 10, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    488
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    477
    emerth
    Jul 10, 2003
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    413
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Chris Angelico

    Re: Airplane mode control using Python?

    Chris Angelico, Dec 23, 2013, in forum: Python
    Replies:
    30
    Views:
    252
    Dave Angel
    Dec 27, 2013
  5. Michael Torrie

    Re: Airplane mode control using Python?

    Michael Torrie, Dec 23, 2013, in forum: Python
    Replies:
    0
    Views:
    244
    Michael Torrie
    Dec 23, 2013
Loading...

Share This Page