How to reset to the beginning of a linked list?

This is a discussion on How to reset to the beginning of a linked list? within the C Programming forums, part of the General Programming Boards category; I have a function that will look something like this: Code: void function(node* root, int num) { int i = ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    How to reset to the beginning of a linked list?

    I have a function that will look something like this:

    Code:
    void function(node* root, int num)
    {
        int i = 0;
        
        while(i < 10)
        {
            while(root->data != num || root != NULL)
            {
                root->next;
            }
            if(root->data == num)
            {
                root->count++;
                i++;
            }
        }
    }
    How do I get it so I can check root from the beginning of the list every time and still keep the updated count value??

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Keep an extra pointer to save the original root.

    Code:
    while (i < 10)
    {
        node* save = root;
        ...
        root = save;
    }
    Now the middle part will always start at "root"

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    Quote Originally Posted by whiteflags View Post
    Keep an extra pointer to save the original root.

    Code:
    while (i < 10)
    {
        node* save = root;
        ...
        root = save;
    }
    Now the middle part will always start at "root"
    Will that keep the update of count?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    It should. Right now your code will update count on the node whose data matches num. You're simply resetting the value of the root pointer to what it was before you started -- which does not mean anything for the dereferenced values.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Use a temp variable.

    Every time you wish to reset the list, change the temp variable back to the location entered into the function.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    Then I do not understand what is wrong with this:

    Code:
    void pickDoubleMu(teams * sPtr, matchups * head)
    {
        int i = 0;
        char *outputText = "output.txt";
        FILE *output = fopen(outputText, "w");
        int randMu;
        int j = 0, k;
        matchups *temp;
        teams *team;
        teams *other;
        
        int usedMu[20];
        while(i < 20)
        {
            temp = head;
            team = sPtr;
            other = sPtr;
    
            randMu = rand() % 45;
            for(j = 0; j <= randMu; j++)
            {
                temp = temp->next;
            }
            if(temp->count == 0)
            {
                temp->count++;
                while(temp->team1 != team->num)
                {
                    team = team->next;
                }
                if(team->count < 4)
                {
                    team->count++;
                    while(temp->team2 != other->num)
                    {
                        other = other->next;
                    }
                    if(other->count < 4)
                    {
                        other->count++;
                    }                            
                }
            }
    
            if(team->count <= 1 && other->count <=4 && team->count <=4)
            {
                usedMu[i] = temp->num;
                i++;
            }
        }
        for(k = 0; k < 20; k++)
        {
            
            temp = head;
            while(usedMu[k] != temp->num)
                temp = temp->next;
            fprintf(output, "%d\t%d\t%d\t%d\n", usedMu[k], temp->team1, temp->team2, temp->count);
        }
    
    }
    It will either segfault or print duplicate values.. I want there to be no duplicates in temp->num, and each team(team->num) should be used 4 times. This will produce duplicates or infinite loop. I've been working on this program for so long and can not get it working
    Last edited by popnfresh12321; 12-10-2012 at 11:19 PM.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Going back to the simple function, and my simple answer:
    Code:
    while (i < 10)
    {
        node* save = root;
        ...
        root = save;
    }
    You realize that you wouldn't otherwise touch "save", right? If you change the temp pointer in your function, as you are, then it won't serve its purpose.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Well if it's crashing with a segfault, then run it in the debugger and it will point you at the line of code causing the problem.

    For me, all your unchecked temp = temp->next; constructs are dangerous. If you run into the end of the list by mistake, then this just blows up.

    Code:
            for(j = 0; temp != NULL && j <= randMu; j++)
            {
                temp = temp->next;
            }
            // check temp is not NULL
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reset to Beginning of file
    By TenTierHook in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2010, 04:54 AM
  2. Replies: 4
    Last Post: 07-05-2010, 06:57 AM
  3. How to reset a linked list i reversed
    By Soulzityr in forum C Programming
    Replies: 5
    Last Post: 02-16-2010, 03:20 PM
  4. Reset fgets to read from the beginning of a file?
    By bivhitscar in forum C Programming
    Replies: 2
    Last Post: 05-20-2006, 11:42 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21