Thread: delete function does not work properly...

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    23

    delete function does not work properly...

    Code:
    struct lista* del(struct lista* p, char* path1) {
           char model[MAX2];
           int len;
           char ch;
           
           printf("Type model.\n");
           scanf("%s", model);
           puts("\n");
           struct lista* t = p;
           if((strcmp(p->l.model, model)) == 0) {
               t = p->next;
               free(p);
               return t;
             }
           while((t->next) != NULL) {
            if((strcmp(t->l.model, model)) == 0) {
             struct lista* e = t->next;
             t->next = e->next;
             free(e);
             break;
            }
           t = t->next;
           }
    return p;
    }
    Hello, this function should delete each element in the list which is the same as this one typed by user. There are no errors, but function doesnt work.
    Can you please help me?

    EDIT:// It deletes something, but not this element which should.
    Last edited by fkmk; 01-18-2014 at 06:50 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Once you find the model, you delete the item after the selected item. I can't tell whether you intended to do
    Code:
    strcmp(t->next->l.model, model)) == 0
    or just really fubared the delete code, but I suspect the former.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    23
    EDIT2// the function should delete the items which are equal to selected by user.
    it deletes only one element...

    I corrected this code to be similiar to previous:
    Code:
    struct lista* del(struct lista* p, char* path1) {
           int year;
           printf("Type year.\n");
           scanf("%d", &year);
           puts("\n");
           struct lista* t = p;
           if(p->l.year == year) {
               t = p->next;
               free(p);
               return t;
             }
           while((t->next) != NULL) {
            if(t->next->l.year == year) {
             struct lista* e = t->next;
             t->next = e->next;
             free(e);
             break;
            }
           t = t->next;
           }
    return p;
    }
    It should delete an item from the list, when its equal to value served by user.
    It delete a one item, but others are intact. :/
    Last edited by fkmk; 01-18-2014 at 07:01 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by fkmk View Post
    It delete a one item, but others are intact. :/
    Then don't break out of the loop in the middle.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    23
    Quote Originally Posted by tabstop View Post
    Then don't break out of the loop in the middle.
    When I delete break from the loop, the function causes the whole program while deleting items is crashing.
    Last edited by fkmk; 01-18-2014 at 07:23 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, anyway, tabstop isn't wrong. It might not be as simple as removing the break statement, though.

    It's much neater if you try to do this in a big loop. You will need a pointer to the current node, a next pointer, and a prev pointer. The reason is since next and prev change all the time, it is easiest just to save them, so you know where you are in the list. Then you loop over the list with respect to the current node. The idea is to move next and prev normally if the node DOES NOT match delete criteria -- this means progressing as next = current->next and prev = current by the end of the loop.

    When the current node DOES get deleted, you need to
    remove it from the list:
    if (current == list) list = next; /* removing head */
    if (prev != NULL) prev->next = next; /* removing other node */
    delete the node:
    free(current);
    Last edited by whiteflags; 01-18-2014 at 09:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input does not work properly
    By mohsen in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2013, 04:30 PM
  2. print function does not work properly
    By mohsen in forum C++ Programming
    Replies: 11
    Last Post: 05-22-2013, 10:02 AM
  3. modf function doesn't work properly
    By paranoidgnu in forum C Programming
    Replies: 1
    Last Post: 06-29-2011, 09:59 AM
  4. Replies: 2
    Last Post: 12-09-2009, 12:04 PM
  5. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 10:59 PM