-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
226 lines (172 loc) · 3.44 KB
/
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <stdlib.h>
struct Node {
int val;
struct Node *next;
};
void print_list (struct Node *);
void push_tail (struct Node *, int);
void push_head (struct Node **, int);
void pop_head (struct Node **);
void pop_tail (struct Node *);
void pop_by_index (struct Node **, int);
void pop_by_value (struct Node **, int);
void print_credits ();
int
main (int argc, char *argv[])
{
struct Node *head = NULL;
head = (struct Node *) malloc (sizeof(struct Node *));
if (head == NULL) {
return 1;
}
puts ("Linked list in C.");
puts ("No arrow operators were used.");
puts ("");
puts ("Demonstration:");
puts ("");
(*head).val = 2;
(*head).next = NULL;
push_tail (head, 3);
push_head (&head, 1);
pop_head (&head);
pop_tail (head);
push_tail (head, 4);
push_tail (head, 5);
push_tail (head, 6);
pop_by_index (&head, 4);
pop_by_index (&head, 2);
pop_by_value (&head, 0);
pop_by_value (&head, 4);
push_head (&head, 9);
pop_by_index (&head, 1);
puts ("Priting list:");
print_list (head);
free(head);
puts ("");
puts ("");
print_credits ();
puts ("");
puts ("");
return 0;
}
void
print_list (struct Node *head)
{
struct Node *current_node = head;
while (current_node != NULL)
{
printf ("%d\n", (*current_node).val);
current_node = (*current_node).next;
}
}
void
push_tail (struct Node *head, int val)
{
struct Node *current_node = head;
struct Node *next_node = NULL;
while ((*current_node).next != NULL)
{
current_node = (*current_node).next;
}
next_node = (struct Node *) malloc (sizeof(struct Node *));
(*current_node).next = next_node;
(*(*current_node).next).val = val;
(*(*current_node).next).next = NULL;
}
void
push_head (struct Node **head, int val)
{
struct Node *next_node = NULL;
next_node = (struct Node *) malloc (sizeof(struct Node *));
(*next_node).val = val;
(*next_node).next = *head;
*head = next_node;
}
void
pop_head (struct Node **head)
{
struct Node *next_node = NULL;
next_node = (**head).next;
free(*head);
*head = next_node;
}
void
pop_tail (struct Node *head)
{
struct Node *current_node = head;
while ( (*(*current_node).next).next != NULL)
{
current_node = (*current_node).next;
}
(*current_node).next = NULL;
free ((*current_node).next);
}
void
pop_by_index (struct Node **head, int index)
{
int i;
struct Node *current_node = *head;
struct Node *temporal_node = NULL;
if (index == 0)
{
pop_head (head);
}
else
{
for (i = 0; i < index - 1; i++)
{
if ((*current_node).next == NULL)
{
break;
}
current_node = (*current_node).next;
}
if ((*current_node).next == NULL)
{
printf ("Error: Cannot delete node at index: %d, since\n", index);
puts ("it may NOT exist.");
}
else
{
temporal_node = (*current_node).next;
(*current_node).next = (*temporal_node).next;
free(temporal_node);
}
}
}
void
pop_by_value (struct Node **head, int val)
{
struct Node *current_node = *head;
struct Node *temporal_node = NULL;
int found = 0;
if ((*current_node).val == val)
{
pop_head (head);
}
else
{
while ((*current_node).next != NULL)
{
if ( (*(*current_node).next).val == val )
{
found = 1;
break;
}
current_node = (*current_node).next;
}
if (found == 1)
{
temporal_node = (*current_node).next;
(*current_node).next = (*temporal_node).next;
free (temporal_node);
}
}
}
void
print_credits ()
{
puts ("Program made by:");
puts ("https://youtube.com/@CodeNoSchool");
}