Deleting a linked list help...

This is a discussion on Deleting a linked list help... within the C Programming forums, part of the General Programming Boards category; A friend helped me write these lines of code to delete a linked list. The program is for making a ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Deleting a linked list help...

    A friend helped me write these lines of code to delete a linked list. The program is for making a linked list of terms for a polynomial. So when I run it in valgrind, the program seems to only free 1 less term from the linked list. Could you guys possibly look at the code and maybe figure out what I'm missing? Thanks!
    Code:
    void deleteList (struct list *ll)
    {
            struct term *temp = ll->head;
            while (temp != NULL)
            {
                    struct term *prevTemp = temp;
                    temp = temp->next;
                    free(prevTemp);
            }
            ll->head = NULL;
            ll->tail = NULL;
    }
    I appreciate any help and/or advice.
    -Squeage

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    The code is right unless your list is messed up somehow.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    IOW, maybe it's not that you're freeing one too few, but mallocing one too many.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Well here's is the code where I allocate the memory for making a new term for the linked list. Maybe there is a problem with it like tabstop said.
    Code:
    struct term *newTerm (float pcoef, char pvar, int pexp)
    {
            struct term *nt = (struct term *)malloc(sizeof(struct term));
            nt->coef = pcoef;
            nt->var = pvar;
            nt->exp = pexp;
            nt->next = NULL;
            nt->prev = NULL;
            return nt;
    }
    Thanks.
    -Squeage

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you link that newTerm into the list somewhere?

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Are you wanting to know how I use that to add terms to the list?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Yes. The fault does not lie in the delete or allocate functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Ok well here is my inputPoly function. I know it contains either 1 or 2 other functions I haven't posted, but hopefully I won't have to.
    Code:
    struct list inputPoly ()
    {
            struct list ll = newList();
            struct term *t;
            float coef;
            char var;
            char choice;
            int exp;
            int cont = 1;
    
            while (cont == 1)
            {
                    printf ("Enter the coefficient: ");
                    scanf("%f", &coef);
                    while (getchar() != '\n');
                    printf ("Enter the variable: ");
                    scanf("%c", &var);
                    while (getchar() != '\n');
                    if (tolower(var) >= 'a' && tolower(var) <= 'z')
                    {
                            printf ("Enter the exponent: ");
                            scanf ("%d", &exp);
                            while(getchar() != '\n');
    
                            t = newTerm(coef, var, exp);
                    }
                    else
                    {
                            t = newTerm(coef, NOVAR, 0);
                    }
    
                    pushBack(&ll, t);
                    printf("Would you like to enter another? (y/n) ");
                    scanf("%c", &choice);
                    if (choice == 'y')
                            cont = 1;
                    else
                            cont = 0;
            }
    
            simplify(&ll);
    
            return ll;
    
    }
    Thanks guys. Oh and this will be my last post until later on tonight, or maybe even Sunday night. I'll leaving to go out of town here in a minute. Thanks for the help!
    -Squeage
    Last edited by Squeage; 03-13-2008 at 07:15 PM. Reason: oops, i had to re-post the code cause it was really messed up before, sorry

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Well since most of it is freed, there is probably a bug there somewhere hidden behind some few lines of code, but it's not a big deal. At least i wouldn't bother if i had some other task to complete, even though i am somewhat maniac about my memory also. But for an assignment such a small bug would really go undetected, if the indent was fine and the files full of long unnecessary comments
    Your presented code looks good an' that's what really matters in universities. Write good flawless code when good flawless code is needed.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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