Linked list - Delete first node

This is a discussion on Linked list - Delete first node within the C Programming forums, part of the General Programming Boards category; Hi all. I am having trouble deleting the first node from a linked list. Code: #include <stdio.h> #include <stdlib.h> #include ...

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

    Linked list - Delete first node

    Hi all. I am having trouble deleting the first node from a linked list.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct node
    	{
    		int age;
    		char name[10];
    		struct node *next;
    	} person;
    	
    void Menu()
    {
    	printf("\n***\tMENU\t***\n");
    	printf("\n 1.) add");
    	printf("\n 2.) print");
    	printf("\n 3.) delete");
    	printf("\n 0.) EXIT");
    	printf("\nChoose ---------> ");	
    }
    
    void Add(cvor **head)
    {
    	person *new = malloc(sizeof(person));
    	char name[10];
    	
    	printf("\nEnter name: ");
    	scanf("%s", name);
    	strcpy(novi-> name, name);
    	printf("Enter age: ");
    	scanf("%d",&novi-> age);
    	
    	if (*head == NULL)
    	{
    		*head = new;
    		new->next = NULL;
    	}
    	else
    	{
    		new->next = *head;
    		*head = new;
    	}
    }
    
    void Print(person **head)
    {
    	person *temp = *head;
    	int i = 1;
    	
    	printf("\n\tLIST MEMBERS\n\n");
    	if (temp == NULL) printf("List is empty!");
    	else
    	{
    		while (temp != NULL)
    		{
    			printf("No: %d\tNAME: %s\tAGE: %d\n", i, temp->name, temp->age);
    			temp = temp->next;
    			i++;
    		}
    	}
    
    	printf("\n\n");
    }
    
    void Delete(person **head)
    {
    	person *temp = *head;
    	person *p;
    	
    	if (temp == NULL) printf("List is already empty!");
    	else
    	{
    		p = temp->next;
    		free(temp);
    		temp = p;
    	}
    
    }
    
    int main (int argc, const char * argv[]) 
    {
        person *head = NULL;
    	int choice;
    	
    	do
    	{
    		Menu();
    		scanf("%d",&choice);
    		switch (choice)
    		{
    			case 1:
    				Add(&head);
    				break;
    			case 2: 
    				Print(&head);
    				break;
    			case 3:
    				Delete(&head);
    				break;
    			default:
    				printf("\nWRONG CHOICE");
    		}
    	} while (choice != 0);
    	
        return 0;
    }
    But it doesn't work as expected. I enter a person, delete it, and when I print the list it isn't deleted. I choose "3" again and get this error messagge

    Code:
    23324(4767) malloc: *** error for object 0x300230: double free
    23324(4767) malloc: *** set a breakpoint in szone_error to debug

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if you delete the head of the list, you need to change head to point to the new head of the list.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    You do not need "person *p" to delete a node in front. You cannot point temp to p anymore after bringing back the memory to the operating system. As tabstop mentioned you have to make the head point to a new head. head and temp both point to the list make head point to the next node where the temp is pointing to then free it.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    thanks. figured it out thanks to you guys

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    void Add(cvor **head)
    What is the use of cvor here?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 06:54 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 07:52 AM

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