Alphabetizing Linked List data

J

jawdoc

In my program I am doing for a school project, I have all of which is
needed except for alphabetizing the data as it is entered. This is an
airplane program where the user enters in his/her last name and first
name and other data. Once they have entered this and verified that it
is legit, I am supposed to have code to alphabetize the entries. Also
my remove cancelReservation somehow has an error when I'm trying to
delete a SEAT at the end of the list. Everything else is working fine
and taking the entries and able to list them perfectly.

Any help with alphabetizing and that small error is greatly
appreciated

/
*-----------------------------------------------------------------------
airplane.cpp
----------------
Course : CSCI 1063 -- Programming II
Description: Airline reservations program ... I will given them
parts of this and they will have to finish it.
Refer to the menu (or the writeup) for information
about what exactly this program is supposed to do.

-----------------------------------------------------------------------
*/
#include<iostream>
#include<string>
#include<cctype>
#include<iomanip>
#include<cmath>

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
int row;
char col;
SEAT *next;
};

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

int menuchoice();
void clearPlane(SEAT &p, bool o[][COLS]);
void showPlane(SEAT &p, bool o[][COLS]);
bool getSeat(SEAT &p, int &row, int &col, bool o[][COLS]);
void getSeatFromUser(SEAT &p, 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], SEAT *&head);
void cancelReservation(SEAT &p, bool o[][COLS], SEAT *&head);
void viewReservations(SEAT &p, bool o[][COLS], SEAT *&head);

int main()
{
int choice; // user's menu choice
SEAT plane; // reservation info for entire plane
bool occupied[ROWS][COLS];
SEAT *head;
clearPlane(plane, occupied);
showPlane(plane, occupied);
do
{
cin.clear();
choice= menuchoice();
cin.ignore();
switch (choice) {
case 1: makeReservation(plane, occupied, head); break;
case 2: cancelReservation(plane, occupied, head); break;
case 3: viewReservations(plane, occupied, head); 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(SEAT &p, 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])
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(p,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(SEAT &p, int &row, int &col, bool o[][COLS])
{
char let;

showPlane(p,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, bool o[][COLS])
{
s=new SEAT;
s->row= row;
s->col= col;
cin.ignore(100,'\n');
string answer;
do{


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

cout<<"Enter your first name: ";

cin>>s->first;
cout<<"\n";

cout<<"Enter your last name: ";

cin>>s->last;
cout<<"\n";

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!="y" && answer!="no");

cin.clear();
s->next=NULL;
}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: "<<(s->row+1)<<", "<<(char)(s->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], SEAT *&head)
{
int row;
int col;

SEAT *temp;
SEAT *j, *q;

do{
cout<<"\n";
getSeat(p,row, col, o);
if(o[row][col]==true)
cout<<"\nSeat is already taken.\n";
}while(o[row][col]==true);

o[row][col]=true;

getInfo(temp,row,col,o);
temp->next=head;
head=temp;

/* for(temp = head; temp->next !=0 && temp->next->last[0] < head-
last[1]; temp = temp->next)
{
SEAT *swp= temp->next;
temp->next = head;
head->next = swp;
}*/



/* for(int i=0; i<temp->last.length(); i++)
{
if( temp->last < head->last)
{
temp->next=head;
head=temp;
}

else{
j=head;
while(j !=NULL && j->last < temp->last)
{
q=j;
j=j->next;
}
temp->next =j;
q->next=temp;
}
}*/

}

/*--------------------------------------------------------
* 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], SEAT *&head)
{
int row;
int col;
getSeatFromUser(p, row, col, o);


if(o[row][col]==false)
cout<<"\nThe reservation is empty.";
else
o[row][col]=false;


SEAT *j;
SEAT *q;
j = head;
if(head==NULL)
return;
if(head->row == row && head->col==col)
{
head = j->next;
delete j;
}

else
{
j= head->next;
while(j!=NULL && j->row != row && j->col != col)
{
q=j;
j=j->next;
}

if( j!=NULL){
q->next = j->next;
delete j;
}
}


}

/*--------------------------------------------------------
* 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], SEAT *&head)
{
SEAT *view;
view=head;
int i;
int n;




cout<<"-----------------------------------------------------------------
\n";
cout<<"| SEAT NAME # of BAGS
MEAL |";

cout<<"\n-----------------------------------------------------------------
\n";
while(view->next!=NULL)
{
cout.setf(ios::left);
if(view->last.length() +view->first.length() == 12)
{
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}

if(view->last.length() == view->first.length() && view-
first.length() == 1)
{
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10);
cout<<" "<<view->last<<", "<<view->first;
cout.width(22);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}



if(view->last.length() + view->first.length() < 12 && view-
last.length() > view->first.length() && view->last.length()<6)
{
i=(view->last.length()-view->first.length());
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10+i);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}

if(view->last.length() + view->first.length() < 12 && view-
last.length() > view->first.length() && view->last.length()>6)
{
i=(view->last.length()-view->first.length());
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10+i-3);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12+i);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}

if(view->last.length() + view->first.length() < 12 && view-
last.length() < view->first.length())
{
i=view->first.length() - view->last.length();
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12+i);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}

if(view->last.length() + view->first.length() < 12 && view-
last.length() == view->first.length() && view->first.length()!=1)
{
i= 12-(view->first.length() + view->last.length());
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12+i);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}

if(view->last.length() + view->first.length() > 12 && view-
last.length() > view->first.length())
{
i=view->last.length() - view->first.length();
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12-i);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}

if(view->last.length() + view->first.length() > 12 && view-
last.length() < view->first.length())
{
i= view->first.length()-view->last.length() ;
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10-i+2);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}


if(view->last.length() + view->first.length() > 12 && view-
last.length() == view->first.length() && view->first.length() !=1)
{
i= 12-(view->first.length() + view->last.length());
cout<<" "<<(view->row+1)<<", "<<(char)(view->col+65);
cout.width(10);
cout<<" "<<view->last<<", "<<view->first;
cout.width(12-i);
cout<<" "<<view->numBags;
cout.width(15);
cout<<" "<<(char)toupper(view->mealType);
cout<<"\n";
}

view=view->next;
}

/* while(view->next!=NULL)
{
showInfo(view);
view=view->next;
}
*/
}
 
D

Daniel T.

jawdoc said:
In my program I am doing for a school project, I have all of which is
needed except for alphabetizing the data as it is entered.

You posted this question in alt.comp.lang.learn.c-c++ yesterday and I
practically gave you the answer.

Check back there.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top