Thread: Help with Singly Linked List !!

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    51

    Help with Singly Linked List !!

    Hey I was trying to write a function in C to delete a node(only from the middle) from a Singly Linked List. I wrote one but not sure if the code will work fine under all test conditions. I have tested it and shows no error so far, but can you help me figure out any problems/issues that might arise if i used it that i have missed?
    Code:
       void deleteAt(struct node *root, int number){
        while(root->link != NULL)
        {
            if(root->link->item == number) //checks if the next node is the element to be deleted
            {
                root->link = root->link->link; //points the link of the element to be deleted to the element before the element to be deleted
            }
            else
                root = root->link;
        }
    
    
    }
    Or is there a better way to do this?

  2. #2
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    The way the function is currently defined, it will delete each element in the list that is equal to 'number' (and also, not the first element of the list.) But the name seems to imply that it will delete the element that occurs at the position 'number', so there seems to be some confusion there. For the first behaviour I think you could just call the function "delete".

    For either intention, the problem I see is that it doesn't handle the first element of the list, so perhaps you might consider one of the following function types?

    Code:
    void delete(struct node **listp, int value);
    struct node *delete(struct node *list, int value);
    Either of these would allow you to delete the first element. I personally tend to lean more toward the first for in-place deletions and the second for producing a copy of the list with each element equal to 'value' removed.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    51
    The naming is ambiguous indeed. I wanted to write a separate function like POP(); to delete the first element in the linked list. Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you use malloc to allocate nodes? If so, remember to free.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly linked list c
    By Creotik in forum C Programming
    Replies: 5
    Last Post: 01-15-2014, 08:42 PM
  2. Singly-linked list
    By Alcatar in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2013, 12:46 PM
  3. singly linked list
    By aextine in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 02:20 PM
  4. singly linked list
    By right2001 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 10:21 AM
  5. singly linked list
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 11:19 AM