I want to take my current prorgam and take the structure and create a sorted list.
When a user enters a name and age i want to put that at the head then the next time a name and age is added i want to sort the list by name and age, hence the layout of my structure:
Code:
struct node
{
char *name;
int age;
struct node *nextName;
struct node *nextAge;
};
I'm having trouble understanding how to create the list. I know i need a pointer to act as the root of the list. i'm just very confused on how to implement this.
As for the sorting i figure i'll just use the return value of stringcmp() to sort the names.
here is my original program where the information is successfully stored and can be found and when the remove function is called it finds the correct record.
BELOW the first hunk of code is my program re-written to incorporate a sorted list.
i was wondering if someone could tell me if i'm on the right track with the pointers adn so on. I'm just having a hard time understanding lists.
Thanks in advance, ALain
ps. there's a bunch of little things missing such as free() the memory, that will come when i have completed the hard stuff.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 250
struct node
{
char *name;
int age;
struct node *nextName;
struct node *nextAge;
};
typedef struct node NODE;
int drain_stdin()
{
int c;
while((c = getchar()) != '\n' && c != EOF );
return c;
}
void addStruct(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF)
{
printf("Enter name: ");
fgets(ptr->name, MAX_LEN + 1, stdin);
printf("Enter age: ");
scanf("%d", &ptr->age);
}
else
{
printf("An Error Has Occured\n");
}
}
void removeStruct(NODE *root, NODE *ptr)
{
char nameRemove[MAX_LEN + 1];
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF )
{
printf("Enter Name Of Desired Record To Be Removed: ");
fgets(nameRemove, MAX_LEN + 1, stdin);
if(strcmp(nameRemove, ptr->name) == 0)
{
printf("Found The Record\n");
}
else
{
printf("There Is No Record With That Name\n");
}
}
else
{
printf("An Error Has Occured\n");
}
}
void printName(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void printAge(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void progExit(NODE *root, NODE *ptr)
{
}
int main()
{
int choice;
NODE *root, *ptr;
ptr = malloc(sizeof(NODE));
ptr->name = malloc(sizeof(char)*MAX_LEN);
do
{
printf("1. Add structure\n");
printf("2. Remove structure\n");
printf("3. Print names\n");
printf("4. Print ages\n");
printf("5. Exit\n");
scanf("%d", &choice);
if(choice == 1)
{
addStruct(root, ptr);
}
else if(choice == 2)
{
removeStruct(root, ptr);
}
else if(choice == 3)
{
printName(root, ptr);
}
else if(choice == 4)
{
printAge(root, ptr);
}
else if(choice == 5)
{
progExit(root, ptr);
}
else
{
return 0;
}
}while(choice != 5);
return 0;
}
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 250
struct node
{
char *name;
int age;
struct node *nextName;
struct node *nextAge;
};
typedef struct node NODE;
int drain_stdin()
{
int c;
while((c = getchar()) != '\n' && c != EOF );
return c;
}
void addStruct(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF)
{
printf("Enter name: ");
fgets(ptr->name, MAX_LEN + 1, stdin);
printf("Enter age: ");
scanf("%d", &ptr->age);
}
else
{
printf("An Error Has Occured\n");
}
}
void removeStruct(NODE *root, NODE *ptr)
{
char nameRemove[MAX_LEN + 1];
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF )
{
printf("Enter Name Of Desired Record To Be Removed: ");
fgets(nameRemove, MAX_LEN + 1, stdin);
if(strcmp(nameRemove, ptr->name) == 0)
{
printf("Found The Record\n");
}
else
{
printf("There Is No Record With That Name\n");
}
}
else
{
printf("An Error Has Occured\n");
}
}
void printName(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void printAge(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void progExit(NODE *root, NODE *ptr)
{
}
int main()
{
int choice;
NODE *root, *ptr;
ptr = malloc(sizeof(NODE));
ptr->name = malloc(sizeof(char)*MAX_LEN);
do
{
printf("1. Add structure\n");
printf("2. Remove structure\n");
printf("3. Print names\n");
printf("4. Print ages\n");
printf("5. Exit\n");
scanf("%d", &choice);
if(choice == 1)
{
addStruct(root, ptr);
}
else if(choice == 2)
{
removeStruct(root, ptr);
}
else if(choice == 3)
{
printName(root, ptr);
}
else if(choice == 4)
{
printAge(root, ptr);
}
else if(choice == 5)
{
progExit(root, ptr);
}
else
{
return 0;
}
}while(choice != 5);
return 0;
}
Thanks again, ALain