0
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 /* Node structure */
typedef struct node {
    int data;
    struct node *next;
} Node;

This is my node structure

   
 /* Create the linked list */
void create_list(Node *head) {
    char check;
    int i=1, num;
    char * temp;
    
    printf("Input data for node 1: ");
    scanf("%d", &num);
    head->data = num;
    
    printf("\nEnter more <y>es/<n>o ? ");
    scanf(" %c", &check);
    
    while(check=='y') {
        head -> next = (Node *) malloc(sizeof(Node));
        head = head->next;
    
        printf("\nInput data for node %d: ",i+1);
        scanf("%d", &head->data);
        i++;
        head->next = NULL;
        printf("\nEnter more <y>es/<n>o ? ");
        if (scanf(" %c", &check)!= 1) {
            printf(" Error: Invalid Input \n");
            scanf(" %s", &temp); /* Clear input buffer */
            continue;   
    }
}
}

This is the function to create the linked list


/* Delete first location*/
void delete_first(Node *head) {
    Node *prev_node;

    prev_node = head;//assign the prev pointer to beginning of the list
    head = head->next;//shift the node to next
    
    if(head == NULL)//condition for no elements in list
        printf("\n The List is empty ");
    else { //logic for deletion
        prev_node->next = head->next;
        free(head); //delete the node
}
}

This is the function to delete the first node


int main() {
Node *head, start;
    
start.next = NULL;
head = &start;
    
create_list(head);

head = &start;
delete_first(head);
}

This is my main

This is my code. Like the title the function to delete the first node turn out to delete the 2nd node instead. I think it has something to do with my create list function but atm I can't figure out why.

Nory Levi
  • 1
  • 3
  • 1
    How do you know it is deleting the second node? Please provide the code or method you are using to determine that. But one thing that stands out is that `delete_first` is not returning the new head to the calling function. Note that `head` is a local variable inside the function and changing it does not change the caller's variable. – kaylum Jun 13 '22 at 06:22
  • `head = head->next; ... free(head);` Why are you moving to `next` before deleting the node? Don't you want to delete `head` as that is the first node? But one aspect of the design may cause you problem - head is static memory whereas all other nodes are dynamic memory. Better to make everything dynamic memory. – kaylum Jun 13 '22 at 06:24
  • In the whole program I have a function to display all the node as well, so when I run the program I see the 2nd node got deleted instead of the first – Nory Levi Jun 13 '22 at 06:32
  • If the duplicate question list is not helpful, please let me know. – n. m. could be an AI Jun 13 '22 at 06:42

1 Answers1

1

Your implementation of delete_first is changing the value of what head points to, but has no way of conveying that back to the caller. Remember, passing the pointer head enables the caller to change the contents of head, but upon return, head is still pointing to what it was originally - which is a deleted node.

I see a few other bugs, so let's clean this up. It's far simpler than you have it. We can either have delete_first return the new "head" as a return value, or as an "out" parameter by allowing the caller to pass a pointer to a pointer. Let's go with the latter in this example:

void delete_first(Node **ptrToHead) {

    Node* head = *ptrToHead;

    if (head != NULL)
    {
        *ptrToHead = head->next;
        free(head);
    }

}

But your create_list function has a similar bug. So let's fix that, but we'll use return style signature so it's easier to read

Node* create_node(int value)
{
    Node* n = (Node*)malloc(sizeof(Node));
    n->next = NULL;
    n->data = value;
    return n;
}

Node* create_list() {
    char check;
    int i=1, num;
    Node *head = NULL;
    Node* last = NULL;
    
    printf("Input data for node 1: ");
    scanf("%d", &num);
    head = create_node(num);
    last = head;
    
    printf("\nEnter more <y>es/<n>o ? ");
    scanf(" %c", &check);
    
    while(check=='y') {
    
        printf("\nInput data for node %d: ",i+1);
        scanf("%d", &num);
        i++;

        last->next = create_node(num);
        last = last->next;

        printf("\nEnter more <y>es/<n>o ? ");
        if (scanf(" %c", &check)!= 1) {
            printf(" Error: Invalid Input \n");
            scanf(" %s", &temp); /* Clear input buffer */
            continue;   
    }

    return head;
}

Then main becomes this:

int main() {
    Node *head = create_list();
    
    // delete the first node - this will update "head"
    delete_first(&head);

    return 0;
}
selbie
  • 100,020
  • 15
  • 103
  • 173