Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE READ THE INSTRUCTIONS IN THE COMMENTS FOR THE CODING QUESTIONS // READ BE

ID: 3798948 • Letter: P

Question

 PLEASE READ THE INSTRUCTIONS IN THE COMMENTS FOR THE CODING QUESTIONS // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases. // // The following will be accepted as input in the following format: "name:gender:standard:roll_number:tuition_fee" // Example Input: "Tom:M:3rd:10:2000.10" or "Elsa:F:4th:15:2700" // Valid name: String containing alphabetical letters beginning with a capital letter // Valid gender: Char value 'M' or 'F' // Valid standard: String containing alpha-numeric letters beginning with a number // Valid roll_number: Positive integer value // Valid tuition fee: Float containing no more than 2 decimal value, for example: 1500.45 or 2000.7 or 2700 // All inputs will be a valid length and no more than the allowed number of students will be added to the list  #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  // included to check for memory leaks #define CRTDBG_MAP_ALLOC #include <crtdbg.h>    typedef enum { male = 0, female } gender; // enumeration type gender  struct student {         char name[30];         gender genderValue;         char standard[30];         int roll_number;         float tuition_fee;         struct student* next; } *list = NULL;  // forward declaration of functions void flush(); void branching(char); void registration(char); int add(char*, char*, char*, int, float); // 15 struct student* search(char*); // 5 void remove_one(char*); // 10 void remove_all(struct student*); // 5 struct student* create_list_by_tuition_fee(float tuition); // 15 void display(struct student*);  int main() {         char ch = 'i';         printf("Assignment 6: Linked Lists  ");         printf("Student Information  ");          do         {                 printf("Please enter your selection: ");                 printf("	a: add a new student to the list ");                 printf("	s: search for a student on the list ");                 printf("	d: display list of student ");                 printf("	r: remove a student from the list ");                 printf("	p: print list of students with at least a certain amount of tuition fee ");                 printf("	q: quit ");                 ch = tolower(getchar());                 flush();                 branching(ch);         } while (ch != 'q');          remove_all(list);         list = NULL;         _CrtDumpMemoryLeaks(); // check for memory leaks (VS will let you know in output if they exist)         return 0; }  // consume leftover ' ' characters void flush() {         int c;         do c = getchar(); while (c != ' ' && c != EOF); }  // branch to different tasks void branching(char c) {         switch (c)         {         case 'a':         case 's':         case 'r':         case 'p': registration(c); break;         case 'd': display(list); break;         case 'q': break;         default: printf("Invalid input! ");         } }  // The registration function is used to determine how much information is needed and which function to send that information to. // It uses values that are returned from some functions to produce the correct ouput. // There is no implementation needed here, but you should study this function and know how it works. // It is always helpful to understand how the code works before implementing new features. // Do not change anything in this function or you risk failing the automated test cases. void registration(char c) {         char input[100];          if (c == 'a')         {                 printf(" Please enter the student's information in the following format: ");                 printf("	name:gender:standard:roll_number:tuition_fee ");                 fgets(input, sizeof(input), stdin);                  // discard ' ' chars attached to input                 input[strlen(input) - 1] = '';                  char* name = strtok(input, ":"); // strtok used to parse string                 char* genderValueString = strtok(NULL, ":");                 char* standard = strtok(NULL, ":");                 int roll_number = atoi(strtok(NULL, ":")); // atoi used to convert string to int                 float tuition_fee = atof(strtok(NULL, ":")); // atof used to convert string to float                  int result = add(name, genderValueString, standard, roll_number, tuition_fee);                  if (result == 0)                         printf(" That student is already on the list  ");                 else                         printf(" Student added to list successfully  ");         }         else if (c == 's' || c == 'r')         {                 printf(" Please enter the student's name: ");                 fgets(input, sizeof(input), stdin);                  // discard ' ' chars attached to input                 input[strlen(input) - 1] = '';                  struct student* result = search(input);                  if (result == NULL)                         printf(" That student is not on the list  ");                 else if (c == 's')                         printf(" Standard: %s  ", result->standard);                 else if (c == 'r')                 {                         remove_one(input);                         printf(" Student removed from the list  ");                 }         }         else // c = 'p'         {                 float tuition=0.0;                  printf(" Please enter a tuition amount: ");                 scanf("%f",&tuition); getchar();                  struct student* result = create_list_by_tuition_fee(tuition);                  display(result);                 remove_all(result);         } }  // Q1: add (15) // Similar to hw05, you will be inserting into a list of students sorted alphabetically by their name. // Differing from hw05, there is no limit to how many students can be on this list. // For this assignment, you can also assume that no 2 students will have the same name. // NOTE: You still need to check if the input student already exists on the list. // // This means that if the name of the student to be added matches the name of a student on the list, // then it is enough to assume that the student is already on the list and should not be added. // To clarify, you will be tested to assure that a student is not added to the list twice. // // If the student is already on the list, do not add the student to the list AND return the integer value 0. // If the student is not on the list, add the student to the list and return the integer value 1. // // "list" is initialized as NULL. Use this as your 'head' of the list and insert into it accordingly.  // NOTE: You will need to allocate memory (malloc) for a new node to add to the linked list of students. // // To insert the student at sorted place, you need to consider 4 possibilities for inserting into the list: //      - inserting into an empty list //      - inserting at the beginning of the list //      - inserting inbetween 2 nodes in the list //      - inserting at the end of the list int add(char* name, char* genderValueString, char* standard, int roll_number, float tuition_fee) {                  return 0; }  // Q2 : search (5 points) // In this function, you are passed the name of a student to find its standard. // If the student exists on the list, return a pointer to the requested student. If not, return NULL. // (You must return a pointer to a node in your list. Do not create a pointer that just includes the standard) // (Remember that it is enough to search for a student by only their name since no 2 students will have the same name) struct student* search(char* name) {         return NULL; }  // Q3: remove_one (10) // In this function, you are passed the name of a student to remove the corresponding student from the list. // The search function is called before this function so you can assume that the student is on the list. // You will need to find the student and remove it using proper memory management to ensure no memory leaks. // (Remember that it is enough search for a student by their name since no 2 students will have the same name) void remove_one(char* name) {      }  // Q4: remove_all (5) // Recursively all of the students from the list while using proper memory management to ensure no memory leaks. // If memory leaks are detected, the _CrtDumpMemoryLeaks() call in main will notify you in the output window. // This function is called in main before the program terminates, you only need to implement the code. void remove_all(struct student* students) {  }  // Q5: create_list_by_tuition_fee (15) // In this function, you will read the existing linked list pointed to by head pointer "list". The function takes   // a parameter tuition fee and will return another list of students that are of at least a certain tuition fee. // The returned list should contain all students with a tuition fee that is greater than or equal (>=) to the parameter tuition fee. // The returned list should still be in alphabetical order. You need to add to the returned list accordingly. // NOTE: If you have implemented your add() function correctly, your list should already be in alphabetical order. struct student* create_list_by_tuition_fee(float tuition) {         return NULL; }  // This function displays the list of students and the information for each one. It is already implemented for you. // It may be helpful to trace through this function to help you complete other sections of this assignment. void display(struct student* students) {         struct student* temp = students;         char* genderValue;          if (students == NULL)                 printf(" There are no students on this list!  ");         else         {                 while (temp != NULL)                 {                         printf(" Name: %s ", temp->name);                          if (temp->genderValue == male)                                 genderValue = "Male";                         else                                 genderValue = "Female";                          printf("Gender: %s ", genderValue);                         printf("Standard: %s ", temp->standard);                         printf("Roll No: %d  ", temp->roll_number);                         printf("Tuition Fees: $%.2f  ", temp->tuition_fee);                          temp = temp->next;                 }                 printf(" ");         } } 

Explanation / Answer

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

// included to check for memory leaks
#define CRTDBG_MAP_ALLOC
#include <crtdbg.h>

typedef enum { male = 0, female } gender; // enumeration type gender

struct student {
   char name[30];
   gender genderValue;
   char standard[30];
   int roll_number;
   float tuition_fee;
   struct student* next;
} *list = NULL;

// forward declaration of functions
void flush();
void branching(char);
void registration(char);
int add(char*, char*, char*, int, float); // 15
struct student* search(char*); // 5
void remove_one(char*); // 10
void remove_all(struct student*); // 5
struct student* create_list_by_tuition_fee(float tuition); // 15
void display(struct student*);

int main()
{
   char ch = 'i';
   printf("Assignment 6: Linked Lists ");
   printf("Student Information ");

   do
   {
       printf("Please enter your selection: ");
       printf(" a: add a new student to the list ");
       printf(" s: search for a student on the list ");
       printf(" d: display list of student ");
       printf(" r: remove a student from the list ");
       printf(" p: print list of students with at least a certain amount of tuition fee ");
       printf(" q: quit ");
       ch = tolower(getchar());

       flush();
       branching(ch);
   } while (ch != 'q');

   remove_all(list);
   list = NULL;
   _CrtDumpMemoryLeaks(); // check for memory leaks (VS will let you know in output if they exist)
   return 0;
}

// consume leftover ' ' characters
void flush()
{
   int c;
   do c = getchar(); while (c != ' ' && c != EOF);
}

// branch to different tasks
void branching(char c)
{
   switch (c)
   {
   case 'a':
   case 's':
   case 'r':
   case 'p': registration(c); break;
   case 'd': display(list); break;
   case 'q': break;
   default: printf("Invalid input! ");
   }
}

// The registration function is used to determine how much information is needed and which function to send that information to.
// It uses values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should study this function and know how it works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void registration(char c)
{
   char input[100];

   if (c == 'a')
   {
       printf(" Please enter the student's information in the following format: ");
       printf(" name:gender:standard:roll_number:tuition_fee ");
       fflush(stdin);
       fgets(input, sizeof(input), stdin);
      
       // discard ' ' chars attached to input
       input[strlen(input) - 1] = '';
      
       char* name = strtok(input, ":"); // strtok used to parse string
      
       char* genderValueString = strtok(NULL, ":");
       char* standard = strtok(NULL, ":");
       int roll_number = atoi(strtok(NULL, ":")); // atoi used to convert string to int
       float tuition_fee = atof(strtok(NULL, ":")); // atof used to convert string to float

       int result = add(name, genderValueString, standard, roll_number, tuition_fee);

       if (result == 0)
           printf(" That student is already on the list ");
       else
           printf(" Student added to list successfully ");
   }
   else if (c == 's' || c == 'r')
   {
       printf(" Please enter the student's name: ");
       fgets(input, sizeof(input), stdin);

       // discard ' ' chars attached to input
       input[strlen(input) - 1] = '';

       struct student* result = search(input);

       if (result == NULL)
           printf(" That student is not on the list ");
       else if (c == 's')
           printf(" Standard: %s ", result->standard);
       else if (c == 'r')
       {
           remove_one(input);
           printf(" Student removed from the list ");
       }
   }
   else // c = 'p'
   {
       float tuition = 0.0;

       printf(" Please enter a tuition amount: ");
       scanf("%f", &tuition); getchar();

       struct student* result = create_list_by_tuition_fee(tuition);

       display(result);
       remove_all(result);
   }
}

// Q1: add (15)
// Similar to hw05, you will be inserting into a list of students sorted alphabetically by their name.
// Differing from hw05, there is no limit to how many students can be on this list.
// For this assignment, you can also assume that no 2 students will have the same name.
// NOTE: You still need to check if the input student already exists on the list.
//
// This means that if the name of the student to be added matches the name of a student on the list,
// then it is enough to assume that the student is already on the list and should not be added.
// To clarify, you will be tested to assure that a student is not added to the list twice.
//
// If the student is already on the list, do not add the student to the list AND return the integer value 0.
// If the student is not on the list, add the student to the list and return the integer value 1.
//
// "list" is initialized as NULL. Use this as your 'head' of the list and insert into it accordingly.
// NOTE: You will need to allocate memory (malloc) for a new node to add to the linked list of students.
//
// To insert the student at sorted place, you need to consider 4 possibilities for inserting into the list:
// - inserting into an empty list
// - inserting at the beginning of the list
// - inserting inbetween 2 nodes in the list
// - inserting at the end of the list
int add(char* name, char* genderValueString, char* standard, int roll_number, float tuition_fee)
{
   struct student *cur = list,*newnode;
   newnode = (struct student *)malloc(sizeof(struct student ));
   newnode->next = NULL;
   strcpy(newnode->name, name);
   newnode->roll_number = roll_number;
   strcpy(newnode->standard, standard);
   if (strcmp(genderValueString, "M") == 0 || strcmp(genderValueString, "m") == 0)
       newnode->genderValue = male;
   if (strcmp(genderValueString, "F") == 0 || strcmp(genderValueString, "f") == 0)
       newnode->genderValue = female;
   newnode->tuition_fee = tuition_fee;
   if (list == NULL)
   {
       list = newnode;
   }
   else
   {
       while (cur->next != NULL)
       {
           cur = cur->next;
       }
       cur->next = newnode;
   }
   return 0;
}

// Q2 : search (5 points)
// In this function, you are passed the name of a student to find its standard.
// If the student exists on the list, return a pointer to the requested student. If not, return NULL.
// (You must return a pointer to a node in your list. Do not create a pointer that just includes the standard)
// (Remember that it is enough to search for a student by only their name since no 2 students will have the same name)
struct student* search(char* name)
{
   struct student *cur = list,*tmp;
  
   while (cur != NULL)
   {
       if (strcmp(cur->name, name) == 0)
       {
           return cur;
       }
       cur = cur->next;
   }
   return NULL;
}

// Q3: remove_one (10)
// In this function, you are passed the name of a student to remove the corresponding student from the list.
// The search function is called before this function so you can assume that the student is on the list.
// You will need to find the student and remove it using proper memory management to ensure no memory leaks.
// (Remember that it is enough search for a student by their name since no 2 students will have the same name)
void remove_one(char* name)
{
   struct student *cur = list, *tmp,*next;
   if (strcmp(cur->name, name) == 0)
   {
       tmp = list;
       list = cur->next;
       free(tmp);
       return;
   }
   while (cur!= NULL)
   {
       if (strcmp(cur->next->name, name) == 0)
       {
           tmp = cur->next;
           next = tmp->next;
           cur->next = next;
           free(tmp);
           return;
       }
       cur = cur->next;
   }
}

// Q4: remove_all (5)
// Recursively all of the students from the list while using proper memory management to ensure no memory leaks.
// If memory leaks are detected, the _CrtDumpMemoryLeaks() call in main will notify you in the output window.
// This function is called in main before the program terminates, you only need to implement the code.
void remove_all(struct student* students)
{
   struct student *cur = list,*tmp;

   while (cur != NULL)
   {
       tmp = cur;
       cur = tmp->next;
       free(tmp);
   }
}

// Q5: create_list_by_tuition_fee (15)
// In this function, you will read the existing linked list pointed to by head pointer "list". The function takes
// a parameter tuition fee and will return another list of students that are of at least a certain tuition fee.
// The returned list should contain all students with a tuition fee that is greater than or equal (>=) to the parameter tuition fee.
// The returned list should still be in alphabetical order. You need to add to the returned list accordingly.
// NOTE: If you have implemented your add() function correctly, your list should already be in alphabetical order.
struct student* create_list_by_tuition_fee(float tuition)
{
   struct student *cur = list, *cur1,*tmp=NULL,*newnode;
   newnode = (struct student *)malloc(sizeof(struct student *));
   while (cur != NULL)
   {
       if (cur->tuition_fee >= tuition)
       {
           char s[2];
           if (cur->genderValue == male)
               strcpy(s, "M");
           if (cur->genderValue == female)
               strcpy(s, "F");
          
           newnode->next = NULL;
           strcpy(newnode->name, cur->name);
           newnode->roll_number = cur->roll_number;
           strcpy(newnode->standard, cur->standard);
           newnode->genderValue = cur->genderValue;
          
           newnode->tuition_fee = tuition;
           if (tmp == NULL)
           {
               list = newnode;
               cur1 = tmp;
           }
           else
           {
               while (cur1->next != NULL)
               {
                   cur1 = cur1->next;
               }
               cur1->next = newnode;
           }
                      
       }
       cur = cur->next;
   }
   return tmp;
}

// This function displays the list of students and the information for each one. It is already implemented for you.
// It may be helpful to trace through this function to help you complete other sections of this assignment.
void display(struct student* students)
{
   struct student* temp = students;
   char* genderValue;

   if (students == NULL)
       printf(" There are no students on this list! ");
   else
   {
       while (temp != NULL)
       {
           printf(" Name: %s ", temp->name);

           if (temp->genderValue == male)
               genderValue = "Male";
           else
               genderValue = "Female";

           printf("Gender: %s ", genderValue);
           printf("Standard: %s ", temp->standard);
           printf("Roll No: %d ", temp->roll_number);
           printf("Tuition Fees: $%.2f ", temp->tuition_fee);

           temp = temp->next;
       }
       printf(" ");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote