Question

Define a C++ class managing objects using a Linked List.

(10 points) Define a C++ class named BirthdayList that manages a list of Birthday objects as a linked list maintaining the order of entry (first birthday should be displayed first). Write a main function to show that you can add a list of birthdays and print out that list in the order that you have entered. (5 points) Add a search method into the BirthdayList class that accepts a Birthday object and returns true if it finds a Birthday object in the current list that has the same birthday information and false if not found. Show how this search method is being used and print out the right result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

//Birthday class
class Birthday{
public:
    int date;
    int month;
    int year;
    //Pointer to store the next birthday in the list
    Birthday *next;
    //Constructor that takes the date, month and year
    Birthday(int d, int m, int y){
        date = d;
        month = m;
        year = y;
    }
};

//BirthdayList class
class BirthdayList{
public:
    //Variable to store beginning of the list
    Birthday *root;

    //This function adds birthdays to the list
    void addBirthday(Birthday *bDay){
        if(root==NULL){
            root = bDay;
            bDay->next = NULL;
            return;
        }
        Birthday *node = root;
        //Move to the last node
        while(node->next!=NULL){
            node = node->next;
        }
        node->next = bDay;
        bDay->next = NULL;

    }

    //This function searches birthday in the list
    bool searchBirthday(Birthday *bDay){
        if(root==NULL){
            return false;
        }
        Birthday *node = root;
        //Search all the birthdays in the list
        while(node->next!=NULL){
            if(node->date==bDay->date && node->month==bDay->month && node->year == bDay->year){
                return true;
            }
            node = node->next;
        }
        return false;
    }

};

int main()
{
    BirthdayList *bList = new BirthdayList();

    Birthday *a1 = new Birthday(21,1,1995);
    Birthday *a2 = new Birthday(23,05,1997);
    Birthday *a3 = new Birthday(24,04,1987);
    bList->addBirthday(a1);
    bList->addBirthday(a2);
    bList->addBirthday(a3);
    cout<<"Birthdays added in the list : "<<endl;
    Birthday *start = bList->root;
    while(start!=NULL){
        cout<<start->date<<" "<<start->month<<" "<<start->year<<endl;
        start = start->next;
    }

    cout<<"\na1 found in the list "<<bList->searchBirthday(a1)<<endl;
    Birthday *a4 = new Birthday(21,2,1995);
    cout<<"a4 found in the list "<<bList->searchBirthday(a4)<<endl;

    return 0;
}


OUTPUT :

Add a comment
Know the answer?
Add Answer to:
Define a C++ class managing objects using a Linked List. (10 points) Define a C++ class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, o...

    Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, or search for a Perso n object in the address book The add method should add a person object to the address book. The delete method should remove the specified person object from the address book 0 .The search method that searches the address book for a specified...

  • Suppose we define a class called "Something" that maintains a list of objects of some type....

    Suppose we define a class called "Something" that maintains a list of objects of some type. Suppose further that we declare methods "doA" and "doB" in the class. Method "doA" has a void return type and takes an object received as an input argument and adds it to the end of the list. Method "doB" removes the object that is in the first position in the list and returns it. What type of abstract data structure does the class represent?

  • C++, Visual Studio (Optional-Extra Credit) Write an AddressBook class that manages a collection of Person objects....

    C++, Visual Studio (Optional-Extra Credit) Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question Lab 4 Question 2. An AddressBook will allow a person to add, delete, or search for a Person object in the address book 1. The add method should add a person object to the address book .The delete method should remove the specified person object from the address book .The search method that searches the address book...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • #1 [4 points] a) Draw a sketch of a doubly-linked list, specifically of class DLinkedList in...

    #1 [4 points] a) Draw a sketch of a doubly-linked list, specifically of class DLinkedList in DLinkedList.h, containing the courses, including the section number, you are taking this semester; for example, CPSC 131.01 will be "CPSC 131.01". Make sure to show the DLinkedList object, all node objects, and all objects' data members and pointers. b) Complete the following main function to create your linked list above (Your code should go in only the indicated space - do not change any...

  • Using an appropriate definition of ListNode, design a simple linked list class called StringList with the...

    Using an appropriate definition of ListNode, design a simple linked list class called StringList with the following member functions: void add (std::string); int positionOf (std::string); bool setNodeVal(int, std::string); std::vector<std::string> getAsVector(); a default constructor a copy constructor a destructor The add() function adds a new node containing the value of the parameter to the end of the list. The positionOf() function returns the (zero-based) position in the list for the first occurrence of the parameter in the list, or -1 if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT