-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
99 lines (83 loc) · 1.98 KB
/
linked_list.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void add(struct Node** head, int data) {
// Create new node
struct Node* newNode = (struct Node*)malloc((sizeof(struct Node)));
newNode->data = data;
newNode->next = NULL;
// If our list is empty
if (*head == NULL) {
*head = newNode;
return;
}
// Add our new node to the end of our list
struct Node *key = *head;
while (key->next != NULL) {
key = key->next;
}
key->next = newNode;
}
void delete(struct Node** head, int data) {
// If our list is empty, return
if (*head == NULL) return;
struct Node* cur = *head;
struct Node* prev = NULL;
// Check if the first element is the one to delete
if (cur->data == data) {
*head = cur->next;
free(cur);
return;
}
// Loop through our linked list
while(cur->next != NULL && cur->data != data) {
prev = cur;
cur = cur->next;
}
// If we dont find our data, then return
if (cur == NULL) return;
// We found our data, so delete
prev->next = cur->next;
free(cur);
}
int contains(struct Node* head, int data) {
struct Node* cur = head;
int found = 0;
while (cur != NULL) {
if (cur->data == data) {
found = 1;
}
cur = cur->next;
}
return found;
}
void printLinkedList(struct Node* head) {
struct Node* cur = head;
while (cur != NULL) {
printf("%d -> ", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
add(&head, 1);
add(&head, 2);
add(&head, 3);
add(&head, 4);
add(&head, 5);
printf("Linked List: ");
printLinkedList(head);
delete(&head, 4);
printf("Delete 4: ");
printLinkedList(head);
printf("Does linked list have 3?: ");
if (contains(head, 3) == 1) {
printf("Yes\n");
} else {
printf("No\n");
}
}