-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathList.c
More file actions
141 lines (122 loc) · 2.18 KB
/
List.c
File metadata and controls
141 lines (122 loc) · 2.18 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "List.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
int add(int val, list* head)
{
list* curr = head;
if(curr != NULL && curr->val == INT_MIN)
{
curr->val = val;
return EXIT_SUCCESS;
}
while (curr->next)
{
curr = curr->next;
}
curr->next = (list*)malloc(sizeof(list));
curr->next->val = val;
curr->next->next = NULL;
return EXIT_SUCCESS;
}
list* removeNode(int val, list* head)
{
list* curr = head;
list* tmp = head;
if (curr->val == val)
{
head = head->next;
free(tmp);
return head;
}
while (curr)
{
if(curr->next != NULL && curr->next->val == val)
{
tmp = curr->next;
curr->next = curr->next->next;
free(tmp);
return head;
} else
{
curr = curr->next;
}
}
return tmp;
}
list* getNode(int val, list* head)
{
list* curr = head;
while (curr)
{
if(curr->val == val)
{
return curr;
} else
{
curr = curr->next;
}
}
return NULL;
}
int ListSize(list* head)
{
int count;
list* curr;
curr = head;
count = 0;
while (curr)
{
if(curr->val != INT_MIN)
{
++count;
}
curr = curr->next;
}
return count;
}
list* init_list()
{
list* head;
head = (list*)malloc(sizeof(list));
head->next = NULL;
head->val = INT_MIN;
return head;
}
void printList(list* head)
{
list* curr;
curr = head;
while (curr)
{
printf("%d -> ",curr->val);
curr = curr->next;
}
printf("NULL\n");
}
int createArrayFromList(list *head, int *array){
int i, length, value;
list *node = head;
length = ListSize(head);
array = (int*)calloc(length, sizeof(int));
for(i = 0 ; i < length ; i++){
value = node->val;
if(value == 0){
return EXIT_FAILURE;
}
array[i] = value;
head = head->next;
}
return EXIT_SUCCESS;
}
void freeList(list* head)
{
list* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}