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
		
		

	
	
	

}

Reading this code is a pain..Can you format the code properly and use the language specific code tags? Also please point the function where you are printing the contents.

I did some correction to your code and now it is working ;)
Your fault was in set next node address in linked lists

#include <stdio.h>
#include <string.h>
#include <stdlib.h>



#define FIRSTCLASS   0x01
#define ECONOMYCLASS 0x02
#define FIRSTSEATS 40
#define ECONOMYSEATS 40
#define SEATSIZE (FIRSTSEATS+ECONOMYSEATS)
#define LENGTH 25

typedef struct BPassStruct 
{
        int classType;
        int seat;
        char firstname[SEATSIZE];
        char lastname[SEATSIZE];
} bPass;

typedef struct NodeStruct 
{
        bPass data;
        struct NodeStruct *next;
} node;


/* function prototypes */
void askFlight( int,int[], node * );
void askName( int, node * );
void printBPass( node * );
void printHeading( void );
void chkFClass( int, int, int, int, int[], node * );
void chkEClass( int, int, int, int, int[], node * );
void prFlight( node *headNode, int seatnumber, int seat[]);



int main(void)
{
	int seatnumber = 0; /* passenger seat number */
	int seat[SEATSIZE] = {0}; /* array to verify seats availible */

	int choice = 0;
	node *headNode = NULL;
	node *newNode;
    node *currentNode;

	do
    {
		newNode = (node *)malloc( sizeof( node ) );
	    askFlight( choice, seat, newNode ); /* prompt user to choose flight */
        newNode->next = headNode;
        headNode = newNode;
     	puts( "\nWould you like to board another flight?" ); /* prompt user to choose to board flight */
      	scanf( "%*c%c", &choice ); /* read in choice */

	}
    while( choice == 'y' || choice == 'Y' );

	if( choice=='n' || choice=='N' ) /* if choice = no print plane roster */
    {
		prFlight( headNode, seatnumber, seat );
		currentNode = headNode;
		while( currentNode!=NULL )
        {
			currentNode = currentNode->next;
			free( currentNode );
		}
	}

    system("PAUSE");
    return EXIT_SUCCESS;

}
void askFlight( int choice, int seat[], node *newNode )
{
	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.\n" ); /* prompt user to choose between firstclass and economy */
    scanf( "%d", &choice ); /* read in choice  */

    chkFClass( seatnumber, fclassChoice, economyChoice, choice, seat, newNode ); //check if seats are availible in firstclass
    chkEClass( seatnumber, fclassChoice, economyChoice, choice, seat, newNode ); //check if seats are availible in economyclass

}

void askName( int seatnumber, node *newNode )
{
	printf( "Please enter your first name:\n" );//prompt user to enter first name
	scanf( "%s", newNode->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", newNode->data.lastname );// read in lastname into the element of lastname array
                                      //corresponding with seatnumber
    newNode->data.seat = seatnumber;
	printHeading(); // print B-pass heading
	printBPass( newNode ); // print b-pass
	
	



}

void printBPass( node *newNode )
{
	printf( "\n%s %5s%10d", newNode->data.firstname, newNode->data.lastname, newNode->data.seat ); // 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 *newNode )
{
     if ( choice==1) // if user chooses firstclass
     {
          newNode->data.classType = FIRSTCLASS;
          for( seatnumber=1 ; seatnumber<FIRSTSEATS ; seatnumber++ )//loop through seats array to check seat avail.
          {
               if(seat[seatnumber]==0)
               {
                    seat[seatnumber]=1; // seat value in seat element to 1 showing its taken
                    askName( seatnumber, newNode ); // prompt user for name
                    break; // continue asking for flight
               }
          }
     }

     if( seatnumber>FIRSTSEATS ) // check if firstclass is full
	 {
         
		 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;

         }
		 chkEClass( seatnumber, fclassChoice, economyChoice, choice, seat, newNode );// check seats in economy
     }
}

void chkEClass( int seatnumber, int fclassChoice, int economyChoice, int choice, int seat[], node *newNode )
{
	 if(choice==2) // if user chooses economy
	 {
         newNode->data.classType = ECONOMYCLASS;
         for( seatnumber=FIRSTSEATS ; seatnumber<FIRSTSEATS+ECONOMYSEATS; seatnumber++ ) // loop thru to check if seats are availible
         {
              if(seat[seatnumber]==0)
              {
                   seat[seatnumber]=1; // seat value in seat element to 1 showing its taken
                   askName( seatnumber, newNode ); // prompt user to enter name
                   break;              
              }
         }
     }
     if (seatnumber>SEATSIZE)//check if economy is full
     {    
          
		  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)
          {
			 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
		  }
		  chkFClass( seatnumber, fclassChoice, economyChoice, choice, seat, newNode );//check seats in first class
     }
		 		 
}

void prFlight( node *headNode, int seatnumber, int seat[] )
{
	int cntFirst=0; // counter
	int cntEconomy=0; // counter
	int i=0; // counter to index array
	int j=0; // counter to index array
   
    node *currentNode = headNode;
     
	printf("\n\n********FIRST CLASS**********\n"); // custom heading

	printHeading(); // print headingy

	while( currentNode != NULL )
    { // loop thru contents of array
         if( currentNode->data.classType==FIRSTCLASS )
         {
             cntFirst++;
         }
         else if( currentNode->data.classType==ECONOMYCLASS )
         {
              cntEconomy++;
         }
         
		 printf("\n%s %5s%10d", currentNode->data.firstname, currentNode->data.lastname, currentNode->data.seat); // print out names and seatnumbers
		 currentNode = currentNode->next;
	}
	
	printf( "\nNumber of people in first class is %d:", cntFirst ); // print out number of people in firstclasss
	printf( "\nNumber of people in economy class is %d:", cntEconomy ); // print out number of people in economyclasss
	
}
This article has been dead for over six months. Start a new discussion instead.