-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Pasqualecoder/main
add 4.c chapter 14
- Loading branch information
Showing
2 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
*.out | ||
*.so | ||
/.vscode | ||
/output | ||
/output | ||
Programming in ANSI C (E Balagurusamy) (Z-Library).pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
|
||
// ---------- list.h ------------------- | ||
typedef struct customer_tag { | ||
char name[15]; | ||
char phone[15]; | ||
} 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> | ||
|
||
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; | ||
while (copy->next != NULL) { | ||
copy = copy->next; | ||
} | ||
|
||
list last = new_list(); | ||
last->customer = input_customer(); | ||
last->next = NULL; | ||
|
||
copy->next = 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; | ||
} |