Skip to content

Commit

Permalink
Merge pull request #39 from Pasqualecoder/main
Browse files Browse the repository at this point in the history
add 4.c chapter 14
  • Loading branch information
SharafatKarim authored Sep 16, 2023
2 parents 0db8314 + 5f47bf2 commit e79214f
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.out
*.so
/.vscode
/output
/output
Programming in ANSI C (E Balagurusamy) (Z-Library).pdf
148 changes: 148 additions & 0 deletions solutions/Pasquale/14/4.c
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;
}

0 comments on commit e79214f

Please sign in to comment.