Created
March 19, 2013 16:03
-
-
Save fakedrake/5197363 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <malloc.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; | |
int i; | |
/* Initialize list to the first student. */ | |
for (i=0; i < STUDENT_NUMBER; i++) { | |
init(students+i); | |
insert(&list, students+i); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment