473,236 Members | 1,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 software developers and data experts.

Question: Adding nodes to sorted linked list

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);

}
Nov 14 '05 #1
5 4234
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

Nov 14 '05 #2
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.
Nov 14 '05 #3
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

Nov 14 '05 #4

"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



Nov 14 '05 #5

"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

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
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....
22
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...
2
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...
3
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...
9
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 *...
1
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...
5
twillie
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...
46
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...
2
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.