#define _CRT_SECURE_NO_WARNINGS
#define PAUSE system("pause")
#define CLS system ("cls")
#define FLUSH myFlush();
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <ctype.h>
#include<time.h>
//Function Prototypes
void displayMenu();
int getChoice(int* option);
void myFlush();
typedef struct personType
{
char name[100];
int age;
float weight;
struct personType* next; //pointer next points
}Person; //to the next node
typedef Person* LIST; //pointer to struct STUDENT named LIST
main() {
LIST temp = NULL, start = NULL, linkedList = NULL; //declare to variable of type LIST
int ageTemp = 0, count = 1, counter;
char nameTemp[100], response;
float weightTemp;
int option;
do {
getChoice(&option);
switch (option) //begin switch
{
case 1:
do
{
CLS;
printf("\nEnter a name for person %d : ", count);
scanf("%[^\t\n]", &nameTemp); FLUSH;
printf("\nEnter the age for %s: ", nameTemp);
scanf("%i", &ageTemp); FLUSH;
printf("\nEnter the weight for %s: ", nameTemp);
scanf("%f", &weightTemp); FLUSH;
if (count == 1)
{
// create a node and store the information
// create the head of the list
linkedList = (LIST)malloc(sizeof(Person));
start = linkedList; // store the top/head/start of the list
strcpy(start->name, nameTemp);
start->age = ageTemp;
start->weight = weightTemp;
start->next = NULL;
}
else
{
temp = (LIST)malloc(sizeof(Person)); // create address of next node
linkedList->next = temp; // assign next pointer in STUDENT struct the address of next node
linkedList = temp;
strcpy(temp->name, nameTemp);
temp->age = ageTemp;
temp->weight = weightTemp;
temp->next = NULL;
}
printf("\nWould you like to enter another person?");
printf("\nEnter Y/N: ");
scanf("%c", &response); FLUSH;
count++;
} while (response == 'Y' || response == 'y');
CLS;
break;
case 2:
temp = start; // assigns temp to start
// begin at the head of the list
CLS;
counter = 1;
while (temp != NULL) //begin while
{
printf("Student %i: %s\t Age: %2i Weight: %.2f \n",
counter, temp->name, temp->age, temp->weight);
counter += 1;
temp = temp->next; //iterates through the temp values
} //end while
printf("\n\n");
PAUSE;
CLS;
break;
case 3:
printf("Exiting program...\n");
printf("See you next time :)");
break;
default:
CLS;
printf("That was not a valid selection...");
PAUSE;
} // end Switch
} while (option != 3);
// Free The Memory
while (start != NULL) //checks if start is pointing to null
{
temp = start; // points temp node to where start is pointing
start = start->next; // iterates to next node that start points to
free(temp); // frees memory allocated for list
} // endwhile
}// End of Main
void myFlush()
{
while (getchar() != '\n');
}
void displayMenu()
{
CLS;
printf("Please select from the following options:\n\n");
printf("1. Add a Record.\n");
printf("2. Display All Records.\n");
printf("3. Quit.\n\n\n");
printf("Please enter a selection: ");
} // end of displayMenu
int getChoice(int* option)
{
displayMenu();
scanf("%i", &*option); FLUSH;
return toupper(*option);
} // ends getChoice function
I don't know how to sort the linked list alphabetically. The linked list works fine on its own but I need to be able to insert a record of a person as the struct and list shows in the code but also alphabetically sort them so when I display the list it will show in that order. I would need to be able to set the head of the linked list if a person's name was to come before alphabetically the original head of the list. I have looked at other linked list posts but they do not help my problem in depth.
For starters the function main shall be declared like
Using the function
toupperfor an object of the type int that gets an integerdoes not make sense.
The call of
scanfwithin the function can be written simplerThe second argument of this call of
scanfshall be written like
Or it is better even to write
The if-else statement in main after the case label
1:can be written the following way