Answer to Question #236747 in C for SOM

Question #236747

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.


1
Expert's answer
2021-09-13T18:30:32-0400
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS