#include <stdio.h>
#include "Customer.h"
void Check()
{
printf("HasCustomers: %s\n", HasCustomers() ? "yes" : "no");
DisplayCustomers();
printf("----------------------\n");
}
int main()
{
printf("EMPTY CHECK\n");
Check();
printf("Append 0, 1000, \"Test Person 0\", \"
[email protected]\", 1234567890, \"Country 0\", 20\n");
Customer * customer0 = AppendCustomer(0, 1000, "Test Person 0", "
[email protected]", "1234567890", "Country 0", 20);
Check();
printf("Append 1, 1001, \"Test Person 1\", \"
[email protected]\", 1234567891, \"Country 1\", 21\n");
Customer * customer1 = AppendCustomer(1, 1001, "Test Person 1", "
[email protected]", "1234567891", "Country 1", 21);
Check();
printf("Append 2, 1002, \"Test Person 2\", \"
[email protected]\", 1234567892, \"Country 2\", 22\n");
Customer * customer2 = AppendCustomer(2, 1002, "Test Person 2", "
[email protected]", "1234567892", "Country 2", 22);
Check();
printf("Find 0\n");
Customer * found = FindCustomer(0);
printf("Find: %p\n", found);
found->name[0] = '?';
Check();
printf("Insert 3 after 0: 3, 1003, \"Test Person 3\", \"
[email protected]\", 1234567893, \"Country 3\", 23\n");
Customer * customer3 = InsertCustomerAfter(customer0, 3, 1003, "Test Person 3", "
[email protected]", "1234567893", "Country 3", 23);
Check();
printf("DELETE 0\n");
DeleteCustomer(customer0);
Check();
printf("DELETE 2\n");
DeleteCustomer(customer2);
Check();
printf("DELETE 1\n");
DeleteCustomer(customer1);
Check();
printf("DELETE 3\n");
DeleteCustomer(customer3);
Check();
return 0;
}
#include "Customer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static Customer * head = NULL;
static Customer * tail = NULL;
Customer * CreateCustomer(uint32_t customerCode, uint32_t tin, char name[MAX_NAME_LENGTH], char email[MAX_EMAIL_LENGTH], char phone[MAX_PHONE_LENGTH], char country[MAX_COUNTRY_LENGTH], uint8_t age, Customer * next)
{
Customer * customer = (Customer *)malloc(sizeof(Customer));
customer->customerCode = customerCode;
customer->tin = tin;
strcpy(customer->name, name);
strcpy(customer->email, email);
strcpy(customer->phone, phone);
strcpy(customer->country, country);
customer->age = age;
customer->next = next;
return customer;
}
Customer * InsertCustomerAfter(Customer * customer, uint32_t customerCode, uint32_t tin, char name[MAX_NAME_LENGTH], char email[MAX_EMAIL_LENGTH], char phone[MAX_PHONE_LENGTH], char country[MAX_COUNTRY_LENGTH], uint8_t age)
{
Customer * newCustomer = CreateCustomer(customerCode, tin, name, email, phone, country, age, customer->next);
customer->next = newCustomer;
return newCustomer;
}
Customer * AppendCustomer(uint32_t customerCode, uint32_t tin, char name[MAX_NAME_LENGTH], char email[MAX_EMAIL_LENGTH], char phone[MAX_PHONE_LENGTH], char country[MAX_COUNTRY_LENGTH], uint8_t age)
{
Customer * customer = CreateCustomer(customerCode, tin, name, email, phone, country, age, NULL);
if (tail == NULL)
{
head = tail = customer;
}
else
{
tail->next = customer;
tail = customer;
}
return customer;
}
bool DeleteCustomer(Customer * customerToDelete)
{
Customer * previous = NULL;
for (Customer * customer = head; customer != NULL; customer = customer->next)
{
if (customer == customerToDelete)
{
if (previous != NULL)
previous->next = customer->next;
else
head = customer->next;
if (tail == customer)
tail = previous;
free(customer);
return true;
}
if (previous == NULL)
previous = head;
else
previous = previous->next;
}
return false;
}
bool HasCustomers() { return head != NULL; }
void DisplayCustomer(const Customer * customer)
{
printf("CustomerCode: %d\n", customer->customerCode);
printf("TIN: %d\n", customer->tin);
printf("Name: %s\n", customer->name);
printf("E-mail: %s\n", customer->email);
printf("Phone: %s\n", customer->phone);
printf("Country: %s\n", customer->country);
printf("Age: %hhu\n", customer->age);
printf("\n");
}
void DisplayCustomers()
{
if (head == NULL)
{
printf("No customer stored.\n\n");
return;
}
printf("Head:\n");
DisplayCustomer(head);
printf("Tail:\n");
DisplayCustomer(tail);
printf("Customers:\n");
for (Customer * customer = head; customer != NULL; customer = customer->next)
DisplayCustomer(customer);
}
Customer * FindCustomer(uint32_t customerCode)
{
for (Customer * customer = head; customer != NULL; customer = customer->next)
{
if (customer->customerCode == customerCode)
return customer;
}
return NULL;
}
#pragma once
#include <stdbool.h>
#include <stdint.h>
#define MAX_NAME_LENGTH 50
#define MAX_EMAIL_LENGTH 50
#define MAX_COUNTRY_LENGTH 30
#define MAX_PHONE_LENGTH 13
typedef struct CustomerElement {
struct CustomerElement * next;
uint32_t customerCode;
uint32_t tin;
char name[MAX_NAME_LENGTH];
char email[MAX_EMAIL_LENGTH];
char phone[MAX_PHONE_LENGTH];
char country[MAX_COUNTRY_LENGTH];
uint8_t age;
} Customer;
Customer * InsertCustomerAfter(Customer * customer, uint32_t customerCode, uint32_t tin, char name[MAX_NAME_LENGTH], char email[MAX_EMAIL_LENGTH], char phone[MAX_PHONE_LENGTH], char country[MAX_COUNTRY_LENGTH], uint8_t age); // Insert a new customer after the existing 'customer'
Customer * AppendCustomer(uint32_t customerCode, uint32_t tin, char name[MAX_NAME_LENGTH], char email[MAX_EMAIL_LENGTH], char phone[MAX_PHONE_LENGTH], char country[MAX_COUNTRY_LENGTH], uint8_t age); // Append a new customer at the end of the list
bool DeleteCustomer(Customer * customerToDelete); // Delete an existing customer
bool HasCustomers(); // Indicates whether the are any customers in the list
void DisplayCustomers(); // Lists all the customers
Customer * FindCustomer(uint32_t customerCode); // Find a customer based on customer code