Hi all. I am having trouble deleting the first node from a linked list.
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 messaggeCode:#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; }
Code:23324(4767) malloc: *** error for object 0x300230: double free 23324(4767) malloc: *** set a breakpoint in szone_error to debug



LinkBack URL
About LinkBacks


