Linked lists and insertion sort

J

Jochus

Hi!

Today we saw the information about lists. We have an assignment about CGI.
That's all going well, but we're stuck at the insertion sorft of linked
lists

--
void voeg_in_lijst(const char* padname, int matches, match *& bestanden) {

match *hulp;
hulp=new match;
strcpy(hulp->url, padname);
hulp->aantal=matches;

if(bestanden==0 || bestanden->aantal<matches){
if (bestanden==0){
bestanden=hulp;
bestanden->vlg=0;
}
else
{
match *temp;
temp=bestanden;
bestanden=hulp;
bestanden->vlg=temp;

}
}
else{
match *schuiver=bestanden->vlg;
while(schuiver!=0 && schuiver->aantal>hulp->aantal){
schuiver=schuiver->vlg;
}
if (schuiver!=0){ // doe insertion
match *extra=schuiver->vlg;
schuiver->vlg=hulp;
hulp->vlg=extra;
}
}
}
--

voeg_in_lijst is dutch for add_in_list. padname is the name of "file" we
need to add in the list, matches is the frequency a word comes forward en
match *bestanden is a list.

So what do we do ...
We check all the lists untill we found an element that is smaller then our
matches.
And then we add in the list.

But that doesn't work ... We have errors. Something about the memory I
assume.

The problem is, we (3 persons) are searching for the fault for 2 days and
can't find the fault. And it must be something stupid!

But I think it's difficult to help, as I can't explain/translate the
assignment ... maybe you can view the whole cpp file:
http://members.lycos.co.uk/jochus/inwe/2ekan/informatica/Prog Proj/CGI/cgi.cpp
-> header:
http://members.lycos.co.uk/jochus/inwe/2ekan/informatica/Prog Proj/CGI/cgi.h
 
R

Rolf Magnus

Jochus said:
Hi!

Today we saw the information about lists. We have an assignment about CGI.
That's all going well, but we're stuck at the insertion sorft of linked
lists

--
void voeg_in_lijst(const char* padname, int matches, match *& bestanden) {

match *hulp;
hulp=new match;
strcpy(hulp->url, padname);

hulp->url is an uninitialized pointer at that point. It must point to some
valid memory big enough to hold the string that padname points to, before
you can use it as target for strcpy.
hulp->aantal=matches;

if(bestanden==0 || bestanden->aantal<matches){
if (bestanden==0){
bestanden=hulp;
bestanden->vlg=0;
}
else
{
match *temp;
temp=bestanden;
bestanden=hulp;
bestanden->vlg=temp;

}
}
else{
match *schuiver=bestanden->vlg;
while(schuiver!=0 && schuiver->aantal>hulp->aantal){
schuiver=schuiver->vlg;
}
if (schuiver!=0){ // doe insertion
match *extra=schuiver->vlg;
schuiver->vlg=hulp;
hulp->vlg=extra;
}
}
}

I can't see any other error ATM.
 
J

Jochus

"Rolf Magnus" schreef in bericht:
hulp->url is an uninitialized pointer at that point. It must point to some
valid memory big enough to hold the string that padname points to, before
you can use it as target for strcpy.

Thnx! We forgot to create the url (char) with new! :-$ ...

But we're still getting Segmentation Errors ... :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top