pr7

 Department of Computer Engineering has student's club named 'Pinnacle Club'. Students of Second,

third and final year of department can be granted membership on request. Similarly one may cancel

the membership of club. First node is reserved for president of club and last node is reserved for

secretary of club. Write C++ program to maintain club member‘s information using singly linked

list. Store student PRN and Name. Write functions to

a) Add and delete the members as well as president or even secretary.

b) Compute total number of members of club

c) Display members

d) Display list in reverse order using recursion

e) Two linked lists exists for two divisions. Concatenate two lists





#include <iostream>

#include <string>

using namespace std;


// Define a structure for a student node

struct Student {

    string prn;

    string name;

    Student* next;

};


// Define a class for managing the club's members

class PinnacleClub {

private:

    Student* head;  // Head pointer for the linked list

public:

    PinnacleClub() {

        head = nullptr;

    }


    // Function to add a member at the end of the list

    void addMember(string prn, string name) {

        Student* newMember = new Student;

        newMember->prn = prn;

        newMember->name = name;

        newMember->next = nullptr;


        if (head == nullptr) {

            head = newMember;  // If list is empty, new member becomes the head

        } else {

            Student* temp = head;

            while (temp->next != nullptr) {

                temp = temp->next;  // Traverse to the last member

            }

            temp->next = newMember;  // Add the new member at the end

        }

    }


    // Function to add a president at the beginning of the list

    void addPresident(string prn, string name) {

        Student* newPresident = new Student;

        newPresident->prn = prn;

        newPresident->name = name;

        newPresident->next = head;

        head = newPresident;  // Set as the new head

    }


    // Function to add a secretary at the end of the list

    void addSecretary(string prn, string name) {

        Student* newSecretary = new Student;

        newSecretary->prn = prn;

        newSecretary->name = name;

        newSecretary->next = nullptr;


        if (head == nullptr) {

            head = newSecretary;

        } else {

            Student* temp = head;

            while (temp->next != nullptr) {

                temp = temp->next;

            }

            temp->next = newSecretary;

        }

    }


    // Function to delete a member by PRN

    void deleteMember(string prn) {

        if (head == nullptr) {

            cout << "No members to delete." << endl;

            return;

        }


        // If the member to delete is the first one

        if (head->prn == prn) {

            Student* temp = head;

            head = head->next;

            delete temp;

            cout << "Member with PRN " << prn << " deleted." << endl;

            return;

        }


        // Traverse the list to find the member to delete

        Student* temp = head;

        while (temp->next != nullptr && temp->next->prn != prn) {

            temp = temp->next;

        }


        if (temp->next == nullptr) {

            cout << "Member with PRN " << prn << " not found." << endl;

        } else {

            Student* toDelete = temp->next;

            temp->next = temp->next->next;

            delete toDelete;

            cout << "Member with PRN " << prn << " deleted." << endl;

        }

    }


    // Function to compute the total number of members

    int getTotalMembers() {

        int count = 0;

        Student* temp = head;

        while (temp != nullptr) {

            count++;

            temp = temp->next;

        }

        return count;

    }


    // Function to display all members

    void displayMembers() {

        if (head == nullptr) {

            cout << "No members in the club." << endl;

            return;

        }


        Student* temp = head;

        while (temp != nullptr) {

            cout << "PRN: " << temp->prn << ", Name: " << temp->name << endl;

            temp = temp->next;

        }

    }


    // Function to concatenate two linked lists (two divisions)

    void concatenateLists(PinnacleClub &otherClub) {

        if (head == nullptr) {

            head = otherClub.head;

            otherClub.head = nullptr;

            return;

        }


        Student* temp = head;

        while (temp->next != nullptr) {

            temp = temp->next;

        }


        temp->next = otherClub.head;  // Link the second list to the end of the first

        otherClub.head = nullptr;  // Set the second list head to null

    }


    // Destructor to delete all members and free memory

    ~PinnacleClub() {

        while (head != nullptr) {

            Student* temp = head;

            head = head->next;

            delete temp;

        }

    }

};


int main() {

    PinnacleClub division1, division2;


    // Adding members to Division 1

    division1.addPresident("P001", "John Doe");

    division1.addMember("P002", "Jane Smith");

    division1.addSecretary("P003", "Michael Brown");


    // Adding members to Division 2

    division2.addMember("P004", "Alice Johnson");

    division2.addMember("P005", "Bob White");


    cout << "Division 1 Members:" << endl;

    division1.displayMembers();


    cout << "\nDivision 2 Members:" << endl;

    division2.displayMembers();


    cout << "\nConcatenating Division 1 and Division 2..." << endl;

    division1.concatenateLists(division2);


    cout << "\nAll Members after Concatenation:" << endl;

    division1.displayMembers();


    cout << "\nTotal members in the club: " << division1.getTotalMembers() << endl;


    // Deleting a member

    division1.deleteMember("P002");


    cout << "\nAll Members after deletion:" << endl;

    division1.displayMembers();


    cout << "\nTotal members in the club: " << division1.getTotalMembers() << endl;


    return 0;

}


Comments

Popular posts from this blog

Pr13