Created
March 19, 2013 16:54
-
-
Save fakedrake/5197852 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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define STR_SIZE 100 | |
struct studentRecord { | |
char courseName[STR_SIZE]; | |
float Mark; | |
struct studentRecord *next; | |
} ; | |
typedef struct studentRecord StudentRecord; | |
typedef StudentRecord * StudentRecordPtr; | |
struct studentType { | |
char firstName[STR_SIZE]; | |
char lastName[STR_SIZE]; | |
int idNumber; | |
StudentRecordPtr courses; | |
} ; | |
typedef struct studentType StudentType; | |
typedef StudentType *StudentTypePtr; | |
struct classNode { | |
StudentTypePtr aStudent; | |
struct classNode *next; | |
} ; | |
typedef struct classNode ClassNode; | |
typedef ClassNode* ClassNodePtr; | |
void insert(ClassNodePtr *sPtr, StudentTypePtr student); | |
void rinsert(StudentRecordPtr *sPtr, StudentTypePtr student); | |
void average(StudentTypePtr *sPtr, int n, int o); | |
void quickSort( float a[], int l, int r); | |
void deleter(ClassNodePtr *sPtr, StudentTypePtr student); | |
int partition( float a[], int l, int r); | |
int main() | |
{ | |
int nos,apm,noc,i,j; | |
ClassNodePtr startPtr=NULL; | |
nos = 2; | |
printf("Num of students: %d\n",nos); | |
apm = 1; | |
printf("Necessary courses: %d\n",apm); | |
for(i=0;i<nos;i++){ | |
StudentRecordPtr startRPtr=NULL; | |
StudentTypePtr student; | |
student=malloc(sizeof(StudentType)); | |
strcpy(student->firstName, "Chris"); | |
printf("firstName: %s\n",student->firstName); | |
strcpy(student->lastName, "Griffin"); | |
printf("lastName: %s\n",student->lastName); | |
student->idNumber = 2273; | |
printf("idNumber: %d\n", student->idNumber); | |
noc = 2; | |
printf("Number of courses: %d\n",noc); | |
for(j=0;j<noc;j++){ | |
rinsert(&startRPtr, student);} | |
insert(&startPtr, student); | |
} | |
system("PAUSE"); | |
return 0; | |
} | |
void insert(ClassNodePtr *sPtr, StudentTypePtr value) | |
{ | |
ClassNodePtr newPtr; | |
ClassNodePtr previousPtr; | |
ClassNodePtr currentPtr; | |
newPtr=malloc(sizeof(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 rinsert(StudentRecordPtr *sPtr, StudentTypePtr value) | |
{ | |
int i=1; | |
StudentRecordPtr newPtr; | |
newPtr=malloc(sizeof(StudentRecord)); | |
strcpy(newPtr->courseName, "Snot Consuming"); | |
printf("courseName: %s\n",newPtr->courseName); | |
newPtr->Mark = 5; | |
printf("Mark: %f\n",newPtr->Mark); | |
while(*sPtr!=NULL){ | |
if(strcmp((void*) (*sPtr)->courseName , (void*) newPtr->courseName)==0){ | |
if((*sPtr)->Mark < newPtr->Mark){ | |
(*sPtr)->Mark = newPtr->Mark;} | |
i=0; | |
break; | |
} | |
*sPtr=(*sPtr)->next; | |
if(i){ | |
*sPtr=newPtr; | |
} | |
} | |
} | |
void average(StudentTypePtr *sPtr, int n, int o) | |
{ | |
float list[n],sum=0,antisum=0; | |
int i; | |
StudentRecordPtr newPtr; | |
newPtr = (*sPtr)->courses; | |
for(i=0;i<n;i++) | |
list[i]=newPtr->Mark; | |
quickSort(list,0,n-1); | |
if(list[o-1]>=5){ | |
for(i=0;i<o;i++){ | |
sum+=list[i]; | |
} | |
} | |
else{ | |
for(i=0;i<o-i;i++){ | |
if(list[i]<5) | |
antisum++; | |
} | |
} | |
} | |
void deleter(ClassNodePtr *sPtr, StudentTypePtr student) | |
{ | |
ClassNodePtr previousPtr; | |
ClassNodePtr currentPtr; | |
ClassNodePtr tempPtr; | |
if(student->lastName==(*sPtr)->aStudent->lastName | |
&& student->firstName==(*sPtr)->aStudent->firstName){ | |
tempPtr=*sPtr; | |
*sPtr=(*sPtr)->next; | |
free(tempPtr); | |
} | |
else{ | |
previousPtr=*sPtr; | |
currentPtr=(*sPtr)->next; | |
while(currentPtr!=NULL && currentPtr->aStudent->firstName!=student->firstName | |
&& currentPtr->aStudent->lastName!=student->lastName){ | |
previousPtr=currentPtr; | |
currentPtr=currentPtr->next; | |
} | |
if(currentPtr!=NULL){ | |
tempPtr=currentPtr; | |
previousPtr->next=currentPtr->next; | |
free(tempPtr); | |
} | |
} | |
} | |
void quickSort( float a[], int l, int r) | |
{ | |
int j; | |
if( l < r ) | |
{ | |
j = partition( a, l, r); | |
quickSort( a, l, j-1); | |
quickSort( a, j+1, r); | |
} | |
} | |
int partition( float a[], int l, int r) { | |
int pivot, i, j, t; | |
pivot = a[l]; | |
i = l; j = r+1; | |
while( 1) | |
{ | |
do ++i; while( a[i] <= pivot && i <= r ); | |
do --j; while( a[j] > pivot ); | |
if( i >= j ) break; | |
t = a[i]; a[i] = a[j]; a[j] = t; | |
} | |
t = a[l]; a[l] = a[j]; a[j] = t; | |
return j; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment