Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- using namespace std;
- class Phone {
- private:
- int number;
- int pin;
- int price;
- char *brand;
- char *model;
- Phone *next;
- public:
- //Constructor
- Phone(int newNumber, int newPin, int newPrice, const char* brand, const char* model);
- //Destructor
- ~Phone();
- void setNext(Phone *nxt) {
- next = nxt;
- }
- Phone *getNext() const {
- return next;
- }
- char *getBrand() const {
- return brand;
- }
- void setBrand(char * b) {
- brand = b;
- }
- void setModel(char * m) {
- model = m;
- }
- void setPrice(int p) {
- price = p;
- }
- void setNumber(int n) {
- number = n;
- }
- void setPin(int p) {
- pin = p;
- }
- char *getModel() const {
- return model;
- }
- int getPrice() const {
- return price;
- }
- int getPin() const {
- return pin;
- }
- int getNumber() const {
- return number;
- }
- void displayList();
- };
- class PhoneList {
- //declaration of head and tail
- Phone *head;
- Phone *tail;
- public:
- //Constructor
- PhoneList() {
- //initialization of head and tail
- head = NULL;
- tail = NULL;
- }
- //Destructor
- ~PhoneList();
- void addPhone(int number, int pin, int price, const char* brand, const char* model);
- void displayList();
- void clearList();
- void removeNumber(int number);
- bool numberExists(int number);
- bool remove(int number);
- int modify(int number, const char* brand, const char*model);
- char *getBrand(int number);
- char *getModel(int number);
- Phone *getPhone(int number);
- };
- Phone::Phone(int newNumber, int newPin, int newPrice, const char* newBrand, const char* newModel)
- {
- //+1 because we are using the index system which starts at 0
- brand = new char[strlen(newBrand)+1];
- model = new char[strlen(newBrand)+1];
- if(brand != NULL && model != NULL) {
- //Assign values
- strcpy(brand, newBrand);
- strcpy(model, newModel);
- pin = newPin;
- number = newNumber;
- price = newPrice;
- next = NULL;
- }
- }
- Phone::~Phone() //DESTRUCTOR
- {
- delete[] brand, delete[] model; //Deletes the memory allocated -> new char...
- }
- void PhoneList::displayList() {
- cout << endl << "phones in the list:" << endl;
- Phone *current = head;
- if(current == NULL) { //If there are no phones in the list
- cout << "There are no phones in the list!" << endl;
- } else {
- while(current != NULL) { //Loop through all of them and print out a information of the phone
- cout << "brand: " << current->getBrand() << "," << " model: " << current->getModel() << "," << " number: " <<
- current->getNumber() << "," << " Price: " << current->getPrice() << "," << " Pin: " << current->getPin() << endl;
- current = current->getNext();
- }
- }
- cout << endl;
- }
- void PhoneList::addPhone(int number, int pin, int price, const char* brand, const char* model) {
- Phone *previous = NULL; //Previous is null
- Phone *current = head; //Current is the head
- //Checking if the current number is less than the new number
- while(current != NULL) {
- if (current->getNumber() < number) {
- previous = current;
- current = current->getNext();
- }
- else break;
- }
- //Checking if current phone's number is as same the new number's id. In this case, it exists so we won't be adding it.
- if(current != NULL && current->getNumber() == number) {
- cout << "Duplicates found for phone: " << "number: " << number << endl;
- //Adding a new phone
- } else {
- Phone *newPhone = new Phone(number, pin, price, brand, model);
- if(newPhone != NULL) {
- if(previous != NULL) {
- newPhone->setNext(previous->getNext());
- previous->setNext(newPhone);
- } else {
- head = newPhone;
- newPhone->setNext(NULL);
- }
- //Print out the phone added.
- cout << "Added a new phone: " << "brand: " << brand << "," << " model: " << model << "," << " number: " << number << endl;
- //newPhone is null
- } else {
- cout << "Error: Memory allocation.";
- }
- }
- }
- bool PhoneList::numberExists(int number)
- {
- Phone* current = head;
- //While not null
- while (current != NULL)
- {
- //Check through the linked list for this id
- if (current->getNumber() == number)
- return true;
- current = current->getNext(); //Go to the next phone
- }
- return false;
- }
- bool PhoneList::remove(int number) {
- Phone *before_temp = NULL;
- Phone *temp = head;
- //While not the end of the list
- while(temp != NULL) {
- //Checks if the current owner id is as same as the passed parameter.
- if (temp->getNumber() == number) {
- if (temp == head && temp == tail) {
- delete head->getBrand();
- delete head->getModel();
- delete(head); //Deletes the node
- head = tail = NULL;
- } else if (temp == head) {
- temp = head->getNext();
- delete head->getBrand();
- delete head->getModel();
- delete(head); //Deletes the node
- head = temp;
- } else if (temp == tail) {
- delete tail->getBrand();
- delete tail->getModel();
- delete(tail); //Deletes the node
- tail = before_temp;
- } else {
- before_temp->setNext(temp->getNext());
- delete temp->getBrand();
- delete temp->getModel();
- delete(temp); //Deletes the node
- }
- return true;
- }
- //Assign the new values
- before_temp = temp;
- temp = temp->getNext();
- }
- return false;
- }
- void PhoneList::removeNumber(int number) {
- //If it exists
- if(numberExists(number)) {
- bool condition;
- int phones_deleted = 0; //Calculator.. Not important but just to track and for practice.
- while (head != NULL) {
- condition = remove(number);
- if (condition) {
- phones_deleted++; //Increment the deleted phones count
- } else {
- break;
- }
- }
- cout << "Deleted " << phones_deleted << " phone(s) for number " << number << endl;
- } else {
- cout << "Number " << number << " doesn't exist to be deleted." << endl;
- }
- }
- void PhoneList::clearList() {
- Phone *temp;
- while(head != NULL) {
- temp = head;
- head = head->getNext();
- delete temp;
- }
- cout << "The list has been cleared" << endl;
- }
- //Checks whether the number exists in the linked list of not.
- char * PhoneList::getBrand(int number) {
- bool condition = false;
- Phone* current = head;
- //While not null
- while (current != NULL)
- {
- //Check through the linked list for this number
- if (current->getNumber() == number) //If identical
- return current->getBrand();
- current = current->getNext();
- }
- }
- //Checks whether the number exists in the linked list of not.
- char * PhoneList::getModel(int number) {
- bool condition = false;
- Phone* current = head;
- //While not null
- while (current != NULL)
- {
- //Check through the linked list for this number
- if (current->getNumber() == number) //If identical
- return current->getModel();
- current = current->getNext();
- }
- }
- //Checks whether the number exists in the linked list of not.
- Phone * PhoneList::getPhone(int number) {
- bool condition = false;
- Phone* current = head;
- //While not null
- while (current != NULL)
- {
- //Check through the linked list for this number
- if (current->getNumber() == number) //If identical
- return current;
- current = current->getNext();
- }
- }
- int PhoneList::modify(int number, const char *brand, const char *model)
- {
- //if the list is empty, or the id doesn't exist, Then create a new element.
- if(head==NULL || !numberExists(number))
- {
- addPhone(number, 0, 0, brand, model); //Adding new phone with entered values
- return getPhone(number)->getPrice();
- //If the id exists in the linked list
- } else if(numberExists(number)) {
- //if the model or the brand are the same
- if(strcmp(getBrand(number), brand) == 0 || strcmp(getModel(number), model) == 0){
- Phone * currentPhone = getPhone(number);
- strcpy( currentPhone->getModel(), model);
- strcpy( currentPhone->getBrand(), brand);
- return currentPhone ->getPrice();
- }else{
- Phone * currentPhone = getPhone(number);
- return currentPhone->getPrice();
- }
- }
- cout << endl;
- }
- PhoneList::~PhoneList()
- {
- Phone *tmp = NULL;
- while(head != NULL)
- {
- tmp = head;
- head = head->getNext();
- delete tmp;
- }
- }
- /* ---------- The main function here ---------- */
- int main() {
- //Declaration and initialization of two lists. Dynamic and static.
- PhoneList List1; //Static
- PhoneList *List2 = new PhoneList(); //Dynamic
- cout << "List 1:" << endl;
- List1.displayList();
- List1.addPhone(10, 0,100,"a brand", "a model");
- List1.addPhone(14, 0,200,"a brand", "a model");
- cout << endl;
- List1.removeNumber(10);
- List1.displayList();
- //Separator between list 1 and 2 to not get lost :P
- cout << "-------------------------------------" << endl;
- cout << "List 2:" << endl;
- List2->addPhone(2,4, 500,"brand1","model1");
- List2->addPhone(2,4, 500,"brand2","model2");
- List2->addPhone(10,0,1000, "a brand","a model");
- cout << endl;
- List2 ->removeNumber(1);
- List2 -> displayList();
- delete List2; //deleting the dynamic object.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement