Hello. I am trying to do a project with linked lists. It seems that everything is coded correct in my code to create new nodes and move my pointers and such. But for some reason my function that is supposed to print all the names stored in my list only prints the last one entered. Any suggestions?
here is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 41
#define FCLASS 10
#define ECONOMY 30
#define FIRST 40
#define LAST 40
#define LENGTH 25
typedef struct BPassStruct {
int classType;
int seat;
char firstname[SIZE];
char lastname[SIZE];
} bPass;
typedef struct NodeStruct {
bPass data;
struct NodeStruct *next;
} node;
typedef struct BPassListStruct {
node *head;
} bPassList;
/*function prototypes*/
void askFlight(int,int[],node *,node *);
void askName(int,node *, node *);
void printBPass(/*char[][LENGTH],char[][LENGTH]*/node *,int);
void printHeading(void);
void chkFClass(int,int,int,int,int[],node *,node *);
void chkEClass(int,int,int,int,int[],node *, node *);
void prFlight(/*char[][LENGTH],char[][LENGTH]*/,int,int[]);
void sortName(void);
int main(void)
{
int seatnumber=0;/*passenger seat number*/
int seat[SIZE]={0};/*array to verify seats availible*/
char choice2;
//char firstname[SIZE][LENGTH]={" "};/*array to store user names*/
//char lastname[SIZE][LENGTH]={" "};/*array to store user names*/
int choice=0;
node *head=NULL;
node *temp;
do{
temp = malloc(sizeof(node));
askFlight(choice,seat,temp,head);/*prompt user to choose flight*/
puts("\nWould you like to board another flight?");/*prompt user to choose to board flight*/
scanf("%*c%c", &choice2);/*read in choice*/
}while (choice2 == 'y' || choice2 == 'Y');
if(choice2=='n' || choice2=='N'){/*if choice = no print plane roster*/
prFlight(temp,seatnumber,seat);
while(temp!=NULL){
head=head->next;
free(temp);
}
}
return 0;/*end main*/
}
void askFlight(int choice,int seat[],node *temp,node *head)
{/* begin askFlight*/
int seatnumber=0;/*holds customer seatnumber*/
char economyChoice=0;//holds char "choice"
char fclassChoice=0;//holds char "choice"
printf("Welcome to the Airline Reservations System\n\nWould you like to fly First class or economy?\n");
printf("Please type 1 for first class\nplease type 2 for economy.");//prompt user to choose between firstclass and economy
scanf("%d", &choice);//read in choice
chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp,head/*,firstname,lastname*/);//check if seats are availible in firstclass
chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp,head/*,firstname,lastname*/);//check if seats are availible in firstclass
//end askFlight function
}
void askName(int seatnumber,node *temp, node *head)
{// begin askName
//int i=0;//counter
printf("Please enter your first name:\n");//prompt user to enter first name
scanf("%s", temp->data.firstname);// read in firstname into the element of firstname array
// corresponding with seatnumber
printf("Please enter your last name:\n");//prompt user to enter lastname
scanf("%s", temp->data.lastname);// read in lastname into the element of lastname array
//corresponding with seatnumber
printHeading();//print B-pass heading
printBPass(/*firstname,lastname,*/temp,seatnumber);//print b-pass
temp->next=head;
head = temp;
}//end askName
void printBPass(node *temp, int seatnumber)
{
printf("\n%s %5s%10d",temp->data.firstname,temp->data.lastname,seatnumber);//prints out user b-pass
}
void printHeading(void)
{
printf("%s%15s", "Customer Name", "Seat Number");
}
void chkFClass(int fclassChoice,int economyChoice,int seatnumber,int choice,int seat[],node *temp,node *head)
{
if ( choice==1)//if user chooses firstclass
{//begin if
for( seatnumber=1 ; seatnumber<=FCLASS ; seatnumber++)//loop through seats array to check seat avail.
{//begin for
if(seat[seatnumber]==0)
{//begin if
seat[seatnumber]=1; //seat value in seat element to 1 showing its taken
askName(seatnumber,temp,head);// prompt user for name
break;//continue asking for flight
}//end if
}//end for
}// end id
if (seatnumber>FCLASS)//check if firstclass is full
{//begin if
printf("I am sorry, but the first class is full. Would you like like to fly economy?\n");
scanf("%*c%c", &economyChoice);//if firstclass is full ask user if they would like economy class
switch(economyChoice){
case 'y':
case 'Y':
choice=2; //read in choice
break;
case 'n':
case 'N':
printf("Next flight leaves in three hours.");//if user does not want firstclaass tell them to board different flight
break;
}//end switch
chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp,head);// check seats in economy
}//end if
}//end chkFclass
void chkEClass(int seatnumber,int fclassChoice,int economyChoice,int choice,int seat[],node *temp,node *head)
{
if(choice==2)//if user chooses economy
{//begin if
for( seatnumber=11 ; seatnumber<SIZE; seatnumber++)//loop thru to check if seats are availible
{//begin for
if(seat[seatnumber]==0)
{//begin if
seat[seatnumber]=1;//seat value in seat element to 1 showing its taken
askName(seatnumber,temp,head);//prompt user to enter name
break;
} //end if
}//end for
}//end if
if (seatnumber>SIZE)//check if economy is full
{//begin if
printf("I am sorry, but the economy class is full. Would you like like to fly first class?\n");
scanf("%*c%c", &fclassChoice);//if economy is full ask user if they would like first class
switch(fclassChoice){//begin switch
case 'y':
case 'Y':
choice=1; //read in choice
break;
case 'n':
case 'N':
printf("Next flight leaves in three hours.");
break;//if user does not want first class, tell them to board another flight
}//end switch
chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp,head);//check seats in first class
}//end if
}//end chkEclass
void prFlight(node *temp, int seatnumber,int seat[])
{
int cnt=0;//counter
int cnt2=0;//counter
int i=0;//counter to index array
int j=0;//counter to index array
printf("\n********FIRST CLASS**********\n");//custom heading
printHeading();//print headingy
while(temp != NULL){//loop thru contents of array
i++;
printf("\n%s %5s%10d",temp->data.firstname,temp->data.lastname,i);//print out names and seatnumbers
if (seat[i]==1)//if a seat is taken increase count of people on classs
cnt++;
temp = temp->next;
}
printf("\nNumber of people in first class is %d:",cnt);//print out number of people in firstclasss
printf("\n********FIRST CLASS**********");
printf("\n********ECONOMY**********\n");
printHeading();//print heading
while(temp!=NULL){//loop thru arrays to access info
j++;
if (seat[j]==1)//if a seat is taken increase count of people on economy
cnt2++;
printf("\n%s %5s%10d",temp->data.firstname,temp->data.lastname,j);//display roster for economy
temp = temp->next;
}
printf("\nNumber of people in economy class is %d:",cnt2);//print out number of people in economy
printf("\n********ECONOMY**********");
printf("\nTotal number of people on plane is %d\n", cnt+cnt2);//print out total number of people
}