You are on page 1of 7

#include <iostream>

#include <string>
#include <conio.h>
#include <cstdlib>
#include <iomanip>
using namespace std;

struct node
{
string name;
int age;
char gender;
node* next;
};

node *head = NULL;

void clearScreen()
{
system("cls");
}

bool searchByName(string& nameToSearch)


{
//string nameToSearch;
//cout << "Enter Name to Search: ";
//cin.getline((toSearch));

node *temp = head;


while (temp != NULL)
{
if (temp->name == nameToSearch)
{
// cout << nameToSearch << "present in List ";
return true;
}
else
temp = temp->next;
}
// cout << nameToSearch << "not present in List" << endl;
return false;
}

void searchByAge()
{

void searchByGender()
{

void searchMenu()
{
char choice;
clearScreen();

cout << "1. Search by Name" << endl;


cout << "2. Search by Age" << endl;
cout << "3. Search by Gender" << endl;

cout << endl << "Enter Choice: ";


choice = _getch();

string nameToBeSearched;
switch (choice)
{
case '1':

cout << "Enter name to Search ";


getline(cin, nameToBeSearched);

if (searchByName(nameToBeSearched))
{
cout << nameToBeSearched << " present in List" << endl;
}
else
{
cout << nameToBeSearched << " not present in List" << endl;
}
break;
case '2':
searchByAge();
break;
case '3':
searchByGender();
break;
default:
cout << "Invalid Choice" << endl;
}
}

void createNode(node **nodeToAdd, string &nameToAdd)


{
// cout << endl << "Enter Name ";
// getline(cin, newNode->name);

*nodeToAdd = new node();


(*nodeToAdd)->name = nameToAdd;
cout << "Enter Age: ";
do
{
if (cin.fail() || ((*nodeToAdd)->age < 0))
{
cout << " Invalid Age: Enter Age again " << endl;
cin.clear();
cin.ignore(256, '\n');
}
} while (!(cin >> (*nodeToAdd)->age) || ((*nodeToAdd)->age) < 0);

cout << "Enter gender(m for male, f for female): ";


cin >> (*nodeToAdd)->gender;
cin.ignore(2, '\n');
cin.clear();

(*nodeToAdd)->next = NULL;
}
void add()
{
string name;
cout << endl << "Enter Name: ";
do
{
getline(cin, name);
if (name.empty() || isdigit(name[0]))
cout << "Invalid Name. Enter valid value(A-Z, a-z" << endl;
else
break;
}while(1);

if (searchByName(name))
{
cout << name << " already present in list. Cannot Add" << endl;
return;
}
else
{
cout << " Adding " << " to List" << endl;
node* nodeToAdd = NULL;

createNode(&nodeToAdd, name);

if (head == NULL)
{
head = nodeToAdd;
}
else
{

node *currentNode = head;


node *previousNode = NULL;
while (currentNode != NULL)
{
if (currentNode->name < nodeToAdd->name)
{
cout << currentNode->name << " is less than " <<
nodeToAdd->name << endl;
previousNode = currentNode;
currentNode = currentNode->next;
}
else
break;
}
if (currentNode == head)
{
cout << endl << "Adding " << nodeToAdd->name << " at head"
<< endl;
nodeToAdd->next = currentNode;
head = nodeToAdd;
}
else if (currentNode == NULL)
{
cout << endl << "Adding " << nodeToAdd->name << " at tail"
<< endl;
previousNode->next = nodeToAdd;
}
else
{
cout << endl << "Adding " << nodeToAdd->name << " between "
<< previousNode->name
<< " and " << currentNode->name << endl;
previousNode->next = nodeToAdd;
nodeToAdd->next = currentNode;
}
}
}
}

void print()
{

node *temp = head;


if (temp == NULL)
{
cout << "list is Empty" << endl;
}
else
{
cout << endl << setw(20) << "Name" << setw(10) << "Age" << setw(10) <<
"gender";
cout << endl << setw(20) << "----" << setw(10) << "---" << setw(10) <<
"------" << endl << endl;
while (temp != NULL)
{
cout << setw(20) /*<< setfill('*') << left */<< temp->name
<< setw(10) << temp->age
<< setw(10) << temp->gender << endl;
temp = temp->next;
}
cout << endl;
}
}

void update(string nameToUpdate)


{
node *nodeToUpdate = head;
while (nodeToUpdate != NULL)
{
if (nodeToUpdate->name == nameToUpdate)
{
cout << "Enter new value for name: ";
do
{
getline(cin, nameToUpdate);
if (nameToUpdate.empty() || isdigit(nameToUpdate[0]))
cout << "Invalid Name. Enter valid value(A-Z, a-z" <<
endl;
else
{
nodeToUpdate->name = nameToUpdate;
break;
}
} while (1);

int age = 0;
cout << "Enter Age: ";
do
{
if (cin.fail() || age < 0)
{
cout << " Invalid Age: Enter Age again " << endl;
cin.clear();
cin.ignore(256, '\n');
}
} while (!(cin >> age) || (age < 0));
nodeToUpdate->age = age;

char gender;
cout << "Enter gender(m for male, f for female): ";
cin >> gender;
cin.ignore(2, '\n');
cin.clear();
nodeToUpdate->gender = gender;

cout << "Update Successful..." << endl;


return;
}
nodeToUpdate = nodeToUpdate->next;
}

cout << nameToUpdate << " Not in list" << endl;


}

bool deleteNode(string name)


{
node *nodeToDelete = head;
node *previous = NULL;
// if value is at head
if (nodeToDelete->name == name)
{
head = nodeToDelete->next;
cout << nodeToDelete->name << "deleted from list" << endl;
delete nodeToDelete;
return true;
}
else
{
previous = nodeToDelete;
nodeToDelete = nodeToDelete->next;
while (nodeToDelete != NULL)
{
if (nodeToDelete->name == name)
{
previous->next = nodeToDelete->next;
cout << endl << nodeToDelete->name << " deleted from list " << endl;
delete nodeToDelete;
return true;
}
previous = nodeToDelete;
nodeToDelete = nodeToDelete->next;
}
}
cout << name << " Not in list. Cannot delete" << endl;
return false;
}

void menu()
{

cout << " 1. Insert Data" << endl;


cout << " 2. Search" << endl;
cout << " 3. Update" << endl;
cout << " 4. Delete" << endl;
cout << " 5. Print" << endl;
cout << " 6. Clear Screen" << endl;
cout << " 7. Quit" << endl << endl;
cout << "Enter Choice: ";
}

void deleteList()
{
node *t = head;
while (t != NULL)
{
t = t->next;
delete head;
head = t;
}
}
int main()
{
char input;
string nameToSearch;
do
{
menu();
input = _getch();
switch (input)
{
case '1':
add();
break;
case '2':
searchMenu();
break;
case '3':
cout << "Enter name to update: ";
getline(cin, nameToSearch);
update(nameToSearch);
break;
case '4':
cout << "Enter name to Delete: ";
getline(cin, nameToSearch);
deleteNode(nameToSearch);
break;
case '5':
print();
break;
case '6':
clearScreen();
break;
case '7':
cout << endl << "Thank You for using the Program";
cout << endl << "Exiting....";
deleteList();
return 0;
default:
cout << "Enter a Valid Choice" << endl;
}

cout << endl << "Do you want another input (y/n)";
input = _getch();
cout << endl;

} while (input != 'n');


cout << endl;

deleteList();
system("pause");
return 0;
}