0% found this document useful (0 votes)
27 views18 pages

Real Time Application in DSA

real time pplications in dsa

Uploaded by

Saketh Gade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views18 pages

Real Time Application in DSA

real time pplications in dsa

Uploaded by

Saketh Gade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

SRM INSTITUTE OF SCIENCE AND

TECHNOLOGY
FACULTY OF ENGINEERING AND TECHNOLOGY
DEPARTMENT OF DSBS
Contact
Organizer and
Sorter using DLL
and Vector
G Saketh (RA2311056010127)
R Sathvika
(RA2311056010125)
TABLE OF CONTENTS
 Problem statement with well Defined Objective
 Data Structures used to implement the problem
 Able to justify and elaborate the why the particular Data Structure
is used
 Tools used to implement
 Source Code and explanation
 Code output and discussion with time complexity
Problem statement with well Defined
Objective
 Problem Statement: Contact Organizer and Sorter

 Objective:
 The objective of this project is to create a Phone Directory Management System that allows users to efficiently
manage their contact information. The system will provide features for adding, displaying, searching, updating, and
deleting contact entries, as well as displaying upcoming birthdays. The data structure to be used for this system is
a double-linked list for efficient data management.
 The Phone Directory Management System is designed to offer a simple yet efficient solution for managing contact
information. It leverages the power of a double-linked list to provide a practical and customizable tool for users to
organize their contacts effectively. This project offers not only a useful application but also a learning opportunity
for data structure implementation and software development.
Data Structures used to implement
the problem
Double-Linked List Data Structure:
A double-linked list is chosen as the underlying data structure for this project due to its efficiency in insertion, deletion,
and traversal operations. Each contact entry is represented as a node in the list, and each node contains the contact’s
information along with pointers to the next and previous nodes.

Data Structure Used: std::vector


It is a dynamic array-like container.
It automatically resizes to accommodate elements.
Provides efficient random access to elements.
Suitable for scenarios involving frequent insertion and deletion of elements.
Simplifies the code and makes it straightforward to manage a list of contact entries.
 Each element in the vector represents a contact, facilitating access, search, and
sorting of contacts.

 If you were to implement this problem using a double-linked list, you would need
to create a custom data structure to manage the contacts. The double-linked list
would consist of nodes, with each node containing the contact information as
well as pointers to the next and previous nodes.

 While a double-linked list offers advantages in terms of efficient insertion and


deletion operations, it requires more intricate management compared to a
vector, which is a more common choice for this kind of task..
Able to justify and elaborate the why the particular Data
Structure is used
Efficiency in Random Access:
1. Justification: A vector is highly efficient for random access to elements. This is a critical feature in a phone
directory system, where users need to quickly search for and access specific contact entries by name or other
attributes.
2. Elaboration: Contact information is often retrieved by searching for a specific name or browsing through the
list. Vectors allow direct access to any element using an index, which is ideal for these use cases.

Dynamic Resizing:
1. Justification: Vectors automatically resize themselves to accommodate elements, which is essential in a
phone
directory system where contacts can be added or deleted frequently.
2. Elaboration: As users add or remove contacts, the vector adapts by resizing itself. This dynamic resizing
eliminates
the need for manually managing memory allocation and simplifies the code, making it easier to maintain and
understand.
Simplicity and Ease of Use:
1. Justification: Vectors are part of the C++ Standard Library and are widely used in C++ programming. They
provide a
straightforward and user-
2.Elaboration: For a project like a Phone Directory Management System, using a commonly used and well-
documented data structure like std::vector simplifies the code and reduces the learning curve for other developers
who may work on the
project friendly way to manage a list of elements.

Efficiency in Sorting:
1. Justification: Although vectors are not inherently sorted, they can be efficiently sorted using standard library
functions. In the code, the contacts are sorted by name before displaying, ensuring organized and user-friendly
presentation.
2. Elaboration: Sorting is a crucial feature in a phone directory system, and vectors can be sorted efficiently using
the
std::sort algorithm from the C++ Standard Library. This enables contacts to be presented in an organized manner,
enhancing user experience.
Tools to implement
C Programming Language:
C is the primary programming language used for writing the code.
It is a versatile language that offers a rich set of features for system-level programming.
C Compiler:
You would need a C compiler to translate your C code into machine code that the computer can
execute. Common C compilers include GCC (GNU Compiler Collection), Clang, and Microsoft
Visual C.

Integrated Development Environment (IDE):


While you can write C code using a simple text editor and compile it through the command line,
many developers prefer using an integrated development environment for a more streamlined
development process. Some popular C IDEs include:
Visual Studio Code, CodeBlocks etc.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct Contact {
char name[100];
char phoneNumber[20];
struct tm birthday;
};
int compareByName(const void *a, const void *b) {
return strcmp(((struct Contact *)a)->name, ((struct Contact *)b)->name);
}
struct PhoneDirectory {
struct Contact contacts[100];
int contactCount;
};
void addContact(struct PhoneDirectory *directory, const char *name, const char *phoneNumber, const struct tm
*birthday) {
if (directory->contactCount < 100) {
struct Contact newContact;
strcpy(newContact.name, name);
strcpy(newContact.phoneNumber, phoneNumber);
newContact.birthday = *birthday;
directory->contacts[directory->contactCount++] = newContact;
} else {
printf("Phone directory is full. Cannot add more contacts.\n");
}
}
void displayDirectory(const struct PhoneDirectory *directory) {
printf("Phone Directory (Sorted by Name):\n");
qsort(directory->contacts, directory->contactCount, sizeof(struct Contact), compareByName);
for (int i = 0; i < directory->contactCount; ++i) {
struct Contact contact = directory->contacts[i];
printf("Name: %s, Phone: %s, Birthday: %d/%d\n",
contact.name, contact.phoneNumber,
contact.birthday.tm_mon + 1, contact.birthday.tm_mday);
}
}
void displayUpcomingBirthdays(const struct PhoneDirectory *directory) {
time_t now = time(NULL);
struct tm today = *localtime(&now);
printf("Upcoming Birthdays:\n");
for (int i = 0; i < directory->contactCount; ++i) {
struct Contact contact = directory->contacts[i];
if (contact.birthday.tm_mon == today.tm_mon && contact.birthday.tm_mday > today.tm_mday)
{
printf("Name: %s, Birthday: %d/%d\n",
contact.name, contact.birthday.tm_mon + 1, contact.birthday.tm_mday);
}
}
}
void searchContact(const struct PhoneDirectory *directory, const char *name) {
int found = 0;
for (int i = 0; i < directory->contactCount; ++i) {
struct Contact contact = directory->contacts[i];
if (strcmp(contact.name, name) == 0) {
printf("Contact found:\n");
printf("Name: %s, Phone: %s, Birthday: %d/%d\n",
contact.name, contact.phoneNumber,
contact.birthday.tm_mon + 1, contact.birthday.tm_mday);
found = 1;
}
}
if (!found) {
printf("Contact with name '%s' not found.\n", name);
}
}
void editContact(struct PhoneDirectory *directory, const char *name) {
for (int i = 0; i < directory->contactCount; ++i) {
struct Contact *contact = &directory->contacts[i];
if (strcmp(contact->name, name) == 0) {
printf("Editing contact: %s\n", contact->name);
printf("Enter new phone number: ");
fflush(stdin);
fgets(contact->phoneNumber, sizeof(contact->phoneNumber), stdin);
printf("Enter new birthday (MM/DD): ");
char bdayStr[20];
fgets(bdayStr, sizeof(bdayStr), stdin);
sscanf(bdayStr, "%d/%d", &contact->birthday.tm_mon, &contact->birthday.tm_mday);
contact->birthday.tm_mon--; // Adjust for 0-based month
printf("Contact information updated.\n");
return;
}
}
printf("Contact with name '%s' not found.\n", name);
}
void deleteContact(struct PhoneDirectory *directory, const char switch (choice) {
*name) { for (int i = 0; i < directory->contactCount; ++i) { case 1: {
if (strcmp(directory->contacts[i].name, name) == 0) { char name[100], phoneNumber[20];
for (int j = i; j < directory->contactCount - 1; + struct tm birthday;
printf("Enter Name: ");
+j) {
fgets(name, sizeof(name), stdin);
directory->contacts[j] = directory->contacts[j + strtok(name, "\n");
1]; printf("Enter Phone Number: ");
} fgets(phoneNumber, sizeof(phoneNumber),
directory->contactCount--; stdin);
printf("Contact '%s' deleted.\n", name); strtok(phoneNumber, "\n");
return; printf("Enter Birthday (MM/DD): ");
} char bdayStr[20];
} fgets(bdayStr, sizeof(bdayStr), stdin);
printf("Contact '%s' not found.\n", name); scanf(bdayStr, "%d/%d", &birthday.tm_mon,
&birthday.tm_mday);
}
birthday.tm_mon--;
int main() { addContact(&directory, name, phoneNumber, &birthday);
struct PhoneDirectory directory; printf("Contact added.\n");
directory.contactCount = 0; break;
while (1) { }
printf("\nPhone Directory Menu:\n"); case 2:
printf("1. Add Contact\n"); displayDirectory(&directory);
printf("2. Display Directory (Sorted by Name)\n"); break;
printf("3. Display Upcoming Birthdays\n"); case 3:
printf("4. Delete Contact\n"); displayUpcomingBirthdays(&directory);
break;
printf("5. Search for Contact\n");
printf("6. Edit Contact\n");
printf("7. Exit\n");
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); if (choice == 7) {
case 4: {
char nameToDelete[100];
printf("Enter the name of the contact to delete: ");
fgets(nameToDelete, sizeof(nameToDelete), stdin);
strtok(nameToDelete, "\n"); // Remove the newline character.
deleteContact(&directory, nameToDelete);
break;
}
case 5: {
char nameToSearch[100];
printf("Enter the name of the contact to search for: ");
fgets(nameToSearch, sizeof(nameToSearch), stdin);
strtok(nameToSearch, "\n"); // Remove the newline character.
searchContact(&directory, nameToSearch);
break; }
case 6: {
char nameToEdit[100];
printf("Enter the name of the contact to edit: ");
fgets(nameToEdit, sizeof(nameToEdit), stdin);
strtok(nameToEdit, "\n"); // Remove the newline character.
editContact(&directory, nameToEdit);
break; }
default:
printf("Invalid choice. Please try again.\n");
break; }
}
printf("Goodbye!\n");

return 0;
Explanation
1. Contact Class:
•The Contact class represents an individual contact and has the following attributes:
• name: Contact's name.
• Phone Number: Contact's phone number.
• birthday: Contact's birthday, represented as a struct tm.

2. Custom Comparison Function:


•The compare By Name function is a custom comparison function for sorting contacts by name. It is used for sorting the contacts in the
directory.

3. Phone Directory Class:


•The Phone Directory class manages contact entries and provides several methods for interacting with the directory.
Methods in Phone Directory:
•Add Contact: Adds a new contact to the directory with a name, phone number, and birthday.
•Display Directory: Displays the contacts in the directory, sorted by name.
•display upcoming Birthdays: Shows upcoming birthdays of contacts.
•Search Contact: Searches for a contact by name and displays their information if found.
•Edit Contact: Edits the phone number and birthday of an existing contact.
•Delete Contact: Deletes a contact from the directory.
Main Function:

•The main function serves as the user interface and provides a menu for users to interact with the
phone
directory.

How the Program Works:

•The program runs in a loop, displaying a menu with various options.


•Users can choose to add a contact, display the directory (sorted by name), display upcoming
birthdays,
delete a contact, search for a contact, edit a contact, or exit the program.
•Contact data is stored in a vector of Contact objects.
•The code demonstrates the use of std::sort to sort contacts by name before displaying them.
•It also compares contact birthdays to the current date to find upcoming birthdays.
Code Output
Discussion About Time Complexity

•The code's performance largely depends on the number of contacts in the


directory (n).
•Adding a contact is typically an efficient O(1) operation.
•Displaying the directory is efficient for small to moderate numbers of
contacts (O(n * log(n))), but sorting can become slower as the number of
contacts increases.
•Searching, editing, and deleting contacts have linear time complexity (O(n))
but remain relatively efficient unless there's a very large number of contacts.
•The code's design and use of a vector are suitable for a moderate number of
contacts but may have performance issues with very large datasets due to
sorting and linear search complexities
Thank You!!!

You might also like