Thread: exercise help

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    39

    exercise help

    I am doing an exercise from King. Can you give some hint. The following code deletes a node
    Code:
    struct node *delete_from_list(struct node *list, int n)
    {
    	struct node *cur, *prev;
    	cur = malloc(sizeof(struct node));
    	
    	for (cur= list, prev = NULL;
    	     cur != NULL && cur->value !=n;
                 prev = cur, cur = cur->next )
                 ;
    	if (cur == NULL)
                 return list;
           if (prev == NULL)
             list = list->next;
          else 
           prev->next = cur-> next;
          free(cur);
    	return cur;
    }
    Isn't it cheating that the returned list is a new list with out the unwanted value it is not the original list which still occupies memory? The question is to do this with just a single variable. So I have to lose one of cur, prev. My idea is to make a linked list from scratch and assign it only the desired value. My attempt is as follows. But its not working. I got lost somewhere out there.
    Code:
    struct node *delete_from_list(struct node *list, int n)
    {
            struct node *prev;
    	prev = malloc(sizeof(struct node)); 
    	
        for (prev->value = list->value;  list!= NULL;list = list->next)
        {
    		if (list->value !=n)
    		{
    			prev->value = list->value;
                           // In the prev next store the address that list next contains
                          // this line causes the error
    			*(prev->next) = *(list->next); 
    		}
    	}
         return prev;
    compiler complains incompatible types when assigning to type ‘struct node *’ from type ‘struct node’
    Last edited by ali.franco95; 09-24-2011 at 06:52 AM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    That code for delete_from_list() is broken. However, what it appears to be trying to do is hardly what I'd call "cheating". All it seems to be trying to do is unhook an element from the list and then free it. That's exactly what you'd want such a function to do. It's not creating a new list at all. It's just returning a pointer to a node in the original list. You want to do this because you might wind up deleting the first element of the list, and then what would you do?

    The obvious problems with this delete_from_list(): it allocates memory for cur and then promptly overwrites it. It frees cur and then immediately returns cur, which is unusable once it's freed.

    The idea behind delete_from_list() is right. What you need to do is find your element to delete in the list. If it doesn't exist, return the list as-is. If the element does exist, unhook it by pointing the previous element to the next element. Take special care if the element to delete is at the head of the list.

    Hint: There is absolutely no need to use malloc() for this.

    If this "King" (I have no idea who it refers to) wrote delete_from_list(), you should stop learning from him immediately. He is an amateur C programmer at best given that he so royally screwed up a simple linked list function.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Here is a decent Linked List Tutorial
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exercise help
    By ali.franco95 in forum C Programming
    Replies: 3
    Last Post: 09-09-2011, 05:59 AM
  2. K&R Exercise 1-9
    By Anarchy in forum C Programming
    Replies: 6
    Last Post: 01-10-2010, 12:42 PM
  3. Help exercise!
    By sanhthai in forum C++ Programming
    Replies: 10
    Last Post: 09-22-2009, 12:07 PM
  4. gdb exercise!
    By MK27 in forum C Programming
    Replies: 9
    Last Post: 11-24-2008, 10:17 AM
  5. Help with K&R Exercise 1.13
    By Yannis in forum C Programming
    Replies: 2
    Last Post: 09-21-2003, 02:51 PM