caxapexac

Untitled

Jul 3rd, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct node {
  4.     int data;
  5.     struct node* next;
  6. } node;
  7.  
  8. void printer(node* head) {
  9.     while (head) {
  10.         printf("%d\n", head->data);
  11.         head = head->next;
  12.     }
  13. }
  14.  
  15. node* pop (node* head) {
  16.     if (head->next == NULL) return NULL;
  17.    
  18. }
  19.  
  20. int removeEl(node** head, int n)
  21. {
  22.     node* prev = NULL;
  23.     node* curr = *head;
  24.     while (curr != NULL && curr->data != n) {
  25.         prev = curr;
  26.         curr = curr->next;
  27.     }
  28.     if (curr->data == n) {
  29.         if (curr == *head) {
  30.             *head = curr->next;
  31.         }
  32.         else {
  33.             prev->next = prev->next->next;
  34.         }
  35.         return 1;
  36.     }
  37.     else {
  38.         return -1;
  39.     }
  40.    
  41. }
  42.  
  43. int main(void){
  44.     node head;
  45.     node *headp = &head;
  46.     head.data = 5;
  47.     node n2;
  48.     n2.data = 6;
  49.     node n3;
  50.     n3.data = 2;
  51.     n3.next = NULL;
  52.     n2.next = &n3;
  53.     head.next = &n2;
  54.     removeEl(&headp, 2);
  55.     printer(headp);
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment