Linked lists and insertion sort

Discussion in 'C++' started by Jochus, Apr 19, 2005.

  1. Jochus

    Jochus Guest

    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
     
    Jochus, Apr 19, 2005
    #1
    1. Advertising

  2. Jochus

    Rolf Magnus Guest

    Jochus wrote:

    > 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.
     
    Rolf Magnus, Apr 19, 2005
    #2
    1. Advertising

  3. Jochus

    Jochus Guest

    "Rolf Magnus" schreef in bericht:

    Jochus wrote:
    >
    >> 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.


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

    But we're still getting Segmentation Errors ... :-(
     
    Jochus, Apr 20, 2005
    #3
  4. Jochus

    Jochus Guest

    Problem solved: we were deleting too much :) ...
     
    Jochus, Apr 21, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    502
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    493
    emerth
    Jul 10, 2003
  3. Java Newbie

    Insertion Sort on a linked list

    Java Newbie, Feb 4, 2007, in forum: Java
    Replies:
    2
    Views:
    3,412
    Mark Space
    Feb 9, 2007
  4. Julia
    Replies:
    6
    Views:
    613
    Dmitri Sologoubenko
    Aug 11, 2006
  5. jawdoc
    Replies:
    9
    Views:
    788
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page