delete node using only one pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Erjan
    New Member
    • Jun 2011
    • 4

    delete node using only one pointer

    i m trying to solve a textbook problem
    to delete a node using one pointer
    my code looks like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct node{
       int first ;
       struct node *next ;
    } ;
    
    struct node *add_to_list(struct node *lst, int n){
       struct node *newnode ;
       newnode = malloc(sizeof(struct node)) ;
       if(newnode == NULL){
          printf("malloc failed to add\n") ;
          exit(EXIT_FAILURE) ;
       }
       newnode->first = n ;
       newnode->next = lst ;
       return newnode ;
    }
    
    void free_list(struct node *lst) {
    struct node *p;
       while (lst != NULL) {
       p = lst->next;
       free(lst);
       lst = p;
       }
    }
    
    void print_list(struct node *lst) {
    struct node *p;
       printf("( ");
       for (p = lst; p != NULL; p = p->next)
          printf("%d ", p->first);
       printf(")\n");
    }
    
    //use only one pointer!
    struct node * delete_from_list(struct node *lst, int key){
       struct node *temp ;
       struct node *helper ;
       if(lst == NULL)
          return NULL ;
       if(lst->first == key){
          temp = lst ;
          lst = lst->next ;
          free(temp) ;
          return lst ;
          }
       for(temp = lst ; temp != NULL && temp->first != key ; temp = temp->next){
             ;
       }        
             printf("%d\n",temp->first) ;
          if (temp == NULL) // key is not in the lst
             return NULL ;
          if (temp->first == key){
             temp = temp->next ;
             return lst ;    
          }
             
          
          helper = temp->next ;
          temp->next = helper->next ;
          //temp->next = temp->next->next ;
          free(helper) ;
          return lst ;
          
    }
    
    int main(void){
       struct node *l = NULL ;
       
       l = add_to_list(l,56) ;
       l = add_to_list(l,45) ;
       l = add_to_list(l,5) ;
       l = add_to_list(l,2) ;
       
       print_list(l) ;
       l = delete_from_list(l,5) ;
       
       print_list(l) ;
       free_list(l) ;
       return 0 ;
    }
    i tested my function on the following base cases -
    if the list is empty (NULL),
    contains one element 4,NULL ;
    but when i try to pass a list 2,5,45,56, NULL and delete
    5 it just returns the whole list

    help?
    i don't really know if there is a way to use only 1 pointer - so i used 2
    thanks in advance
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You have a single linked list. So if there are nodes A*,B* and C* then to delete B you must set A->next to C. That means you need to know B->prev which you don't have available. So when you walk the list to find your key you need to keep the address of the prevoius node handy.

    Note that if you just delete B all that does is return the memory occuied by *B back to the free store. So if you walk your list again B will still show up as part of the list until that memory is reused and then you eill probably crash.

    Comment

    Working...