Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  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.  
  40.  
  41.     return 0;
  42. }
  43.  
  44.  
  45. int rem(N** head, int key)
  46. {
  47.     N** outside = head;
  48.     N* temp;
  49.  
  50.     while((temp = *outside)){
  51.         if(temp->n == key){
  52.             *outside = temp->next;
  53.             free(temp);
  54.             return 1;
  55.         } else {
  56.             outside = &temp->next;
  57.         }
  58.     }
  59.     return -1;
  60. }
  61.  
  62. void pl(N* node){
  63.     while(node){
  64.         printf("[%d] ", node->n);
  65.         node = node->next;
  66.     }
  67.     printf("\n");
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement