Header File
This code is a header file (ecommerce.h) for a simple e-commerce system that provides
functionality to manage users, products, and their interactions within the system.
It includes data structures and function prototypes for managing and tracking user-product
interactions like browsing and purchasing.
1. Macros
● #define USER_TABLE_SIZE 100: Specifies the maximum number of users the
system can manage.
● #define PRODUCT_TABLE_SIZE 100: Specifies the maximum number of
products the system can manage.
2. Data Structures
User
typedef struct User{
int id; // Unique identifier for the user.
char name[100]; // Name of the user.
char email[100]; // Email of the user.
} User;
This code is a header file (ecommerce.h) for a simple e-commerce system that provides
functionality to manage users, products, and their interactions within the system. It includes
data structures and function prototypes for managing and tracking user-product interactions
like browsing and purchasing.
Key Components
1. Macros
● #define USER_TABLE_SIZE 100: Specifies the maximum number of users the
system can manage.
● #define PRODUCT_TABLE_SIZE 100: Specifies the maximum number of
products the system can manage.
2. Data Structures
User
Represents a user in the system:
typedef struct User{
int id; // Unique identifier for the user.
char name[100]; // Name of the user.
char email[100]; // Email of the user.
} User;
Product
Represents a product in the system:
typedef struct Product{
int id; // Unique identifier for the product.
char name[100]; // Name of the product.
float price; // Price of the product.
} Product;
Interaction
Represents an interaction between a user and a product:
typedef struct Interaction{
int userId; // ID of the user involved in the interaction.
int productId; // ID of the product involved in the interaction.
int interactionType; // Type of interaction: 0 for browse, 1 for
purchase.
struct Interaction* next; // Pointer to the next interaction
(linked list structure).
} Interaction;
This structure uses a linked list to store multiple interactions, enabling dynamic growth.
ECommerceContext
The main structure that holds the entire system's data:
typedef struct ECommerceContext{
User* userTable[USER_TABLE_SIZE]; // Array of pointers to User structures.
Product* productTable[PRODUCT_TABLE_SIZE]; // Array of pointers to Product structures.
Interaction* interactionGraph; // Linked list of all interactions.
} ECommerceContext;
This structure is the central context of the system, storing all users, products, and
interactions.
3. Function Prototypes
These are declarations of functions that will implement various functionalities of the
e-commerce system:
● Initialization
○ void initECommerceContext(ECommerceContext* context);
Initializes an ECommerceContext instance, setting up tables and the
interaction graph.
● User Management
○ void addUser(ECommerceContext* context, int id, char*
name, char* email); Adds a user to the userTable with the given
details.
○ User* getUser(ECommerceContext* context, int userId);
Retrieves a user by their ID.
○ void displayUser(User* user); Displays details of a specific user.
● Product Management
○ void addProduct(ECommerceContext* context, int id, char*
name, float price); Adds a product to the productTable with the
given details.
○ Product* getProduct(ECommerceContext* context, int
productId); Retrieves a product by its ID.
○ void displayProduct(Product* product); Displays details of a
specific product.
● Interaction Tracking
○ void trackInteraction(ECommerceContext* context, int
userId, int productId, int interactionType); Records an
interaction (browse or purchase) between a user and a product.
● Reports and Analysis
○ void generateRecommendations(ECommerceContext* context,
int userId); Generates product recommendations for a user based on
their interactions.
○ void displayBrowsingHistory(ECommerceContext* context,
int userId); Displays the browsing history of a user.
○ void displayUserActivity(ECommerceContext* context, int
userId); Displays all activity (browsing and purchasing) of a user.
Purpose of the Code
This header file defines the foundational components of the e-commerce system. It focuses
on:
1. Managing users and products: Adding, retrieving, and displaying them.
2. Tracking interactions: Logging user activities like browsing or purchasing.
3. Analysis: Generating user-specific recommendations and activity reports.
The implementation details for these functions will be provided in the corresponding .c file.
Function 3
Tracking User Interactions
To track user interactions, I used a linked list (for its dynamic nature) and inserting
interaction between users and products.
What is linked list?
A linked list is a data structure, consists of a sequence of nodes.
Data: The value or information stored in the node.
Pointer (or Link): A reference to the next node in the sequence.
It helps in storing Data dynamically.
Why use linked list?
I wanted it to be flexible enough to handle a growing number of interactions without worrying
about a fixed size or running into issues. I decided to use a linked list, so that it can
dynamically storing data.
Here's how the function works:
Interaction* newInteraction = (Interaction*) malloc(sizeof(Interaction));
1. The first thing I did was created a node newInteraction to represent the interaction.
2. This line creates space in memory for a new interaction node.
3. It uses malloc to ensure the size is appropriate for the Interaction structure,
making it ready to hold the interaction data.
newInteraction->userId = userId;
Stores the userId (the ID of the user performing the interaction).
newInteraction->productId = productId;
Stores the productId (the product involved in the interaction).
newInteraction->interactionType = interactionType;
Stores the interactionType, where 0 means the user browsed the product and 1 means
they purchased it.
newInteraction->next = context->interactionGraph;
Links the new interaction to the current head of the list. This ensures the new interaction
"points" to the previous head.
context->interactionGraph = newInteraction;
Updates the head of the list (context->interactionGraph) to the new interaction. This
makes the new interaction the first item in the list.
I chose a singly linked list because interactions only needed to be stored and retrieved
sequentially.
● I ensured the function could handle multiple types of interactions (browsing and
purchasing) by adding the interactionType field, making the system versatile
for future use.
4. Retrieve User Information
Purpose:
The getUser function retrieves user information from the system based on the user's
unique ID.
Data Structure Used:
A hash table is used to store and retrieve user information efficiently.
In this case:
● Array Implementation: The userTable is an array of pointers to User structures.
● Hash Function: A simple modulo operation (id % USER_TABLE_SIZE) is used to
determine the index for storing or retrieving a user.
How it works:
User* getUser(ECommerceContext* context, int userId) {
1. Function Declaration:
○ The function getUser is designed to retrieve a user from a table.
○ Parameters:
i. ECommerceContext* context: A pointer to a structure
(ECommerceContext) that contains the user table (or similar data).
ii. int userId: An integer representing the unique identifier for the
user we want to retrieve.
○ Return Type: User* indicates the function will return a pointer to a User
object.
2. Hashing:
The id of the user is passed to a hash function:
unsigned int hash(int id, int tableSize) {
return id % tableSize;
○ Indexes must be non-negative: Since array or table indices cannot be
negative, using an unsigned int ensures that the index is always a
non-negative value
○ This function ensures that the user's data is stored in a specific index in the
userTable.
○ The hash function computes a value (the index) within the range [0,
USER_TABLE_SIZE - 1] using userId as input. This ensures that the
index corresponds to a valid position in the hash table.
○ Result: The computed index determines where the user object associated
with the userId is stored in the table.
3. Retrieve the User:
○ The getUser function computes the index of the user using the hash
function.
○ It then directly accesses the user stored at the calculated index in the array.
○ return context->userTable[index]; // Retrieve the user
from the table
○ context->userTable: Refers to the user table stored within the
ECommerceContext structure. This is likely an array or hash table where
user objects are stored.
○ context->userTable[index]: Accesses the user object stored at the
position index in the table.
○ The function returns this user object (a User* pointer) to the caller.
○
Advantages of Hash Table:
● Fast Lookup: Hashing allows near-constant time complexity for accessing a user's
data.
● Simple Implementation: The array-based hash table provides a straightforward and
efficient way to manage fixed-size datasets.
Code Walkthrough:
User* getUser(ECommerceContext*context, int userId) {
unsigned int index = hash(userId, USER_TABLE_SIZE); // Compute
the hash index
return context->userTable[index]; // Retrieve the user from the
table
}
1. Linked List
● Definition: A sequence of nodes where each node contains data and a pointer to the
next node.
● Why Use It?: It allows dynamic growth without worrying about predefined sizes.
Perfect for cases like the interaction log, where the number of interactions is
unpredictable.
2. Hash Table
● Definition: A data structure that maps keys (like user IDs) to values (like user
information) using a hash function.
● Why Use It?: It enables fast and direct access to stored data, making it ideal for
retrieving users or products based on their unique IDs.