I have been asked to create a student database using linked list.

this is the question given to me.

Write a C/C++ program to perform the task of maintaining student information for 123 College. You are to use a linked-list for storing the students’ information. Each node of the linked-list will be used to hold the information for one student, which includes the name, age and GPA (a double). Your program should have an insert(), deleteNode(), and printAverage() functions. The insert() and deleteNode() functions are not the typical ones, so do make a note of that.

The prototypes are given below:

void insert( Student **headOfList, char *name, int age, double balance );

This insert() function will always insert the new node as the second node of the list, unless the linked-list is empty, in which case it will be inserted as the first node. Hence, the nodes in the linked-list are not sorted.

int deleteNode( Student **headOfList, char *sname );

This deleteNode() function will delete the node whose student’s name matches sname and also the next node. Hence, each time the function is called, it deletes two consecutive nodes. However, if the node to be deleted is the last node, then only one node is deleted.

void printAverage( Student *headOfList );

This printAverage() function prints out all the information of all the students in the list. After performing that, it then prints out the average GPA of all the students in the list.

In the main() function of your program, perform the following tasks:

1) Add the following 4 students to the linked-list (which is initially empty), one at a time:
a) Bill, 18, 2.00
b) Choo, 19, 3.00
c) John, 22, 1.50
d) Tim, 18, 4.00

2) After adding the 4 customers, print out the whole list using the printAverage() function.

3) Add the following 2 customers to the list:
e) Lisa, 19, 2.00
f) Mary, 21, 1.00

4) Print out the whole list again using the printAverage() function.
5) Delete the student Choo from the list and print out the resulting list using the printAverage() function.

All output should be properly formatted so that the data is easily read.


I tried to create a sample coding using my understanding of the question given.

#include <stdio.h>
#include <stdlib.h>
                       
struct studentNode {                                      
   char name;
   int age;
   double balance;
   struct studentNode *nextPtr;
};                       

typedef struct studentNode StudentNode;
typedef StudentNode *StudentNodePtr;

/* prototypes */
void insert( StudentNodePtr *headOfList,  char name, int age, double balance );
char deletenode( StudentNodePtr  *headOfList, char sname  );
int isEmpty( StudentNodePtr sPtr );
void printList( StudentNodePtr currentPtr );
void instructions( void );

int main( void )
{ 
   StudentNodePtr startPtr = NULL;
   int choice;
   char nm;  
   int ag;
   double bln;

   instructions(); 
   printf( "? " );
   scanf( "%d", &choice );

 
   while ( choice != 3 ) { 

      switch ( choice ) { 

         case 1:
            printf( "Enter a name: " );
            scanf( "\n%s", &nm );
			printf( "Enter age: " );
            scanf( "\n%d", &ag );
			printf( "Enter GPA : " );
            scanf( "\n%lf", &bln );
            insert( &startPtr, nm, ag, bln ); 
            printList( startPtr );
            break;

         case 2: 

           
            if ( !isEmpty( startPtr ) ) { 
               printf( "Enter name to be deleted: " );
               scanf( "\n%s", &nm );

               if ( deletenode( &startPtr, nm ) ) { 
                  printf( "%s deleted.\n", nm );
                  printList( startPtr );
               } 
               else {
                  printf( "%s not found.\n\n", nm );
               }

            }
            else {
               printf( "List is empty.\n\n" );
            } 

            break;

         default:
            printf( "Invalid choice.\n\n" );
            instructions();
            break;
      
      } 

      printf( "? " );
      scanf( "%d", &choice );
   } 

   printf( "End of run.\n" );
   
   return 0; 

} 


void instructions( void )
{ 
   printf( "Enter your choice:\n"
      "   1 to insert an element into the list.\n"
      "   2 to delete an element from the list.\n"
      "   3 to end.\n" );
} 


void insert( StudentNodePtr *headOfList,  char name, int age, double balance )
{ 
   StudentNodePtr newPtr;      
   StudentNodePtr previousPtr; 
   StudentNodePtr currentPtr;  

   newPtr = ( StudentNode * ) malloc( sizeof( StudentNode ) ); 

   if ( newPtr != NULL ) { 
      newPtr->name = name;
	  newPtr->age = age;
	  newPtr->balance = balance;
      newPtr->nextPtr = NULL; 

      previousPtr = NULL;
      currentPtr = *headOfList;

            
      if ( currentPtr != NULL ) {
         previousPtr = currentPtr;          
         currentPtr = currentPtr->nextPtr; 
      } 
    
      if ( previousPtr == NULL ) { 
         newPtr->nextPtr = *headOfList;
         *headOfList = newPtr;
      } 
      else { 
         previousPtr->nextPtr = newPtr;
         newPtr->nextPtr = currentPtr;
      }
   
   }
   else {
      printf( "%s not inserted. No memory available.\n", name);
   } 

  } 

