SHARE
TWEET

Untitled

a guest Apr 26th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <regex.h>
  5.  
  6. typedef struct Node{
  7.     int n;
  8.     struct Node* next
  9. } N;
  10.  
  11. int rem(N** head, int key);
  12. void pl(N* node);
  13.  
  14. int main()
  15. {
  16.     N* head = (N*)calloc(1, sizeof(N));
  17.     head->next = (N*)calloc(1, sizeof(N));
  18.     head->next->next = (N*)calloc(1, sizeof(N));
  19.     head->next->next->next = (N*)calloc(1, sizeof(N));
  20.     head->next->next->next->next = (N*)calloc(1, sizeof(N));
  21.     //
  22.     head->n = 1;
  23.     head->next->n = 2;
  24.     head->next->next->n = 3;
  25.     head->next->next->next->n = 4;
  26.     head->next->next->next->next->n = 5;
  27.     pl(head);
  28.     //delete first item
  29.     rem(&head, 1);
  30.     pl(head);
  31.     //delete last
  32.     rem(&head, 5);
  33.     pl(head);
  34.  
  35.     //del Mid
  36.     rem(&head, 3);
  37.     pl(head);
  38.  
  39.     //del mid
  40.     rem(&head, 3);
  41.     pl(head);
  42.  
  43.     //del
  44.     rem(&head, 2);
  45.     pl(head);
  46.  
  47.     //del last
  48.     rem(&head, 4);
  49.     pl(head);
  50.  
  51.     printf("%d\n", head);
  52.  
  53.     return 0;
  54. }
  55.  
  56.  
  57. int rem(N** head, int key)
  58. {
  59.     N** outside = head;
  60.     N* temp;
  61.  
  62.     while((temp = *outside)){
  63.         if(temp->n == key){
  64.             *outside = temp->next;
  65.             free(temp);
  66.             return 1;
  67.         } else {
  68.             outside = &temp->next;
  69.         }
  70.     }
  71.     return -1;
  72. }
  73.  
  74. void pl(N* node){
  75.     while(node){
  76.         printf("[%d] ", node->n);
  77.         node = node->next;
  78.     }
  79.     printf("\n");
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top