Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to add 1000 customers automatically when program runs. Below is my C++ code // Linked list implementation in C++ #include using namespace std; //

How to add 1000 customers automatically when program runs. Below is my C++ code

// Linked list implementation in C++

#include using namespace std;

// Creating a Customer Node class Customer { public: // Define the Public Variable name,id,dob,city,next string name; int id; string dob; string city; Customer* next;

// parameterized constructor Customer(string n,int myId,string date_of_birth,string c){ name=n; id=myId; dob=date_of_birth; city=c; next=NULL; } };

// Creating the Linked List class Linkedlist { // define head of Linked List Customer* head;

public: // Default constructor Linkedlist() { head = NULL; }

// Function to insert a new Customer. void addCustomer(string name,int id,string dob,string city) { // Create the new Node. Customer* newNode = new Customer(name,id,dob,city); // Assign to head if (head == NULL) { head = newNode; return; }

// Traverse till end of list Customer* temp = head; while (temp->next != NULL) {

// Update temp temp = temp->next; }

// Insert at the last. temp->next = newNode; }

// Function to search Customer by id void searchCustomer(int id) { Customer* temp = head;

// Check for empty list. if (head == NULL) { cout << "List empty" << endl; return; }

// Traverse the list. while (temp != NULL) { if(temp->id==id){ cout<<"Id Found"; return; } temp = temp->next; }

// print message the Id is Not Found cout<<"Id Not Found"; }

// Function to search by address void searchByAddress(string address) { Customer* temp = head;

// Check for empty list. if (head == NULL) { cout << "List empty" << endl; return; }

// Traverse the list. while (temp != NULL) { if((temp->city).compare(address)==0){ cout<name<<" "<id; return; } temp = temp->next; }

}

// Function to remove all the customer void removeAllCustomer() { // Define Customer curr variable Customer *curr = head;

// Check Empty List if (head == NULL) { cout << "List empty." << endl; return; }

// Traverse the Linked List while (curr != NULL) { // Move to next Customer curr = curr->next; // delete the Customer delete curr; }

// update head with NULL head=NULL; cout<<"Deleted All Customer";

}

void printAllCustomer() { // Define Customer curr variable Customer *curr = head;

// Check Empty List if (head == NULL) { cout << "List empty." << endl; return; }

// Traverse the Linked List while (curr != NULL) { // print the Customer Detail cout<<(curr->name)<<" "<<(curr->id)<<" "<<(curr->dob)<<" "<<(curr->city)<next; }

}

};

// Function to display all the menu in // the current program void menu() { cout << " \t\tMENU :"; cout << " 1. Add New Customer "; cout << "2. Search Customer by Id "; cout << "3. List Name and IDs of Customer "; cout << "4. Remove All the Customer "; cout << "5. Print All the Customer "; cout << "6. Exit ";

}

// Driver Code int main() { // Define the Linked List Linkedlist list; // Define the choice int choice;

// run the loop while(true){

cout << " "; // call the menu Function menu(); // Take Input from user for choice cout << " Enter your " << "choice: "; cin >> choice;

// if choice is 1 // take input for data (name,id,date of birth,city) // call the addCustomer function if(choice==1) {string name; int id; string dob; string city; cout<<"Name : ";cin>>name; cout<<"ID : ";cin>>id; cout<<"Date of Birth : ";cin>>dob; cout<<"City : ";cin>>city; list.addCustomer(name,id,dob,city); }

// if choice is 2 // take input for data (id) // call the searchCustomer function else if(choice==2) {int id; cout<<"ID : ";cin>>id; list.searchCustomer(id); } // if choice is 3 // take input for data (city) // call the searchByAddress function else if(choice==3) {string city; cout<<"City : ";cin>>city; list.searchByAddress(city); } // if choice is 4 // call the removeAllCustomer function else if(choice==4) {list.removeAllCustomer(); } // if choice is 5 // call the printAllCustomer function else if(choice==5) {list.printAllCustomer(); break;} // if choice is 6 // exit the loop else if(choice==6) {exit(0); } // invalid choice else{ cout << "INVALID CHOICE :-("; }

} return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions