Hey, Im trying to delete a specified node from a linked list.
I've inlcuded my whole program but the problem is in removeNode() function.
The program never enters the if statement in removeNode()
The function removeNode is not finished. It only checks for the special case where the node to be removed is the head of the linked list.
What is wrong with my if statement ?
Thanks
Code:
#include <stdio.h>
#include <stdlib.h>
/*linked list*/
struct node {
int data;
struct node* next;
};
/*prototypes*/
void push(struct node**, struct node*);
void printList(struct node*);
struct node* createNode(int);
int removeNode(struct node**, struct node*);/*remove the specified link*/
int main(void){
struct node* head = NULL;
push(&head, createNode(5));
push(&head, createNode(20));
printList(head);
removeNode(&head, createNode(20));
printList(head);
return 0;
}
void push(struct node** head, struct node* newNode){
struct node* current = *head;
newNode->next = *head;
*head = newNode;
}
void printList(struct node* head){
struct node* current = head;
while(current != NULL){
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
struct node* createNode(int data){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int removeNode(struct node** head, struct node* node){
struct node* current = *head;
if(current == node){
*head = current->next;
return 1;
}
return 0;
}