-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.c
120 lines (114 loc) · 2.92 KB
/
numbers.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
#include <stdio.h>
#include "list.h"
void clear_buffer()
{
while ((getchar()) != '\n')
{
};
}
void show_main_menu()
{
printf("Main Menu\n---------\n\n");
printf("(a) add a number to the end of the list\n");
printf("(b) add a number to the start of the list\n");
printf("(c) insert a number at a given position in the list\n");
printf("(d) add a unique item on the list at the end(if it already exists, do not insert)\n");
printf("(e) remove a number from the beginning of the list\n");
printf("(f) remove a number from the end of the list\n");
printf("(g) remove a number from a given position in the list\n");
printf("(h) remove first occurrence of a number\n");
printf("(i) remove all occurrences of a number\n");
printf("(j) clear the whole list\n");
printf("(k) check if a number exists in the list\n");
printf("(l) display the list of numbers\n");
printf("(m) exit\n\n");
}
char read_user_choice(Char_ptr user_choice)
{
printf("Please enter the alphabet of the operation you would like to perform\n");
scanf("%c", user_choice);
clear_buffer();
return *user_choice;
}
int read_input_values(Int_ptr number)
{
printf("Enter a number \n");
scanf("%d", number);
clear_buffer();
return *number;
}
void perform_required_operation(List_ptr list, char user_choice)
{
int number, position;
Status status;
switch (user_choice)
{
case 'a':
number = read_input_values(&number);
status = add_to_end(list, number);
break;
case 'b':
number = read_input_values(&number);
status = add_to_start(list, number);
break;
case 'c':
number = read_input_values(&number);
position = read_input_values(&position);
status = insert_at(list, number, position);
break;
case 'd':
number = read_input_values(&number);
status = add_unique(list, number);
break;
case 'e':
status = remove_from_start(list);
break;
case 'f':
status = remove_from_end(list);
break;
case 'g':
position = read_input_values(&position);
status = remove_at(list, position);
break;
case 'h':
number = read_input_values(&number);
status = remove_first_occurrence(list, number);
break;
case 'i':
number = read_input_values(&number);
status = remove_all_occurrences(list, number);
break;
case 'j':
status = clear_list(list);
break;
case 'k':
number = read_input_values(&number);
status = is_number_present(list, number);
break;
case 'l':
display(list);
break;
default:
break;
}
char *message = status == 0 ? "failed" : "succeeded";
printf("Operation %s\n", message);
}
int main(void)
{
List_ptr list = create_list();
if (list == NULL)
{
printf("Memory not alloted!\n");
return 1;
}
char user_choice;
do
{
show_main_menu();
user_choice = read_user_choice(&user_choice);
perform_required_operation(list, user_choice);
} while (user_choice != 'm');
destroy_list(list);
return 0;
}