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);
} 5 4239
On Mon, 22 Dec 2003 05:24:50 UTC, "CR" <fa*******@adelphia.net> wrote: int AddSortFirst(LinkedList *pList, data e)
It's a bad idea to copy the whole struct to a local parameter. Use a
pointer instead.
{ NODE * pNodeCurrent; NODE * pNodeNew; NODE * pNodePrevious;
pNodeNew = (NODE *)malloc(sizeof(NODE));
Creats undefined behavior as you've forgotten to #include <stdlib.h>
and you tells the compiler that it has to comvert int (as it things
now malloc returns) to a pointer type.
Never cast the return type of a function that returns a pointer to
void! The only you reashes by the cast is undefined behavior because
int is not a pointer.
if (pNodeNew == NULL) return(0);
pNodeNew->pNodeNext = NULL; pNodeNew->e = e;
This copies the data another time.
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 */
I can't find the destination of the goto. Using goto is BAD!
Another thing is that you has created now a memory leak. You've
allocated a block of memory but forgets its address.
I stop here because too important design errors found yet.
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);
}
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
The Real OS/2 Guy <os****@pc-rosenau.de> spoke thus: I can't find the destination of the goto. Using goto is BAD!
Well, it's there - just totally unnecessary, since this is all it
is... skipname: /* SKIP THE FILE IF DUPLICATES ARE FOUND */ return(0); }
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
On Mon, 22 Dec 2003 15:02:50 UTC, Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote: The Real OS/2 Guy <os****@pc-rosenau.de> spoke thus:
I can't find the destination of the goto. Using goto is BAD!
Well, it's there - just totally unnecessary, since this is all it is...
skipname: /* SKIP THE FILE IF DUPLICATES ARE FOUND */ return(0); }
As said already. Code faild in design by using goto and producing
memory leaks. Code needs redesigned and written from scratch based on
new design.
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
"CR" <fa*******@adelphia.net> wrote in message
news:Ce********************@news1.news.adelphia.ne t... Any suggestions or improvements would be greatly appreciated.
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);
}
Mercy...
Consider...
int AddSortFirst (LinkedList *pList, data e) {
NODE **npp = &pList->head, *np = 0;
while (*npp) {
int i = strcmp ((*npp)->e.firstName, e.firstName);
if (!i) /* Duplicate? */
return 0; /* Bail! */
if (i > 0) {
np = *npp;
break;
}
npp = &(*npp)->pNodeNext;
}
*npp = malloc (sizeof **npp);
if (!*npp)
return 0;
(*npp)->e = e;
(*npp)->pNodeNext = np;
return 1;
}
Others have wondered why you don't use a pointer rather than pass a copy of
the whole struct to the function? I wonder whether you need a deep(er) copy
than that provided by the simple assignment....
pNodeNew->e = e; (yours)
.. . . or . . .
(*npp)->e = e; (mine)
Regards
Brian
"Brian MacBride" <ma******@ix.netcom.com> wrote in message
news:bs************@ID-26770.news.uni-berlin.de... "CR" <fa*******@adelphia.net> wrote in message news:Ce********************@news1.news.adelphia.ne t... Any suggestions or improvements would be greatly appreciated.
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);
}
Mercy...
Consider...
int AddSortFirst (LinkedList *pList, data e) { NODE **npp = &pList->head, *np = 0;
while (*npp) { int i = strcmp ((*npp)->e.firstName, e.firstName); if (!i) /* Duplicate? */ return 0; /* Bail! */
if (i > 0) { np = *npp; break; } npp = &(*npp)->pNodeNext; }
*npp = malloc (sizeof **npp); if (!*npp) return 0; (*npp)->e = e; (*npp)->pNodeNext = np;
return 1; }
Others have wondered why you don't use a pointer rather than pass a copy
of the whole struct to the function? I wonder whether you need a deep(er)
copy than that provided by the simple assignment....
pNodeNew->e = e; (yours)
. . . or . . .
(*npp)->e = e; (mine)
Oops... a mental lapse (or too much EggNog?)
Consider...
int AddSortFirst (LinkedList *pList, data e) {
NODE **npp = &pList->head, *np;
while (*npp) {
int i = strcmp ((*npp)->e.firstName, e.firstName);
if (!i) /* Duplicate? */
return 0; /* Bail! */
if (i > 0)
break;
npp = &(*npp)->pNodeNext;
}
np = malloc (sizeof *np);
if (!np)
return 0;
np->pNodeNext = *npp;
np->e = e;
*npp = np;
return 1;
}
Regards
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jeff Relf |
last post by:
Hi All,
I plan on using the following C++ code
to create nodes with unlimited children:
// I would like to declare NodeT like this,
// but it won't compile because Lnk_T is not defined yet....
|
by: lechequier |
last post by:
Let's say I define a list of pairs as follows:
>>l =
Can anyone explain why this does not work?
>>h = {}.update(l)
and instead I have to go:
>>h = {}
>>h.update(l)
to initialize a...
|
by: PRadyut |
last post by:
In this code i tried to add the elements in ascending order
but the output is only
0
1
2
the rest of the elements are not shown. the code...
|
by: gwainguard |
last post by:
Hello I am studying the ECMA specs and was doing wonderfully until just
now. They have just broached the topic of AMM and given some example
code which seems to be demanding a greater understanding...
|
by: william |
last post by:
When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node *...
|
by: subramanian100in |
last post by:
This is not a homework assignment question.
Kindly excuse me for posting this question here.
Suppose a BST contains 100,000 ndoes.
Suppose I have to traverse the BST by Preorder, say, without...
|
by: twillie |
last post by:
Hello,
I have a linked list in C++ and I am trying to remove all duplicate nodes from the list. The code below is what our prof. wrote on the board in pusdo code in class, yet it doesn't seem to...
|
by: junky_fellow |
last post by:
Hi,
Is there any efficient way of finding the intesection point of two
singly linked lists ?
I mean to say that, there are two singly linked lists and they meet at
some point. I want to find...
|
by: phiefer3 |
last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs.
First of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |