Write a program to delete the nodes whose information part is divisible by 7 in a doubly linked list. For example, if input: 5->7->11->3->21->6, then output 5->11->3->6.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int d;
struct Node* next;
struct Node* prev;
};
void insert(struct Node** h, int d) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->d = d;
newNode->next = (*h);
newNode->prev = NULL;
if ((*h) != NULL)
(*h)->prev = newNode;
(*h) = newNode;
}
void delete(struct Node** h, struct Node* del_node) {
if (*h == NULL || del_node == NULL)
return;
if (*h == del_node)
*h = del_node->next;
if (del_node->next != NULL)
del_node->next->prev = del_node->prev;
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
free(del_node);
}
void display(struct Node* node) {
struct Node* last;
while (node != NULL) {
printf("%d->", node->d);
last = node;
node = node->next;
}
if (node == NULL)
printf("NULL\n");
}
int main() {
struct Node* h = NULL;
insert(&h, 5);
insert(&h, 7);
insert(&h, 11);
insert(&h, 3);
insert(&h, 21);
insert(&h, 6);
display(h);
delete(&h, h->next->next->next->next->next);
display(h);
}
Comments
Leave a comment