-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly_linked_list.c
executable file
·185 lines (159 loc) · 4.47 KB
/
singly_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
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
#include <stdio.h>
#include <stdlib.h>
struct element {
int data;
struct element* next;
};
struct singly_linked_list {
struct element* head;
unsigned int size;
};
typedef struct element element;
typedef struct singly_linked_list list;
void
initialize(list* user_list) {
user_list->head = NULL;
user_list->size = 0;
}
element*
new_element(int value) {
element* tmp = malloc(sizeof(element));
tmp->next = NULL;
tmp->data = value;
return tmp;
}
void
print_element(element* user_element) {
printf("%d ", user_element->data);
}
void
add_in_the_end(list* user_list, element* member) {
if (!user_list->size) {
user_list->head = member;
} else {
element* aux = malloc(sizeof(element));
aux = user_list->head;
while (aux->next)
aux = aux->next;
aux->next = member;
}
user_list->size++;
}
void
add_in_the_begin(list* user_list, element* member) {
if (!user_list->size) {
user_list->head = member;
} else {
member->next = user_list->head;
user_list->head = member;
}
user_list->size++;
}
void
insert(list* user_list, element* member, int position) {
if (!user_list->size || position == 1)
add_in_the_begin(user_list, member);
else if (position == user_list->size + 1)
add_in_the_end(user_list, member);
else if (position > user_list->size || position < 1)
printf("Invalid position\n");
else {
element* aux = malloc(sizeof(struct element));
aux = user_list->head;
unsigned int index = 1;
while (++index != position)
aux = aux->next;
member->next = aux->next;
aux->next = member;
user_list->size++;
}
}
void
show(list* user_list) {
printf("Size: %d\n", user_list->size);
if (!user_list->size) return;
element* aux = malloc(sizeof(element));
aux = user_list->head;
while (aux) {
print_element(aux);
aux = aux->next;
}
printf("\n");
}
void
delete(list* user_list, int position) {
if (!user_list->size) {
printf("The list is empty.");
return;
} else if (user_list->size == 1 && position == 1) {
free(user_list->head);
user_list->head = NULL;
} else if (position < 1 || position > user_list->size) {
printf("Invalid position\n");
} else {
element* tmp = malloc(sizeof(element));
if (position == 1) {
tmp = user_list->head->next;
free(user_list->head);
user_list->head = NULL;
user_list->head = tmp;
} else {
unsigned int index = 1;
element* aux = malloc(sizeof(element));
aux = user_list->head;
while (++index != position)
aux = aux->next;
tmp = aux->next->next;
free(aux->next);
aux->next = NULL;
aux->next = tmp;
}
}
user_list->size--;
}
int
main(void) {
int number, choice, position;
list my_list;
initialize(&my_list);
do {
printf("List Menu\n\t1. Add in the begin\n"
"\t2. Add in the end\t3. Insert into\n"
"\t4. Display\t5. Remove\t0. Exit\n");
printf("\nEnter Choice 0─3: ");
scanf("%d", &choice);
switch (choice) {
case 0:
break;
case 1:
printf("\nPlease, enter a number: ");
scanf("%d", &number);
add_in_the_begin(&my_list, new_element(number));
break;
case 2:
printf("\nPlease, enter a number: ");
scanf("%d", &number);
add_in_the_end(&my_list, new_element(number));
break;
case 3:
printf("\nPlease, enter a number: ");
scanf("%d", &number);
printf("\nPlease, enter the position: ");
scanf("%d", &position);
insert(&my_list, new_element(number), position);
break;
case 4:
printf("\n");
show(&my_list);
break;
case 5:
printf("Please, enter the position: ");
scanf("%d", &position);
delete(&my_list, position);
break;
default:
printf("Invalid choice, please try again.\n\n");
}
} while (choice != 0);
printf("Thank you!\n");
}