repl.it
@paulfears/

linked_list_delete

C

No description

fork
loading
Files
  • main.c
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>

struct node{
  int value;
  struct node* next;
};

void printList(struct node* head){
  struct node* cur = head;
  printf("%d",cur->value);
  while(cur->next){
    cur =  cur->next;
    printf("->%d", cur->value);
  }
  printf("\n");
}




void deleteNodeByValue(struct node* head, int value){
  if(!head->next){
    return;
  }
  if(head->value == value){
    *head = *head->next;
    return;
  }
  if(head->next->value == value){
    struct node* temp;
    temp = head->next;
    //free(head->next);
    if(temp->next){
      head->next = temp->next;
    }
    else{
      head->next = NULL;
    }
    return;
  }
  deleteNodeByValue(head->next, value);
}

void appendNode(struct node* head, struct node* last){
  struct node* cur = head;
  
  while(cur->next){
    cur = cur->next;
  }
  cur->next = last;
}


int main(void) {
  struct node head = {};
  struct node next0 = {2};
  struct node next1 = {3};
  struct node next2 = {4};
  struct node next3 = {5};
  head.value = 1;
  head.next = &next0;
  next0.next = &next1;
  next1.next = &next2;
  next2.next = &next3;
  

  printList(&head);
  deleteNodeByValue(&head, 5);
  
  printList(&head);


  struct node eighty = {};
  eighty.value = 80;
  struct node nienty = {};
  nienty.value = 90;
  appendNode(&head, &eighty);
  appendNode(&head, &nienty);
  printList(&head);


  printf("Hello World\n");
  return 0;
}
?