Week 9 Code Examples
// Week 8 Tuesday Lecture
// Modified Week 9 Monday Lecture and Week 9 Tuesday Lecture
// Delete the first node in the linked list
// Delete all nodes in a linked list
// Search and delete in a linked list
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *create_node(int data, struct node *next);
void print_list(struct node *head);
struct node *insert_at_tail(struct node *head, int data);
//Deletion related functions
struct node *delete_first(struct node *head);
void delete_all(struct node *head);
struct node *search_and_delete(struct node *head, int value);
struct node *search_and_delete_all(struct node *head, int value);
int main(void) {
//create a node with data value i using function
//add node to tail/end of list
struct node *head = NULL;
print_list(head);
printf("Search and delete from empty list\n");
head = search_and_delete_all(head, 99);
print_list(head);
for (int i = 0; i < 10; i++) {
head = insert_at_tail(head, 0);
}
for (int i = 10; i > 0; i--) {
head = insert_at_tail(head, 0);
}
print_list(head);
// Between nodes
printf("Search and delete 4\n");
head = search_and_delete_all(head, 4);
print_list(head);
//Not in list
printf("Search and delete 99\n");
head = search_and_delete_all(head,99);
print_list(head);
//Last element in the list
printf("Search and delete 9\n");
head = search_and_delete_all(head,9);
print_list(head);
//First element in list
printf("Search and delete 0\n");
head = search_and_delete_all(head, 0);
print_list(head);
// printf("Delete first element\n");
// for(int i = 0; i <= 10; i++) {
// head = delete_first(head);
// print_list(head);
// }
delete_all(head);
// check for memory leaks
return 0;
}
struct node *create_node(int data, struct node *next) {
struct node *new_node = malloc(sizeof(struct node));
if (new_node == NULL) {
return NULL;
}
new_node->data = data;
new_node->next = next;
return new_node;
}
void print_list(struct node *head) {
printf("list:\n");
struct node *curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct node *insert_at_tail(struct node *head, int data) {
struct node *new_node = create_node(data, NULL);
struct node *current = head;
// Case for when the list is empty
if (head == NULL) {
head = new_node;
} else {
// Find the last node in the list
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
return head;
}
// Deletes and frees the memory of the first node in the list
// returns the head of the list
struct node *delete_first(struct node *head) {
if (head != NULL) {
struct node *tmp = head;
head = head->next;
free(tmp);
}
return head;
}
// frees memory of all nodes in the given list
void delete_all(struct node *head) {
struct node *current = head;
while (current != NULL) {
head = head->next;
free(current);
current = head;
}
}
// searches for a value in the list, deletes it and frees
// the memory.
// returns the head of the list
// Only deletes the first occurrence of a value in the list
struct node *search_and_delete_original(struct node *head, int value) {
struct node *prev = NULL;
struct node *curr = head;
// Find node to delete
while (curr != NULL && curr->data != value) {
prev = curr;
curr = curr->next;
}
// Make sure curr is not NULL (ie make sure we found a node to delete)
if (curr != NULL) {
if (prev == NULL) {
head = head->next;
} else {
prev->next = curr->next;
}
free(curr);
}
return head;
}
// searches for a value in the list, deletes it and frees
// the memory.
// returns the head of the list
// Only deletes the first occurrence of a value in the list
struct node *search_and_delete(struct node *head, int value) {
if (head != NULL) {
if (head->data == value) {
struct node *tmp = head;
head = head->next;
free(tmp);
} else {
struct node *curr = head;
// Find node to delete
// check the next node exists and
// look at the data in the next node
while (curr->next != NULL && curr->next->data != value) {
curr = curr->next;
}
//TODO COMPLETE AND DEBUG
struct node *tmp = curr->next;
if (tmp != NULL) {
curr->next = tmp->next;
free(tmp);
}
}
}
return head;
}
// Modify to delete all occurrences of value, not just the first one.
struct node *search_and_delete_all(struct node *head, int value) {
struct node *prev = NULL;
struct node *curr = head;
// Find node to delete
while (curr != NULL) {
if (curr->data == value) {
if (prev == NULL) {
head = head->next;
free(curr);
curr = head;
} else {
prev->next = curr->next;
free(curr);
curr = prev->next;
}
} else {
prev = curr;
curr = curr->next;
}
}
return head;
}
// email_management_system.c
// Tammy Zhong
// Implementation file for email management system functions
// Modified by Angela Finlayson
// (TODO)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "email_management_system.h"
// Function prototypes
// ADD YOUR OWN FUNCTION PROTOTYPES HERE
// Function definitions
// Creates a new empty email folder
//(TODO)
struct folder *create_folder(char name[]) {
// TODO: implement this function
printf("create_folder not yet implemented.\n");
exit(1);
}
// Add a new email to the folder at the head
// Similar to previous lecture code insert_head
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[],
char subject[], double size, enum email_type type, int priority) {
// TODO: implement this function
printf("insert_email_at_head not yet implemented.\n");
exit(1);
}
// Given a string, look for email where the subject is this string
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]) {
// TODO: implement this function
printf("search_email not yet implemented.\n");
exit(1);
}
// Removes all emails from a folder
// clears and delete associated memory of folder and emails in it
void clear_folder(struct folder *email_folder) {
// TODO: implement this function
printf("clear_folder not yet implemented.\n");
exit(1);
}
// Remove email(s) with certain priority (e.g. filter out low priority emails)
void delete_email_of_priority(struct folder *email_folder, int priority) {
// TODO: implement this function
printf("delete_email_of_priority not yet implemented.\n");
exit(1);
}
// Append emails from folder_2 after folder_1 and returns them in the one folder
// Assumption made here assumes not same subject email in folder_1 and folder_2
struct folder *merge_folders(char merged_folder_name[],
struct folder *folder_1, struct folder *folder_2) {
// TODO: implement this function
printf("merge_folders not yet implemented.\n");
exit(1);
}
// Split emails in a given folder into 3 folders based on email type
// keeping original ordering
// assumption: inbox, sent and draft are folders already malloc-ed and initialised
void split_folder(struct folder *given_folder,
struct folder *inbox, struct folder *sent, struct folder *draft) {
// TODO: implement this function
printf("split_folder not yet implemented.\n");
exit(1);
}
// returns number of emails in a given folder
// (EXTENSION TODO: Improve this)
int count_emails(struct folder *email_folder) {
int counter = 0;
struct email *current = email_folder->emails;
while (current != NULL) {
counter++;
current = current->next;
}
return counter;
}
// Your functions here (include function comments):
////////////////////////////////////////////////////////////////////////////////
// Provided Code //
////////////////////////////////////////////////////////////////////////////////
// Print/display all emails in folder
// Similar to previous lecture code print_list
void print_emails(struct folder *email_folder) {
printf("Folder Name: %s\n", email_folder->name);
struct email *current = email_folder->emails;
if (current == NULL) {
printf("There are no emails in this folder.\n");
}
while (current != NULL) {
printf("---\n");
print_single_email(current);
current = current->next;
}
}
// Prints out details of a single email
void print_single_email(struct email *email) {
if (email == NULL) {
printf("Email is NULL.\n");
return;
}
printf("Sender: %s\n", email->sender);
printf("Subject: %s\n", email->subject);
printf("Size: %lfmb\n", email->size);
printf("Type: ");
if (email->type == RECEIVED) {
printf("RECEIVED\n");
} else if (email->type == SENT) {
printf("SENT\n");
} else if (email->type == DRAFT) {
printf("DRAFT\n");
} else {
printf("UNKNOWN\n");
}
printf("Priority: ");
if (email->priority == HIGH_PRIORITY) {
printf("HIGH_PRIORITY\n");
} else if (email->priority == NORMAL_PRIORITY) {
printf("NORMAL_PRIORITY\n");
} else if (email->priority == LOW_PRIORITY) {
printf("LOW_PRIORITY\n");
} else {
printf("UNKNOWN\n");
}
}
// email_management_system.h
// Tammy Zhong
// Header file for email management system functions
// Modified by Angela Finlayson
// (Provided)
// #defines
#define MAX_LEN 100
// enum definition
enum email_type {
DRAFT,
RECEIVED,
SENT,
};
enum priority_type {
LOW_PRIORITY,
NORMAL_PRIORITY,
HIGH_PRIORITY
};
// struct definitions
struct folder {
char name[MAX_LEN];
//int num_emails; // to use later :)
struct email *emails;
};
struct email {
char sender[MAX_LEN];
char subject[MAX_LEN];
double size;
enum email_type type;
enum priority_type priority;
struct email *next;
};
// Creates a new empty email folder
// (TODO)
struct folder *create_folder(char name[]);
// Add a new email to the folder at the head
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[],
char subject[], double size, enum email_type type, int priority);
// Given a string, look for email where the subject is this string
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]);
// Remove email(s) with certain priority (e.g. filter out low priority emails)
// (TODO)
void delete_email_of_priority(struct folder *email_folder, int priority);
// Append emails from folder_2 after folder_1 and returns them in the one folder
// (TODO)
struct folder *merge_folders(char merged_folder_name[],
struct folder *folder_1, struct folder *folder_2);
// Split emails in a given folder into 3 folders based on email type
// keeping original ordering
// (TODO)
void split_folder(struct folder *given_folder,
struct folder *inbox, struct folder *sent, struct folder *draft);
// Removes all emails from a folder
// (TODO)
void clear_folder(struct folder *email_folder); // leak-check demo
// Returns number of emails in a given folder
// (PROVIDED - TODO CAN BE IMPROVED)
int count_emails(struct folder *email_folder);
// CHALLENGE at home for fun: Sort emails in folder based on size
// (slightly out of scope of COMP1511 - touches on sorting algorithms)
// (TODO)
void sort_emails(struct folder *email_folder);
// Prints out details of a single email
// (PROVIDED)
void print_single_email(struct email *email);
// Print/display all emails in folder
// (PROVIDED)
void print_emails(struct folder *email_folder);
// main.c
// Angela Finlayson
// Uses the email management system to allow a user to create a folder
// and and emails (with not content) to it.
// Only works for 1 folder...
// (Provided)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "email_management_system.h"
void print_instructions(void);
void handle_search(struct folder *current_folder);
void handle_add_email(struct folder *current_folder);
void trim_newline(char str[]);
int main(void) {
struct folder *current_folder = create_folder("inbox");
char command;
print_instructions();
printf("\nEnter command: ");
while (scanf(" %c", &command) == 1 && command != 'q') {
if (command == 'a') {
handle_add_email(current_folder);
} else if (command == 'p') {
print_emails(current_folder);
} else if (command == 'n') {
printf("Number of emails: %d\n", count_emails(current_folder));
} else if (command == 's') {
handle_search(current_folder);
} else {
printf("Unknown command. Try again.\n");
}
printf("\nEnter command: ");
}
printf("Exiting\n");
clear_folder(current_folder);
return 0;
}
// Prints instructions to users
void print_instructions(void) {
printf("Email Manager - Command Interface\n");
printf("Commands:\n");
printf("a - Add a new email to the folder\n");
printf("p - Print all emails in the folder\n");
printf("n - Count emails in the folder\n");
printf("s - Search for an email by subject\n");
printf("q - Quit\n");
}
// Handles adding a new email to the folder
// Assumes no error checking needed
void handle_add_email(struct folder *current_folder) {
char sender[MAX_LEN] = "";
char subject[MAX_LEN]= "";
double size;
int priority, type;
// Remove whitespace from buffer
int whitespace_ch;
while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}
printf("Enter sender: ");
fgets(sender, MAX_LEN, stdin);
trim_newline(sender);
printf("Enter subject: ");
fgets(subject, MAX_LEN, stdin);
trim_newline(subject);
printf("Enter size (in MB): ");
scanf("%lf", &size);
printf("Enter email type (0 for DRAFT, 1 for RECEIVED, 2 for SENT): ");
scanf("%d", &type);
printf("Enter priority (0 for LOW, 1 for NORMAL, 3 for HIGH): ");
scanf("%d", &priority);
insert_email_at_head(current_folder, sender, subject, size, type, priority);
printf("Email added to folder '%s'.\n", current_folder->name);
}
// Handles searching for an email by subject in the folder
void handle_search(struct folder *current_folder) {
char subject[MAX_LEN];
// Remove whitespace from buffer
int whitespace_ch;
while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}
printf("Enter subject to search: ");
fgets(subject, MAX_LEN, stdin);
trim_newline(subject);
struct email *found_email = search_email(current_folder, subject);
if (found_email != NULL) {
printf("Email found:\n");
print_single_email(found_email);
} else {
printf("No email with subject '%s' found.\n", subject);
}
}
// removes trailing newline character
void trim_newline(char str[]) {
int len = strlen(str);
if (len > 0 && str[len - 1] == '\n') {
str[len - 1] = '\0';
}
}
// main.c
// Tammy Zhong
// Program to test email management system functions
// Modified by Angela Finlayson 9/11/2025
// (Provided)
#include <stdio.h>
#include <stdlib.h>
#include "email_management_system.h"
int main(void) {
// STAGE 1
printf("*****1. Creating Folder 1...*****\n");
struct folder *folder_1 = create_folder("Folder 1");
printf("\n*****2. Number of emails in Empty Folder 1:*****\n");
printf("%d\n", count_emails(folder_1));
printf("\n*****3. Printing Empty Folder 1...*****\n");
print_emails(folder_1);
printf("\n*****4. Inserting emails in Empty Folder 1 *****\n");
insert_email_at_head(folder_1, "Jon", "AAA", 10.2, RECEIVED, HIGH_PRIORITY);
insert_email_at_head(folder_1, "Tim", "BBB", 20.44, DRAFT, NORMAL_PRIORITY);
insert_email_at_head(folder_1, "Brianna", "CCC", 150.0, SENT, LOW_PRIORITY);
insert_email_at_head(folder_1, "Tammy", "DDD", 90.2, SENT, NORMAL_PRIORITY);
insert_email_at_head(folder_1, "Tammy", "EEE", 90.0, DRAFT, LOW_PRIORITY);
printf("\n*****5. Printing Folder 1...*****\n");
print_emails(folder_1);
printf("\n*****6. Number of emails in Folder 1: *****\n");
printf("%d\n", count_emails(folder_1));
printf("\n*****7. Look for email with subject 'AAA'...*****\n");
struct email *email_found = search_email(folder_1, "AAA");
printf("\n*****8. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****9. Look for email with subject 'DDD'...*****\n");
email_found = search_email(folder_1, "DDD");
printf("\n*****10. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****11. Look for email with subject 'EEE'...*****\n");
email_found = search_email(folder_1, "EEE");
printf("\n*****12. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****13. Look for email with subject 'Holiday'...*****\n");
email_found = search_email(folder_1, "Holiday");
printf("\n*****14. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****15. Creating Folder 2... and inserting emails...*****\n");
struct folder *folder_2 = create_folder("Folder 2");
insert_email_at_head(folder_2, "Tammy", "E", 50.5, SENT, LOW_PRIORITY);
insert_email_at_head(folder_2, "Sofia", "F", 5.97, RECEIVED, HIGH_PRIORITY);
insert_email_at_head(folder_2, "Grace", "G", 30.28, SENT, NORMAL_PRIORITY);
insert_email_at_head(folder_2, "Andrew", "H", 5.5, RECEIVED, LOW_PRIORITY);
insert_email_at_head(folder_2, "Holly", "I", 25.25, RECEIVED, LOW_PRIORITY);
insert_email_at_head(folder_2, "Ibby", "J", 15.25, RECEIVED, NORMAL_PRIORITY);
printf("\n*****16. Printing Folder 2...*****\n");
print_emails(folder_2);
//STAGE 2
//printf("\n*****17. Deleting LOW_PRIORITY emails from Folder 2...*****\n");
//delete_email_of_priority(folder_2, LOW_PRIORITY);
//printf("\n*****18. Printing Folder 2...*****\n");
//print_emails(folder_2);
/*
printf("\n*****19. Merging Folder 1 and Folder 2...*****\n");
struct folder *merged_folder = merge_folders("Merged Folder", folder_1,
folder_2);
printf("\n*****20. Printing merged folder...*****\n");
print_emails(merged_folder);
printf("\n*****21. Split merged folder into inbox, sent, draft...*****\n");
struct folder *inbox = create_folder("inbox");
struct folder *sent = create_folder("sent");
struct folder *draft = create_folder("draft");
split_folder(merged_folder, inbox, sent, draft);
printf("\n*****22. Printing inbox...*****\n");
print_emails(inbox);
printf("\n*****23. Printing sent...*****\n");
print_emails(sent);
printf("\n*****24. Printing draft...*****\n");
print_emails(draft);
*/
// STAGE 1: comment this section out once you implement merge
printf("\n*****Clearing stage 1 folders folder_1 and folder_2...*****\n");
clear_folder(folder_1);
clear_folder(folder_2);
// STAGE 2 merge: comment this section out once you implement split
//printf("\n*****Clearing stage 2 merged_folder...*****\n");
//clear_folder(merged_folder);
// STAGE 2
//printf("\n*****Clearing inbox, sent, draft (freeing all memory)...*****\n");
//clear_folder(inbox);
//clear_folder(sent);
//clear_folder(draft);
return 0;
}
// email_management_system.c
// Tammy Zhong
// Implementation file for email management system functions
// Modified by Angela Finlayson
// (TODO)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "email_management_system.h"
// Function prototypes
// ADD YOUR OWN FUNCTION PROTOTYPES HERE
struct email *create_email(char sender[], char subject[], double size,
enum email_type type, int priority, struct email *next);
// Function definitions
// Creates a new empty email folder
//(TODO)
struct folder *create_folder(char name[]) {
struct folder *new_folder = malloc(sizeof(struct folder));
strcpy(new_folder->name, name);
new_folder->emails = NULL;
return new_folder;
}
// Add a new email to the folder at the head
// Similar to previous lecture code insert_head
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[],
char subject[], double size, enum email_type type, int priority) {
struct email *new_email = create_email(sender, subject, size, type,
priority, email_folder->emails);
email_folder->emails = new_email;
}
// Given a string, look for email where the subject is this string
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]) {
struct email *curr = email_folder->emails;
while (curr != NULL && strcmp(curr->subject, subject) != 0) {
curr = curr->next;
}
return curr;
}
// Removes all emails from a folder
// clears and delete associated memory of folder and emails in it
void clear_folder(struct folder *email_folder) {
struct email *current = email_folder->emails;
while (current != NULL) {
email_folder->emails = email_folder->emails->next;
free(current);
current = email_folder->emails;
}
free(email_folder);
}
// Remove email(s) with certain priority (e.g. filter out low priority emails)
void delete_email_of_priority(struct folder *email_folder, int priority) {
struct email *prev = NULL;
struct email *curr = email_folder->emails;
// Find node to delete
while (curr != NULL) {
if (curr->priority == priority) {
if (prev == NULL) {
email_folder->emails = email_folder->emails->next;
free(curr);
curr = email_folder->emails;
} else {
prev->next = curr->next;
free(curr);
curr = prev->next;
}
} else {
prev = curr;
curr = curr->next;
}
}
}
// Append emails from folder_2 after folder_1 and returns them in the one folder
// Assumption made here assumes not same subject email in folder_1 and folder_2
struct folder *merge_folders(char merged_folder_name[],
struct folder *folder_1, struct folder *folder_2) {
struct folder *merged_folder = create_folder(merged_folder_name);
merged_folder->emails = folder_1->emails;
if (folder_1->emails == NULL) {
merged_folder->emails = folder_2->emails;
} else {
//Find the last node in folder_1 emails
struct email *curr = folder_1->emails;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = folder_2->emails;
}
free(folder_1);
free(folder_2);
return merged_folder;
}
// Split emails in a given folder into 3 folders based on email type
// keeping original ordering
// assumption: inbox, sent and draft are folders already malloc-ed and initialised
void split_folder(struct folder *given_folder,
struct folder *inbox, struct folder *sent, struct folder *draft) {
// TODO: implement this function
printf("split_folder not yet implemented.\n");
exit(1);
}
// returns number of emails in a given folder
// (EXTENSION TODO: Improve this)
int count_emails(struct folder *email_folder) {
int counter = 0;
struct email *current = email_folder->emails;
while (current != NULL) {
counter++;
current = current->next;
}
return counter;
}
// Your functions here (include function comments):
struct email *create_email(char sender[], char subject[], double size,
enum email_type type, int priority, struct email *next) {
struct email *new_email = malloc(sizeof(struct email));
strcpy(new_email->sender, sender);
strcpy(new_email->subject, subject);
new_email->size = size;
new_email->type = type;
new_email->priority = priority;
new_email->next = next;
return new_email;
}
////////////////////////////////////////////////////////////////////////////////
// Provided Code //
////////////////////////////////////////////////////////////////////////////////
// Print/display all emails in folder
// Similar to previous lecture code print_list
void print_emails(struct folder *email_folder) {
printf("Folder Name: %s\n", email_folder->name);
struct email *current = email_folder->emails;
if (current == NULL) {
printf("There are no emails in this folder.\n");
}
while (current != NULL) {
printf("---\n");
print_single_email(current);
current = current->next;
}
}
// Prints out details of a single email
void print_single_email(struct email *email) {
if (email == NULL) {
printf("Email is NULL.\n");
return;
}
printf("Sender: %s\n", email->sender);
printf("Subject: %s\n", email->subject);
printf("Size: %lfmb\n", email->size);
printf("Type: ");
if (email->type == RECEIVED) {
printf("RECEIVED\n");
} else if (email->type == SENT) {
printf("SENT\n");
} else if (email->type == DRAFT) {
printf("DRAFT\n");
} else {
printf("UNKNOWN\n");
}
printf("Priority: ");
if (email->priority == HIGH_PRIORITY) {
printf("HIGH_PRIORITY\n");
} else if (email->priority == NORMAL_PRIORITY) {
printf("NORMAL_PRIORITY\n");
} else if (email->priority == LOW_PRIORITY) {
printf("LOW_PRIORITY\n");
} else {
printf("UNKNOWN\n");
}
}
// email_management_system.h
// Tammy Zhong
// Header file for email management system functions
// Modified by Angela Finlayson
// (Provided)
// #defines
#define MAX_LEN 100
// enum definition
enum email_type {
DRAFT,
RECEIVED,
SENT,
};
enum priority_type {
LOW_PRIORITY,
NORMAL_PRIORITY,
HIGH_PRIORITY
};
// struct definitions
struct folder {
char name[MAX_LEN];
//int num_emails; // to use later :)
struct email *emails;
};
struct email {
char sender[MAX_LEN];
char subject[MAX_LEN];
double size;
enum email_type type;
enum priority_type priority;
struct email *next;
};
// Creates a new empty email folder
// (TODO)
struct folder *create_folder(char name[]);
// Add a new email to the folder at the head
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[],
char subject[], double size, enum email_type type, int priority);
// Given a string, look for email where the subject is this string
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]);
// Remove email(s) with certain priority (e.g. filter out low priority emails)
// (TODO)
void delete_email_of_priority(struct folder *email_folder, int priority);
// Append emails from folder_2 after folder_1 and returns them in the one folder
// (TODO)
struct folder *merge_folders(char merged_folder_name[],
struct folder *folder_1, struct folder *folder_2);
// Split emails in a given folder into 3 folders based on email type
// keeping original ordering
// (TODO)
void split_folder(struct folder *given_folder,
struct folder *inbox, struct folder *sent, struct folder *draft);
// Removes all emails from a folder
// (TODO)
void clear_folder(struct folder *email_folder); // leak-check demo
// Returns number of emails in a given folder
// (PROVIDED - TODO CAN BE IMPROVED)
int count_emails(struct folder *email_folder);
// CHALLENGE at home for fun: Sort emails in folder based on size
// (slightly out of scope of COMP1511 - touches on sorting algorithms)
// (TODO)
void sort_emails(struct folder *email_folder);
// Prints out details of a single email
// (PROVIDED)
void print_single_email(struct email *email);
// Print/display all emails in folder
// (PROVIDED)
void print_emails(struct folder *email_folder);
// main.c
// Angela Finlayson
// Uses the email management system to allow a user to create a folder
// and and emails (with not content) to it.
// Only works for 1 folder...
// (Provided)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "email_management_system.h"
void print_instructions(void);
void handle_search(struct folder *current_folder);
void handle_add_email(struct folder *current_folder);
void trim_newline(char str[]);
int main(void) {
struct folder *current_folder = create_folder("inbox");
char command;
print_instructions();
printf("\nEnter command: ");
while (scanf(" %c", &command) == 1 && command != 'q') {
if (command == 'a') {
handle_add_email(current_folder);
} else if (command == 'p') {
print_emails(current_folder);
} else if (command == 'n') {
printf("Number of emails: %d\n", count_emails(current_folder));
} else if (command == 's') {
handle_search(current_folder);
} else {
printf("Unknown command. Try again.\n");
}
printf("\nEnter command: ");
}
printf("Exiting\n");
clear_folder(current_folder);
return 0;
}
// Prints instructions to users
void print_instructions(void) {
printf("Email Manager - Command Interface\n");
printf("Commands:\n");
printf("a - Add a new email to the folder\n");
printf("p - Print all emails in the folder\n");
printf("n - Count emails in the folder\n");
printf("s - Search for an email by subject\n");
printf("q - Quit\n");
}
// Handles adding a new email to the folder
// Assumes no error checking needed
void handle_add_email(struct folder *current_folder) {
char sender[MAX_LEN] = "";
char subject[MAX_LEN]= "";
double size;
int priority, type;
// Remove whitespace from buffer
int whitespace_ch;
while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}
printf("Enter sender: ");
fgets(sender, MAX_LEN, stdin);
trim_newline(sender);
printf("Enter subject: ");
fgets(subject, MAX_LEN, stdin);
trim_newline(subject);
printf("Enter size (in MB): ");
scanf("%lf", &size);
printf("Enter email type (0 for DRAFT, 1 for RECEIVED, 2 for SENT): ");
scanf("%d", &type);
printf("Enter priority (0 for LOW, 1 for NORMAL, 3 for HIGH): ");
scanf("%d", &priority);
insert_email_at_head(current_folder, sender, subject, size, type, priority);
printf("Email added to folder '%s'.\n", current_folder->name);
}
// Handles searching for an email by subject in the folder
void handle_search(struct folder *current_folder) {
char subject[MAX_LEN];
// Remove whitespace from buffer
int whitespace_ch;
while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}
printf("Enter subject to search: ");
fgets(subject, MAX_LEN, stdin);
trim_newline(subject);
struct email *found_email = search_email(current_folder, subject);
if (found_email != NULL) {
printf("Email found:\n");
print_single_email(found_email);
} else {
printf("No email with subject '%s' found.\n", subject);
}
}
// removes trailing newline character
void trim_newline(char str[]) {
int len = strlen(str);
if (len > 0 && str[len - 1] == '\n') {
str[len - 1] = '\0';
}
}
// main.c
// Tammy Zhong
// Program to test email management system functions
// Modified by Angela Finlayson 9/11/2025
// (Provided)
#include <stdio.h>
#include <stdlib.h>
#include "email_management_system.h"
int main(void) {
// STAGE 1
printf("*****1. Creating Folder 1...*****\n");
struct folder *folder_1 = create_folder("Folder 1");
printf("\n*****2. Number of emails in Empty Folder 1:*****\n");
printf("%d\n", count_emails(folder_1));
printf("\n*****3. Printing Empty Folder 1...*****\n");
print_emails(folder_1);
printf("\n*****4. Inserting emails in Empty Folder 1 *****\n");
insert_email_at_head(folder_1, "Jon", "AAA", 10.2, RECEIVED, HIGH_PRIORITY);
insert_email_at_head(folder_1, "Tim", "BBB", 20.44, DRAFT, NORMAL_PRIORITY);
insert_email_at_head(folder_1, "Brianna", "CCC", 150.0, SENT, LOW_PRIORITY);
insert_email_at_head(folder_1, "Tammy", "DDD", 90.2, SENT, NORMAL_PRIORITY);
insert_email_at_head(folder_1, "Tammy", "EEE", 90.0, DRAFT, LOW_PRIORITY);
printf("\n*****5. Printing Folder 1...*****\n");
print_emails(folder_1);
printf("\n*****6. Number of emails in Folder 1: *****\n");
printf("%d\n", count_emails(folder_1));
printf("\n*****7. Look for email with subject 'AAA'...*****\n");
struct email *email_found = search_email(folder_1, "AAA");
printf("\n*****8. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****9. Look for email with subject 'DDD'...*****\n");
email_found = search_email(folder_1, "DDD");
printf("\n*****10. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****11. Look for email with subject 'EEE'...*****\n");
email_found = search_email(folder_1, "EEE");
printf("\n*****12. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****13. Look for email with subject 'Holiday'...*****\n");
email_found = search_email(folder_1, "Holiday");
printf("\n*****14. Printing email found in Folder 1 ...*****\n");
print_single_email(email_found);
printf("\n*****15. Creating Folder 2... and inserting emails...*****\n");
struct folder *folder_2 = create_folder("Folder 2");
insert_email_at_head(folder_2, "Tammy", "E", 50.5, SENT, LOW_PRIORITY);
insert_email_at_head(folder_2, "Sofia", "F", 5.97, RECEIVED, HIGH_PRIORITY);
insert_email_at_head(folder_2, "Grace", "G", 30.28, SENT, NORMAL_PRIORITY);
insert_email_at_head(folder_2, "Andrew", "H", 5.5, RECEIVED, LOW_PRIORITY);
insert_email_at_head(folder_2, "Holly", "I", 25.25, RECEIVED, LOW_PRIORITY);
insert_email_at_head(folder_2, "Ibby", "J", 15.25, RECEIVED, LOW_PRIORITY);
printf("\n*****16. Printing Folder 2...*****\n");
print_emails(folder_2);
//STAGE 2
printf("\n*****17. Deleting LOW_PRIORITY emails from Folder 2...*****\n");
delete_email_of_priority(folder_2, LOW_PRIORITY);
printf("\n*****18. Printing Folder 2...*****\n");
print_emails(folder_2);
printf("\n*****19. Merging Folder 1 and Folder 2...*****\n");
struct folder *merged_folder = merge_folders("Merged Folder", folder_1,
folder_2);
printf("\n*****20. Printing merged folder...*****\n");
print_emails(merged_folder);
/*
printf("\n*****21. Split merged folder into inbox, sent, draft...*****\n");
struct folder *inbox = create_folder("inbox");
struct folder *sent = create_folder("sent");
struct folder *draft = create_folder("draft");
split_folder(merged_folder, inbox, sent, draft);
printf("\n*****22. Printing inbox...*****\n");
print_emails(inbox);
printf("\n*****23. Printing sent...*****\n");
print_emails(sent);
printf("\n*****24. Printing draft...*****\n");
print_emails(draft);
*/
// STAGE 1: comment this section out once you implement merge
// printf("\n*****Clearing stage 1 folders folder_1 and folder_2...*****\n");
// clear_folder(folder_1);
// clear_folder(folder_2);
// STAGE 2 merge: comment this section out once you implement split
printf("\n*****Clearing stage 2 merged_folder...*****\n");
clear_folder(merged_folder);
// STAGE 2
//printf("\n*****Clearing inbox, sent, draft (freeing all memory)...*****\n");
//clear_folder(inbox);
//clear_folder(sent);
//clear_folder(draft);
return 0;
}