char deletenode( StudentNodePtr  *headOfList, char sname  )
{ 
   StudentNodePtr previousPtr; 
   StudentNodePtr currentPtr;  
   StudentNodePtr tempPtr;     

 
   if ( sname == ( *headOfList )->name ) { 
      tempPtr = *headOfList; 
      *headOfList = ( *headOfList )->nextPtr; 
      free( tempPtr ); 
	  tempPtr = *headOfList; 
      *headOfList = ( *headOfList )->nextPtr; 
      free( tempPtr );
      return sname;
   } 
   else { 
      previousPtr = *headOfList;
      currentPtr = ( *headOfList )->nextPtr;

      
      while ( currentPtr != NULL && currentPtr->name != sname ) { 
         previousPtr = currentPtr;         
         currentPtr = currentPtr->nextPtr; 
      } 

      
      if ( currentPtr != NULL ) { 
         tempPtr = currentPtr;
         previousPtr->nextPtr = currentPtr->nextPtr;
         free( tempPtr );
		 tempPtr = currentPtr;
         previousPtr->nextPtr = currentPtr->nextPtr;
         free( tempPtr );
         return sname;
      } 
     
   } 

   return '\0';

  } 


  int isEmpty( StudentNodePtr sPtr )
  { 
   return sPtr == NULL;

  } 

  
  void printList( StudentNodePtr currentPtr )
  { 

   
   if ( currentPtr == NULL ) {
      printf( "List is empty.\n\n" );
   } 
   else { 
      printf( "The list is:\n" );

      while ( currentPtr != NULL ) { 
         printf( "%s --> ", currentPtr->name);
         currentPtr = currentPtr->nextPtr;   
      } 

      printf( "NULL\n\n" );
   } 

  }

I have 2 questions.
1.
void insert( Student **headOfList, char *name, int age, double balance );
Why does the question use two asterisks in front of headOfList? Is it different from using one asterisk?

2. When i compiled the coding above, it did not give me any errors. When i run the codes, it gives me an error right after i finish keying in the details for a student to insert a node. I don't understand why.

Any help is kindly appreciated. :)

Recommended Answers

All 4 Replies

> Why does the question use two asterisks in front of headOfList?
So you can return a modified list.

> Is it different from using one asterisk?
Yes.

> I don't understand why.
How many characters (as a string) can you store in nm ?

> Why does the question use two asterisks in front of headOfList?
So you can return a modified list.

> Is it different from using one asterisk?
Yes.

> I don't understand why.
How many characters (as a string) can you store in nm ?

Thank you for replying, Salem.
I would like to store about 10 characters in nm

Ok, i have tried my best to come up with the solution on my own. I just need someone to check it for me. Thank you.

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


struct StudentNode {
   char name[10];
   int age;
   double balance;
   struct StudentNode *nextPtr;
}; 

typedef struct StudentNode Student;
typedef Student *StudentPtr;

void insert( Student **headOfList,  char *name, int age, double balance )
{
	StudentPtr newPtr;
	StudentPtr previousPtr;
	StudentPtr currentPtr;

	newPtr = ( Student * ) malloc( sizeof( Student ) );
	
	if ( newPtr != NULL ) {
		strcpy(newPtr->name, name);
		newPtr->age = age;
		newPtr->balance = balance;
		newPtr->nextPtr = NULL;
		
		previousPtr = NULL;
		currentPtr = *headOfList;
		
		if ( currentPtr != NULL ) {
			previousPtr = currentPtr;
			currentPtr = currentPtr->nextPtr;
		}
		
		if ( previousPtr == NULL ) {
			newPtr->nextPtr = *headOfList;
			*headOfList = newPtr;
		}
		else {
			previousPtr->nextPtr = newPtr;
			newPtr->nextPtr = currentPtr;
		}
	}
	
	else {
		printf( "%s not inserted. No memory available.\n", name );
	}
}

void deleteNode( Student  **headOfList, char *sname )
{
	StudentPtr previousPtr;
	StudentPtr currentPtr;
	StudentPtr tempPtr;

	
	if ( sname == (*headOfList)->name ) { 
		tempPtr = (*headOfList);
		(*headOfList) = (*headOfList)->nextPtr;
		free( tempPtr );
		
	}
	else {
		previousPtr = (*headOfList);
		currentPtr = (*headOfList)->nextPtr;
		
		while (currentPtr->nextPtr != NULL && currentPtr->name != sname ) {
			previousPtr = currentPtr;
			currentPtr = currentPtr->nextPtr;  
		}

		if ( currentPtr->nextPtr != NULL) {
			tempPtr = currentPtr;
			previousPtr->nextPtr = currentPtr->nextPtr;
			previousPtr->nextPtr = currentPtr->nextPtr;
			free( tempPtr );
		}
		else {
			tempPtr = currentPtr;
			previousPtr->nextPtr = NULL;
			free( tempPtr );
		}
	}
	

}

void printAverage( Student *headOfList )
{
	double sum = 0.0;
	double number = 0.0;
	double average = 0.0;

		if ( headOfList == NULL ) {
		printf( "List is empty.\n\n" );
	}
	else {
		printf( "The list is:\n" );
		
		while ( headOfList != NULL ) {
			printf( "Name : %s Age : %d G.P.A. : %.2lf --> ", headOfList->name, headOfList->age, headOfList->balance );
			sum+=headOfList->balance;
			number+=1;
			headOfList = headOfList->nextPtr;
		}
		
		printf( "NULL\n\n" );

		average = sum / number;
		printf( "Average G.P.A is %.2lf\n\n", average);
	}


}	

int main(void)
{
	StudentPtr startptr = NULL;
	insert( &startptr, "Bill", 18, 2.00);
	insert( &startptr, "Choo", 19, 3.00);
	insert( &startptr, "John", 22, 1.50);
	insert( &startptr, "Tim", 18, 4.00);
	printAverage( startptr );
	insert( &startptr, "Lisa", 19, 2.00);
	insert( &startptr, "Mary", 21, 1.00);
	printAverage( startptr );
	deleteNode( &startptr, "Choo");
	printAverage( startptr );
	return 0;
}

> if ( sname == (*headOfList)->name )
Use strcmp() for comparing strings (both instances).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.