Skip to content

Instantly share code, notes, and snippets.

@fakedrake
Created March 19, 2013 16:13
Show Gist options
  • Save fakedrake/5197473 to your computer and use it in GitHub Desktop.
Save fakedrake/5197473 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <malloc.h>
#include <assert.h>
#define STRING_SIZE 20
#define STUDENT_NUMBER 100000
struct Student {
char lastName[STRING_SIZE];
char firstName[STRING_SIZE];
};
typedef struct Student* StudentTypePtr;
struct ClassNode {
StudentTypePtr aStudent;
struct ClassNode* next;
};
typedef struct ClassNode* ClassNodePtr;
void insert(ClassNodePtr *sPtr, StudentTypePtr value)
{
ClassNodePtr newPtr;
ClassNodePtr previousPtr;
ClassNodePtr currentPtr;
newPtr = (ClassNodePtr)malloc(sizeof(struct ClassNode));
if(newPtr!=NULL){
newPtr->aStudent = value;
newPtr->next = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while(currentPtr != NULL && (strcmp( (void *)currentPtr->aStudent->lastName, (void *)value->lastName) == -1 ||
(strcmp( (void *)currentPtr->aStudent->lastName, (void *)value->lastName) == 0 &&
strcmp( (void *)currentPtr->aStudent->firstName, (void *)value->firstName) == -1))){
previousPtr=currentPtr;
currentPtr=currentPtr->next;
}
if(previousPtr==NULL){
newPtr->next = *sPtr;
*sPtr = newPtr;
} else {
previousPtr->next = newPtr;
newPtr->next = currentPtr;
}
} else {
printf("No memory available");
}
}
void init(StudentTypePtr student) {
strcpy(student->firstName, "Chris");
strcpy(student->lastName, "Griffin");
}
int main(int argc, char *argv[])
{
StudentTypePtr students = (StudentTypePtr)malloc(sizeof(struct Student) * STUDENT_NUMBER);
ClassNodePtr list = NULL, currentPtr;
int i, counter = 0;
for (i=0; i < STUDENT_NUMBER; i++) {
init(students+i);
insert(&list, students+i);
}
/* Test the values */
for (currentPtr = list; currentPtr; currentPtr = currentPtr->next){
counter++;
assert(strcmp(currentPtr->aStudent->lastName, "Griffin") == 0);
assert(strcmp(currentPtr->aStudent->firstName, "Chris") == 0);
}
assert(counter == STUDENT_NUMBER);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment