hey guys,
i know that I have made already many posts but this one is tough! I want to make a function that deletes the Node when it finds a specific value
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
struct node *next,*prev;
int data;
}Node;
typedef struct list{
Node *head,*tail;
int len;
}List;
int find_len(List *list)
{
int counter = 0;
Node *dummy;
dummy = (*list).head;
while(dummy != NULL)
{
counter = counter + 1;
dummy = dummy->next;
}
list->len = counter;
return 0;
}
int del1st(List *list,int value)
{
Node *dummy;
dummy = (*list).head;
while(dummy != NULL)
{
if(dummy->data == value)
{
dummy->prev->next = dummy->next;
free(dummy);
}
dummy = dummy -> next;
}
return 0;
}
int main()
{
Node *new1,*new2,*new3;
List list;
new1 = malloc(sizeof(Node));
new2 = malloc(sizeof(Node));
new3 = malloc(sizeof(Node));
new1->data = 26;
new2->data = 12;
new3->data = 45;
list.head = new1;
list.tail = new3;
list.head->next = new2;
new2->next = new3;
new3->next = NULL;
new3->prev = new2;
new2->prev = new1;
find_len(&list);
printf("LENGTH = %d\n",list.len);
del1st(&list,12);
printf("LENGTH = %d\n",list.len);
return 0;
}
I put in the find_len function to be easier to count the nodes.
Bookmarks