-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_linear_list.c
executable file
·130 lines (115 loc) · 3.11 KB
/
sequential_linear_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
#include <stdio.h>
#include <stdlib.h>
struct element {
int value;
};
struct seq_list {
struct element** head;
unsigned int size;
};
typedef struct seq_list seq_linear_list;
typedef struct element element;
element*
new_element(int value) {
element* aux = malloc(sizeof(element));
aux->value = value;
return aux;
}
void
show_element(element* member) {
if (member != NULL)
printf("%d\n", member->value);
else
printf("Not used.\n");
}
void
initialize(seq_linear_list* list, unsigned int size) {
list->size = size;
list->head = calloc(size, sizeof(element*));
}
void
show(seq_linear_list* list) {
for (int i = 0; i < list->size; i++) {
show_element(*(list->head + i));
}
printf("\n");
}
void
insert(seq_linear_list* list, element* member, int index) {
if (index < 0 || index > list->size) {
printf("Invalid index!\n");
return;
} else {
*(list->head + index) = member;
}
}
void
delete(seq_linear_list* list, int index) {
if (index < 0 || index > list->size) {
printf("Invalid index!\n");
return;
} else if (*(list->head + index) != NULL) {
// free((*(list->head + index)));
*(list->head + index) = NULL;
}
}
int
search(seq_linear_list* list, int value) {
int i = 0;
for (; i < list->size; i++) {
if (*(list->head + i) != NULL && (*(list->head + i))->value == value) {
printf("It is the %d-th element.\n", i + 1);
return i;
}
}
printf("Not found!\n");
return -1;
}
int
main(int argc, char const *argv[]) {
int number, choice, position;
seq_linear_list my_list;
do {
printf("\t\tLinear Sequential List Menu\n"
"\t1. What is the size of the list? \n"
"\t2. Insert\t3. Remove\n"
"\t4. Display\t5. Search\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);
initialize(&my_list, number);
break;
case 2:
printf("\nPlease, enter a number: ");
scanf("%d", &number);
printf("Please, enter the position: ");
scanf("%d", &position);
insert(&my_list, new_element(number), position - 1);
break;
case 3:
printf("Please, enter the position: ");
scanf("%d", &position);
delete(&my_list, position - 1);
break;
case 4:
printf("\n");
show(&my_list);
break;
case 5:
printf("Please, enter a number: ");
scanf("%d", &number);
search(&my_list, number);
printf("\n");
break;
default:
printf("Invalid choice, please try again.\n\n");
}
} while (choice != 0);
printf("Thank you!\n");
return 0;
}