#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.