sorting linked list in alphabetical order HELP!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kylie991
    New Member
    • Feb 2009
    • 41

    sorting linked list in alphabetical order HELP!

    I'm confused as to why this isnt working.. am i doing this right?? I have to create a function that does the following:

    - Insert a word to the linked list so that the list
    // remains sorted alphabetically.
    // PRE: The list is sorted alphabetically or is NULL
    // POST: The resulting list is sorted alphabetically. If the word is
    // in the list already, do nothing.

    struct WordNode {
    string word;
    WordNode *link;
    };

    here is what i have........

    {
    while(!list)
    {
    WordNode *tempNode = new WordNode;
    tempNode->word=word;
    tempNode->link=list;
    list=tempNode;
    }
    // do a deep copy of list to current

    WordNode *current, *lastNode;
    current = new(WordNode);
    current->word = list->word;
    current ->link = NULL;
    lastNode = current;
    list = list -> link;
    while (list!=NULL)
    {
    WordNode *p = new(WordNode);
    p -> word = list -> word;
    p->link = NULL;
    lastNode -> link = p;
    lastNode = p;
    list = list -> link;
    }

    // now sort in alphabetical order

    WordNode *last;
    while(current!= NULL && current->word<word)
    {
    last=current;
    current=current->link;
    }
    WordNode*tempNo de = new WordNode;
    tempNode->word=word;
    //cout << "temp-> word " << tempNode->word << endl;
    last->link=tempNod e;
    tempNode->link=current ;}

    Am I way off track??? it keeps coming up with an error when I try to compile. I can see the problem lies somewhere within

    last->link=tempNode. .......

    ????
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    I don't see a syntax error on that line. To be honest, assuming list and word are defined, the code ought to compile. Can you post the text of the error message you are receiving and confirm the line referenced by the error message?

    Your code seems rather complicated for such a simple task. I think you can reduce it to a single, short loop.

    I am assuming you intend to add code to delete the WordNodes you've allocated with new.

    Comment

    • kylie991
      New Member
      • Feb 2009
      • 41

      #3
      yes I will have to do that too.. I'm only new to this :) will get the errors...

      Comment

      • kylie991
        New Member
        • Feb 2009
        • 41

        #4
        it just says filename.exe has stopped working. Windows can check online for a solution to the problem.

        so there is no syntax error being shown. could i possibly be going out of bounds???

        originally I tried to use this code to sort it but it didnt seem to work!

        while(!list)
        {
        WordNode *tempNode = new WordNode;
        tempNode->word=word;
        tempNode->link=list;
        list=tempNode;
        }

        node *current=head,* last;
        while(current!= NULL && current->word< word)
        {
        last=current;
        current=current->link;
        }
        node *temp;
        temp->word=word;
        last->link=temp;
        temp->link=current ;

        if I left this code as node*temp.. then it crashes. So I created temp new Wordnode to stop this from happening...

        Comment

        • scruggsy
          New Member
          • Mar 2007
          • 147

          #5
          Originally posted by kylie991
          it just says filename.exe has stopped working. Windows can check online for a solution to the problem.

          so there is no syntax error being shown. could i possibly be going out of bounds???

          originally I tried to use this code to sort it but it didnt seem to work!

          while(!list)
          {
          WordNode *tempNode = new WordNode;
          tempNode->word=word;
          tempNode->link=list;
          list=tempNode;
          }

          node *current=head,* last;
          while(current!= NULL && current->word< word)
          {
          last=current;
          current=current->link;
          }
          node *temp;
          temp->word=word;
          last->link=temp;
          temp->link=current ;

          if I left this code as node*temp.. then it crashes. So I created temp new Wordnode to stop this from happening...
          Okay. You are getting a run-time error then, not a compile-time error as you indicated in your first post.
          In this bit:
          Code:
          WordNode *last;
          while(current!=NULL && current->word<word)
          {
          last=current;
          current=current->link;
          }
          ...it is possible for last to never become initialized with the address of a WordNode if execution never enters the body of your while loop, which would cause a crash later if you try to use last.

          I'm unclear about what you're trying to do with this bit:
          Code:
          while(!list)
          {
          WordNode *tempNode = new WordNode;
          tempNode->word=word;
          tempNode->link=list;
          list=tempNode;
          }
          This loop can only run at most once, because you assign to list within the body, making !list false - is that intentional (if so, why not just use an if?)

          I suggest writing out the logical steps you need to take, in English (or whatever natural language you're most comfortable with), and making sure you've covered all possible cases before moving on to code. That way you won't get bogged down in all the pointer stuff before you've got a handle on the actual problem.

          Comment

          • kylie991
            New Member
            • Feb 2009
            • 41

            #6
            oh sorry i get confused with the terminology.. Ive worked it out anyway :) but thanks for trying to help!

            Comment

            Working...