C
CR
I've been to figure out how to get AddSortLast function to add nodes in
acsending order (a b c d)
I figured it out how to get it to sort in decending order but I can't get it
to sort in acsending order by flipping
the < sign to a > sign like I thought I could. Any suggestions or
improvements would be greatly appreciated.
typedef struct /* file data structure */
{
string_f firstName;
string_l lastName;
char gender;
char rate;
int tenure;
float salary;
char raise;
int rec;
} data;
typedef struct node *pointer_t; /* pointer to node */
typedef struct node /* node data structure */
{
data e;
pointer_t pNodeNext;
} NODE;
typedef struct list /* list data structure */
{
pointer_t head;
int rec;
} LinkedList;
int AddSortFirst(LinkedList *pList, data e)
{
NODE * pNodeCurrent;
NODE * pNodeNew;
NODE * pNodePrevious;
pNodeNew = (NODE *)malloc(sizeof(NODE));
if (pNodeNew == NULL)
return(0);
pNodeNew->pNodeNext = NULL;
pNodeNew->e = e;
pNodePrevious = NULL;
pNodeCurrent = pList->head;
/* CHECK LIST FOR DUPLICATES */
while(pNodeCurrent != NULL)
{
if((strcmp(pNodeNew->e.firstName,
pNodeCurrent->e.firstName))==0)
goto skipname; /* SKIP THIS NAME IT ALREADY EXISTS IN LIST
*/
pNodeCurrent = pNodeCurrent->pNodeNext;
}
/* REINITIALIZE pNodeCurrent TO START OF LINKED LIST */
pNodeCurrent = pList->head;
while ((pNodeCurrent != NULL) && (strcmp(pNodeNew->e.firstName,
pNodeCurrent->e.firstName))>0)
{
pNodePrevious = pNodeCurrent;
pNodeCurrent = pNodeCurrent->pNodeNext;
}
if (pNodePrevious != NULL)
{
pNodeNew->pNodeNext = pNodePrevious->pNodeNext;
pNodePrevious->pNodeNext = pNodeNew;
}
else
{
pNodeNew->pNodeNext = pList->head;
pList->head = pNodeNew;
}
return(1);
skipname: /* SKIP THE FILE IF DUPLICATES ARE FOUND */
return(0);
}
int AddSortLast(LinkedList *pList, data e)
{
NODE * pNodeCurrent;
NODE * pNodeNew;
NODE * pNodePrevious;
pNodeNew = (NODE *)malloc(sizeof(NODE));
if (pNodeNew == NULL)
return(0);
pNodeNew->pNodeNext = NULL;
pNodeNew->e = e;
pNodePrevious = NULL;
pNodeCurrent = pList->head;
while(pNodeCurrent != NULL)
{
if((strcmp(pNodeNew->e.firstName,
pNodeCurrent->e.firstName))==0)
goto skipname; /* SKIP THIS NAME IT ALREADY EXISTS */
pNodeCurrent = pNodeCurrent->pNodeNext;
}
pNodeCurrent = pList->head;
/* WHAT AM I MISSING IN ORDER TO SORT IN ASCENDING ORDER LIKE IN
AddSortFirst() ? */
while ((pNodeCurrent != NULL) && (strcmp(pNodeNew->e.lastName,
pNodeCurrent->e.lastName))<0)
{
pNodePrevious = pNodeCurrent;
pNodeCurrent = pNodeCurrent->pNodeNext;
}
if (pNodePrevious != NULL)
{
pNodeNew->pNodeNext = pNodePrevious->pNodeNext;
pNodePrevious->pNodeNext = pNodeNew;
}
else
{
pNodeNew->pNodeNext = pList->head;
pList->head = pNodeNew;
}
return(1);
skipname:
return(0);
}
acsending order (a b c d)
I figured it out how to get it to sort in decending order but I can't get it
to sort in acsending order by flipping
the < sign to a > sign like I thought I could. Any suggestions or
improvements would be greatly appreciated.
typedef struct /* file data structure */
{
string_f firstName;
string_l lastName;
char gender;
char rate;
int tenure;
float salary;
char raise;
int rec;
} data;
typedef struct node *pointer_t; /* pointer to node */
typedef struct node /* node data structure */
{
data e;
pointer_t pNodeNext;
} NODE;
typedef struct list /* list data structure */
{
pointer_t head;
int rec;
} LinkedList;
int AddSortFirst(LinkedList *pList, data e)
{
NODE * pNodeCurrent;
NODE * pNodeNew;
NODE * pNodePrevious;
pNodeNew = (NODE *)malloc(sizeof(NODE));
if (pNodeNew == NULL)
return(0);
pNodeNew->pNodeNext = NULL;
pNodeNew->e = e;
pNodePrevious = NULL;
pNodeCurrent = pList->head;
/* CHECK LIST FOR DUPLICATES */
while(pNodeCurrent != NULL)
{
if((strcmp(pNodeNew->e.firstName,
pNodeCurrent->e.firstName))==0)
goto skipname; /* SKIP THIS NAME IT ALREADY EXISTS IN LIST
*/
pNodeCurrent = pNodeCurrent->pNodeNext;
}
/* REINITIALIZE pNodeCurrent TO START OF LINKED LIST */
pNodeCurrent = pList->head;
while ((pNodeCurrent != NULL) && (strcmp(pNodeNew->e.firstName,
pNodeCurrent->e.firstName))>0)
{
pNodePrevious = pNodeCurrent;
pNodeCurrent = pNodeCurrent->pNodeNext;
}
if (pNodePrevious != NULL)
{
pNodeNew->pNodeNext = pNodePrevious->pNodeNext;
pNodePrevious->pNodeNext = pNodeNew;
}
else
{
pNodeNew->pNodeNext = pList->head;
pList->head = pNodeNew;
}
return(1);
skipname: /* SKIP THE FILE IF DUPLICATES ARE FOUND */
return(0);
}
int AddSortLast(LinkedList *pList, data e)
{
NODE * pNodeCurrent;
NODE * pNodeNew;
NODE * pNodePrevious;
pNodeNew = (NODE *)malloc(sizeof(NODE));
if (pNodeNew == NULL)
return(0);
pNodeNew->pNodeNext = NULL;
pNodeNew->e = e;
pNodePrevious = NULL;
pNodeCurrent = pList->head;
while(pNodeCurrent != NULL)
{
if((strcmp(pNodeNew->e.firstName,
pNodeCurrent->e.firstName))==0)
goto skipname; /* SKIP THIS NAME IT ALREADY EXISTS */
pNodeCurrent = pNodeCurrent->pNodeNext;
}
pNodeCurrent = pList->head;
/* WHAT AM I MISSING IN ORDER TO SORT IN ASCENDING ORDER LIKE IN
AddSortFirst() ? */
while ((pNodeCurrent != NULL) && (strcmp(pNodeNew->e.lastName,
pNodeCurrent->e.lastName))<0)
{
pNodePrevious = pNodeCurrent;
pNodeCurrent = pNodeCurrent->pNodeNext;
}
if (pNodePrevious != NULL)
{
pNodeNew->pNodeNext = pNodePrevious->pNodeNext;
pNodePrevious->pNodeNext = pNodeNew;
}
else
{
pNodeNew->pNodeNext = pList->head;
pList->head = pNodeNew;
}
return(1);
skipname:
return(0);
}