-
Notifications
You must be signed in to change notification settings - Fork 16
/
5.c
165 lines (143 loc) · 3.58 KB
/
5.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
/*
only the implementation of the function
void insert_list(list *l);
in the list.c from 4.c file has been changed.
<string.h> has been added into list.c
*/
// ---------- list.h -------------------
#define MAXLEN_STR 15
typedef struct customer_tag {
char name[MAXLEN_STR];
char phone[MAXLEN_STR];
} customer;
void print_customer(customer *c);
customer input_customer();
typedef struct node_tag {
customer customer;
struct node_tag *next;
} node;
typedef node *list;
list new_list();
void print_list(list l);
void insert_list(list *l);
void delete_list(list *l, int index);
// ------------------------------------
// ----------- list.c -----------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_customer(customer *c) {
printf("Name: %s\n", c->name);
printf("phone: %s\n\n", c->phone);
}
customer input_customer() {
customer c;
printf("Insert the name: ");
scanf("%s", c.name);
printf("Insert the phone number: ");
scanf("%s", c.phone);
return c;
}
list new_list() {
return (list) malloc(sizeof(node));
}
void print_list(list l) {
if (l == NULL) {
printf("The customer list is empty\n");
return;
}
for (int i = 1; l != NULL; i++)
{
printf("Customer #%d:\n", i);
print_customer(&l->customer);
l = l->next;
}
}
void insert_list(list *l) {
if (*l == NULL) {
*l = new_list();
(*l)->customer = input_customer();
(*l)->next = NULL;
return;
}
else {
list copy = *l;
list prec = NULL;
//creating the new node
list last = new_list();
last->customer = input_customer();
// find the right spot to place it
// while the left compared name is less than the new customer name
while (copy != NULL && strncmp(copy->customer.name, last->customer.name, MAXLEN_STR) < 0) {
prec = copy;
copy = copy->next;
}
// making connections
last->next = copy;
if (prec != NULL) prec->next = last;
else {
*l = last;
}
}
}
void delete_list(list *l, int index) {
if (index == 0) {
list tmp = *l;
(*l) = (*l)->next;
free(tmp);
printf("User deleted\n");
return;
}
list actual = *l;
list prec;
int i;
for (i = 0; i < index && actual->next != NULL; i++) {
prec = actual;
actual = actual->next;
}
if (i == index) {
prec->next = actual->next;
free(actual);
printf("User deleted\n");
}
else {
printf("User doesn't exist\n");
}
}
// ------------------------------------
// ------------ main.c ----------------
#include <stdio.h>
#include <stdlib.h>
// #include "list.h"
int main() {
list customers = NULL;
int operation;
printf("Welcome\n");
do {
printf("1. Print all customers\n");
printf("2. Add customer\n");
printf("3. Delete customer\n");
printf("0. Exit\n>");
scanf("%d", &operation); getchar();
switch (operation)
{
case 1:
print_list(customers);
break;
case 2:
insert_list(&customers);
break;
case 3:
int index;
printf("Insert the number of the customer to be deleted: ");
scanf("%d", &index); index--;
delete_list(&customers, index);
break;
default:
operation = 0;
break;
}
} while (operation);
printf("Bye\n");
return 0;
}