problem with deleting the first node in a linked list

This is a discussion on problem with deleting the first node in a linked list within the C Programming forums, part of the General Programming Boards category; Hi there. This is my first post, so please be gentle :-) I have a little program which creates a ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    problem with deleting the first node in a linked list

    Hi there. This is my first post, so please be gentle :-)

    I have a little program which creates a linked list. Adding to the list isn't a problem, but deleting the first node is. And I don't know why this code doesn't work.

    Code:
    void DeleteFirst (node** head)
    {
    	node* current;
    	
    	current = *head;	
    
    	if (current = NULL)
    	{
    		printf("\nThe List is empty\n");
    	}
    	else
    	{
    		*head = current->next;
    		free(current);
    	}
    }
    If you like, I can post the entire code

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,812
    You wrote current = NULL when you probably intended to write current == NULL.
    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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    yep, I am an a idiot

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Using a higher warning level on you compiler should have told you about that mistake.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    I use Xcode. Maybe the compiler can be set to a higher warning level but I am not aware of it.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If there's a setting where you can pass compiler flags, try passing: -Wall
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21