By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

help with C

P: n/a
I need to insert and sort in a link list

Nov 20 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
sulays wrote:
I need to insert and sort in a link list


how far have you got so far with your problem? Could you show us the
code you
have written so far or explain what you have are having difficulties
with?
--
Nick Keighley

Nov 20 '05 #2

P: n/a
in getting an infinite loop dont know why?

this is in main
while(!feof(fpA))
{
readingaemployeedata(&fpA,lastn,firstn,midini,&emp id,title,balance);
if(feof(fpA))
break;
tempPtr =
creatingaemployeedata(lastn,firstn,midini,empid,ti tle,balance);
while(tempPtr != NULL)
{
prinheader();
firstemployeePtr = inserting(firstemployeePtr,tempPtr);
printing(firstemployeePtr);
}

function

EIS* inserting(EIS *fPtr,EIS *tempPtr)
{
EIS *prev = NULL;
EIS *curr = NULL;

if(fPtr == NULL)
return tempPtr;

curr = prev = fPtr;

while(curr != NULL)
{
if(strcmp(tempPtr->lastname,curr->lastname) > ZEROI)
{
tempPtr->nextnode = curr;
if(curr == prev)
return tempPtr;
else
prev->nextnode = tempPtr;
return fPtr;
}
else
{
prev = curr;
curr = curr->nextnode;
}
}
prev->nextnode = tempPtr;
return fPtr;
}
thanks for replying to me

Nov 20 '05 #3

P: n/a
if I use **fPtr insted of *fPtr I get a seg fault

Nov 20 '05 #4

P: n/a
"sulays" <ss**********@yahoo.com> writes:
I need to insert and sort in a link list


Ok, well, good luck with that.

Don't expect much help unless you post an actual question and make
some effort to do it yourself.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 20 '05 #5

P: n/a
please leave some context in your post (as I have)

in general when posting to comp.lang.c you should post a short
*complete* example
that illustrates your problem. I am left trying to gues what the
missing bits of your
program are.
sulays wrote:
in getting an infinite loop dont know why?
where is it looping? The easy way to answer this question is to use a
debugger or
put diagnostic print statements in your program.
this is in main
while(!feof(fpA))
bad idea

the usual C idiom is
while (fgets() != NULL)

lookup fgets() in your textbook for its parameters and semantics

{
readingaemployeedata(&fpA,lastn,firstn,midini,&emp id,title,balance);
some comments would be nice...
if(feof(fpA))
break;
tempPtr =
creatingaemployeedata(lastn,firstn,midini,empid,ti tle,balance);
while(tempPtr != NULL)
I havn't fully analysed your program (I can't because you didn't post
all of it...) but
this loop only exits if tempPtr is NULL. Your loop never modifies
tempPtr. I'm
assuming tempPtr isn't a global.
{
add a diagnostic to confirm this
printf ("tempPtr=%p\n", (void*)tempPtr);
prinheader();
firstemployeePtr = inserting(firstemployeePtr,tempPtr);
printing(firstemployeePtr);
}

function

EIS* inserting(EIS *fPtr,EIS *tempPtr)
{
EIS *prev = NULL;
EIS *curr = NULL;

if(fPtr == NULL)
return tempPtr;

curr = prev = fPtr;
I think it would help if you explained what you are trying to do here.
It would probably
help you as well.

while(curr != NULL)
{
if(strcmp(tempPtr->lastname,curr->lastname) > ZEROI)
{
tempPtr->nextnode = curr;
if(curr == prev)
return tempPtr;
else
prev->nextnode = tempPtr;
return fPtr;
}
else
{
prev = curr;
curr = curr->nextnode;
}
}
prev->nextnode = tempPtr;
return fPtr;
}
thanks for replying to me

--
Nick Keighley

Testing can show the presense of bugs, but not their absence.
-- Dijkstra

Nov 20 '05 #6

P: n/a
sulays wrote:

if I use **fPtr insted of *fPtr I get a seg fault


Post something that I can compile.
If you feel that your program is too large to post,
create a smaller version that exhibits the same problem.

--
pete
Nov 20 '05 #7

P: n/a
Im trying create a link list by accepting initial input from a file
<that part of my program is working ok>
the link list must be maintained in increasing alphabetical order base
on the employee's last name
employees with the same last name I need to listed in alphabetical
order based on the first name and if the have the same last name and
first name need to listed in ascending order based on employee id

trying to do the last name for now and see how it works

EIS* inserting(EIS *fPtr,EIS *tempPtr)
{
EIS *prev = NULL;
EIS *curr = NULL;

if(fPtr == NULL)< this is checking if the list is empty>
return tempPtr;

curr = prev = fPtr;<this is setting the other pointers to the first
node>
tempPtr->nextnode = curr; <this one is for the second node>
while(curr != NULL)
{
if(strcmp(tempPtr->lastname,curr->lastname) > ZEROI)
{
if(curr == prev)
return tempPtr;
else
prev->nextnode = tempPtr;
return fPtr;
}
else
{
prev = curr;
curr = curr->nextnode;
}
}
prev->nextnode = tempPtr;
return fPtr;
}

I dont know why is not working

Nov 20 '05 #8

P: n/a
yes is long and I dont know how to make small version think that
everything is necesary for the program but if you want I can
post the whole thing

Nov 20 '05 #9

P: n/a
LEAVE SOME CONTEXT IN YOUR POSTS!!

that is, please leave in the text of the part of the message you are
replying to.
My replies to your posts do this.

sulays wrote:
yes is long and I dont know how to make small version think that
everything is necesary for the program but if you want I can
post the whole thing


try a simpler problem. Try just building and printing a linked list for
instance.
int main (void)
{
List list;

insert (&list, "red"); /* add an item to the end of the list*/
insert (&list, "blue");
insert (&list, "green");

print (&list);

return 0;
}

this is the way to write a complicated program. Break it into simpler
steps
and solve the steps. When you have a simple program working then add
an extra feature. For instance you could modify insert to insert in the
correct
order,
--
Nick Keighley

Nov 20 '05 #10

P: n/a
like this? I dont know what you want i need hel with the inserting, is
only for last name for now but I need first name and mid ini and id
now i'm getting the first input to print but it stop
int main()
{
List list;
List temp;

while(!feof(fpA))
{
readingaemployeedata(fpA);
if(feof(fpA))
break;
temp = creatingaemployeedata();
while(temp != NULL)
{
list = inserting(list,temp);
printing(list);
}
setptrzero(list);
return 0;
}
}

List* inserting(EIS *list,EIS *temp)
{
List *prev = NULL;
List *curr = NULL;

if(list == NULL)
{
return temp;
}
else
{
curr = prev = list;
temp->next = curr;

while(curr != NULL)
{
if(strcmp(temp->lastname,curr->lastname) > ZEROI)
{
if(curr == prev)
return temp;
else
prev->next = temp;
return list;
}
else
{
prev = curr;
curr = curr->next;
}
}
prev->next = temp;
return list;
}
}

Nov 20 '05 #11

P: n/a
"sulays" <ss**********@yahoo.com> writes:
I need to insert and sort in a link list


I'd recommend a merge sort.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Nov 20 '05 #12

P: n/a
how do i do that?

Nov 20 '05 #13

P: n/a
sulays wrote
(in article
<11*********************@z14g2000cwz.googlegroups. com>):
how do i do that?


Hint 1: Continuing to snip all relevant context from your posts
is going to piss everyone off.

Hint 2: Continuing to demonstrate zero self-effort at solving
your problems is going to piss everyone off.

Hint 3: We don't do your homework for you here, unless you post
contact information for your professor so we can contact him
directly with a solution.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 20 '05 #14

P: n/a
I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway

Nov 20 '05 #15

P: n/a
On 20 Nov 2005 11:50:52 -0800, in comp.lang.c , "sulays"
<ss**********@yahoo.com> wrote:
I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway


Didn't someone just say to you:

Hint 1: Continuing to snip all relevant context from your posts
is going to piss everyone off.

Hint 2: Continuing to demonstrate zero self-effort at solving
your problems is going to piss everyone off.

Hint 3: We don't do your homework for you here, unless you post
contact information for your professor so we can contact him
directly with a solution.

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 20 '05 #16

P: n/a
sulays said:
like this? I dont know what you want i need hel with the inserting, is
only for last name for now but I need first name and mid ini and id
now i'm getting the first input to print but it stop


Thanks for showing your code. I compiled the code you posted, and here's the
compiler's output for you to study:

gcc -W -Wall -ansi -pedantic -O2 -g -pg -c -o foo.o foo.c
foo.c: In function `main':
foo.c:3: `List' undeclared (first use in this function)
foo.c:3: (Each undeclared identifier is reported only once
foo.c:3: for each function it appears in.)
foo.c:3: parse error before `list'
foo.c:6: warning: implicit declaration of function `feof'
foo.c:6: `fpA' undeclared (first use in this function)
foo.c:8: warning: implicit declaration of function `readingaemployeedata'
foo.c:11: `temp' undeclared (first use in this function)
foo.c:11: warning: implicit declaration of function `creatingaemployeedata'
foo.c:12: `NULL' undeclared (first use in this function)
foo.c:14: `list' undeclared (first use in this function)
foo.c:14: warning: implicit declaration of function `inserting'
foo.c:15: warning: implicit declaration of function `printing'
foo.c:17: warning: implicit declaration of function `setptrzero'
foo.c:20: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:22: parse error before `*'
foo.c:22: parse error before `*'
foo.c:23: warning: return-type defaults to `int'
foo.c:23: warning: type mismatch with previous implicit declaration
foo.c:14: warning: previous implicit declaration of `inserting'
foo.c:23: warning: `inserting' was previously implicitly declared to return
`int'
foo.c: In function `inserting':
foo.c:24: `List' undeclared (first use in this function)
foo.c:24: `prev' undeclared (first use in this function)
foo.c:24: `NULL' undeclared (first use in this function)
foo.c:25: `curr' undeclared (first use in this function)
foo.c:27: `list' undeclared (first use in this function)
foo.c:29: `temp' undeclared (first use in this function)
foo.c:38: `ZEROI' undeclared (first use in this function)
foo.c:55: warning: control reaches end of non-void function
make: *** [foo.o] Error 1

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 20 '05 #17

P: n/a
thanks

Nov 20 '05 #18

P: n/a
sulays wrote
(in article
<11**********************@g49g2000cwa.googlegroups .com>):
I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway


Hint 4: Replace your ROM with RAM.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 20 '05 #19

P: n/a
"sulays" <ss**********@yahoo.com> writes:
I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway


If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

This will automatically quote the previous message and add an
attribution line. The result will be similar to this followup, where
I've quoted what you wrote and indicated that you were the one who
wrote it.

We can't necessarily see the article to which you're replying. Each
followup needs to provide enough context so it can be read on its own.
(That doesn't mean you need to quote the entire article, just enough
for your followup to make sense.)

We have tried to tell you this several times, and you have
consistently failed to follow our advice. Please correct this;
otherwise, we'll probably give up on trying to help you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 21 '05 #20

P: n/a
Keith Thompson said:
We have tried to tell you this several times, and you have
consistently failed to follow our advice. Please correct this;
otherwise, we'll probably give up on trying to help you.


Too late.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 21 '05 #21

P: n/a
sulays wrote:
I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway

one last try...

You have been given several items of advice by myself and other people
and seem incapable of following simple advice. Please read this post
carefully and try to follow the advice I give,

1. always leave some context. This means you quote the post (or parts
of it) you are replying to. By this time I have no idea what program
you
are talking about.

2. if posting code you have a problem with then post SHORT,
COMPLETE examples.The code should compile. You have consistently
ommitted important data structures and functions from your posts.

3. if your code is too long to post then post a shorter version that
illustrates your problem. For instance I suggested you write a simpler
program that built and printed a list. We would then know if you
understood the basics of linkedlists and were capable of writing a
simple program.

4. you posted code saying it had an infinite loop. I suggested you
added diagnostic code. I suggested a possible problem. Did this solve
your problem? If so why are you still posting?

5. the code you posted lacks comments and uses poor choices in the
selection of identifier names. I would give examples but you snipped
the context...

I begin to wonder if you have some of the desirable charcter traits
that will enable you to program successfully. You need to listen when
people explain things. You need to break complex problems into
simpler ones. You need to think clearly. You need to communicate
clearly. Learn these things and you will become a better person.

In a sense we should be grateful to you for illustrating in multiple
ways
why leaving context in your post is a good idea.
--
Nick Keighley (Before Coffee)

Nov 21 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.