Thread: linked list delete method

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    linked list delete method

    My delete method is not working
    Code:
    struct node{
        int key;
        struct node *next;
        
    };
    
    
    struct listset{
        int length;  
        struct node *head;
    };
    struct listset * litset_new(){
        struct listset *p = malloc(sizeof(*p)); 
        p->head=malloc(sizeof(p->head)); //creating space for my node head in the list
        p->head->next=NULL;
        p->head->key=0;
        
        
        return p;
         
    }
    void listset_add(struct listset * t, int item){
        struct listset *a=t;
        struct node *p= malloc(sizeof(*p));
       
        p->key=item;
        
        p->next =a->head->next;
        a->head->next=p; 
       
    }
    
    
    /*look up fucntion checks to see if a list contains a certain item it returns
     * a 1 if it does and a zero if it dosent
     * 
     */
    int listset_lookup(struct listset * p , int item){
       struct listset *a=p;
        struct node*current=a->head->next;
        while(current!=NULL){
            if(current->key==item)
                return 1;
            current=current->next;
           //printf("i am here");
        }
        return 0; 
    }
    void listset_remove(struct listset * p, int item){
       struct listset *a=p;
       struct node * next;
       struct node *previous;
       previous=a->head;  // first previous is my head
       struct node*current=a->head->next;//first node in list
        if(listset_lookup(a,item)){
            
            while(current!=NULL){
                if(current->key==item){
                    //printf("%d\n",item);
                    previous=current->next;
                    printf("%d\n",previous->key);
                    break;
                   
                }
                
                 printf("i am in");
                previous=previous->next;
                current=current->next;
            }
            
            
        }
    }
    
    
    /* Given a linke list head pointer 
     *  this function to counts the number of nodes in a list
     */
    int Length(struct node *head) {
    
    
    struct node* current =head; 
    int count = 0;
    while (current != NULL) 
    { count++;
    current = current->next;
    }
    return count;
    }
    
    
    int main(int argc, char** argv) {
        struct listset  *s;
        int b=0;int c=0;
         s=litset_new();
         listset_add(s,5);
         listset_add(s,6);
         listset_add(s,7);
         listset_add(s,4);
         b= listset_lookup(s,4);
          
        // printf("%d\n",b);
         printf("%d\n",s->head->next->key);
         printf("%d\n",s->head->next->next->key);
         printf("%d\n",s->head->next->next->next->key);
         printf("%d\n",s->head->next->next->next->next->key);
           listset_remove(s,4);
            c= listset_lookup(s,4);
             printf("%d\n",c);
    
    
        return (EXIT_SUCCESS);
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sigur47 View Post
    My delete method is not working
    Explain what that means if you want some help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by iMalc View Post
    Explain what that means if you want some help.
    it does not delete the item that if its found.so basically if a node contains an item. the previous node then points to the nothe that the deleted node was pointing to

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    You're list_add method is even more confusing. Now you have a singly linked list (because the last element points to NULL), and your "list_add" method is inserting the new element in the second position. Not sure that's what you want to do, check the response to your first post. And again you don't need to allocate the head at the creation of the set, you can set it to NULL and just allocate it the first time you call "list_add".

    I think you are a bit confused with linked list and you should spend some time on a tutorial, it would help you understand the concept and how to use them.

    In your "listset_remove" method you are just assigning "previous" to the head, then to "current->next", and then to "previous->next", and doing the same kind of weird thing with "current", so you really have to think about what you want to do, maybe drawing some schemes might help you.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Tibo-88 View Post
    You're list_add method is even more confusing. Now you have a singly linked list (because the last element points to NULL), and your "list_add" method is inserting the new element in the second position. Not sure that's what you want to do, check the response to your first post. And again you don't need to allocate the head at the creation of the set, you can set it to NULL and just allocate it the first time you call "list_add".

    I think you are a bit confused with linked list and you should spend some time on a tutorial, it would help you understand the concept and how to use them.

    In your "listset_remove" method you are just assigning "previous" to the head, then to "current->next", and then to "previous->next", and doing the same kind of weird thing with "current", so you really have to think about what you want to do, maybe drawing some schemes might help you.
    i thought when a list set structure is created.. it first contains a node in the listset structure called head which den points to another node that is null

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    I don't really now about this list set, because I usually just create linked list by combining the nodes together and just keeping the head as a pointer to the list, but it does not really make sense to have a first node that does not contain useful information, while you could have a useful node instead, or NULL if there is no element in the list.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Tibo-88 View Post
    I don't really now about this list set, because I usually just create linked list by combining the nodes together and just keeping the head as a pointer to the list, but it does not really make sense to have a first node that does not contain useful information, while you could have a useful node instead, or NULL if there is no element in the list.
    thank ou very much would reiplement it now.my list set structure is a structure that contains nodes

  8. #8
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by sigur47 View Post
    i thought when a list set structure is created.. it first contains a node in the listset structure called head which den points to another node that is null
    don't worry about the head pointer..
    you can call a recursive function to keep adding nodes...(which i find easy..as saves a lot of time by not writting bulky codes)
    then to delete any node..u got to break the link...and use free(curr) to dealloccate memory...where curr points to the node u are deleting...after the deletion again join the links

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by rajarshi View Post
    don't worry about the head pointer..
    you can call a recursive function to keep adding nodes...(which i find easy..as saves a lot of time by not writting bulky codes)
    then to delete any node..u got to break the link...and use free(curr) to dealloccate memory...where curr points to the node u are deleting...after the deletion again join the links
    raj here is my algorith
    creat a structure that contains a node as head
    create a new node an make my new node=null;
    head->new node.

    to add a node
    just add a node at the head.let me head point to the node just added and d node just added point to the node head pointed to

  10. #10
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by sigur47 View Post
    raj here is my algorith
    creat a structure that contains a node as head
    create a new node an make my new node=null;
    head->new node.

    to add a node
    just add a node at the head.let me head point to the node just added and d node just added point to the node head pointed to
    In short u want to keep on adding nodes at the beginning of a linked list...right ??
    why are u using head as a node (u are putting absolutely nothing in it....why are u allocating memory for it ?? make head a pointer instead)

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete all from linked list
    By lio in forum C Programming
    Replies: 4
    Last Post: 12-01-2010, 03:15 PM
  2. Linked list - Delete first node
    By budala in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 01:32 PM
  3. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  4. retieve method linked list
    By TimC in forum C++ Programming
    Replies: 0
    Last Post: 12-15-2005, 04:06 PM
  5. Linked List Move Method
    By TimC in forum C++ Programming
    Replies: 1
    Last Post: 12-14-2005, 02:15 PM

Tags for this Thread