user.h (second update)

 avatar
user_2508819
plain_text
a year ago
3.4 kB
1
Indexable
Never
#pragma once
#include <iostream>
#include <string>

using namespace std;

// Set Data Structure 
struct UserInfo
{
    // User Information - Attributes
    int userID;
    string firstName;
    string lastName;
    string fullName;
    string email;
    string gender;
    int age;
    string phoneNumber;
    string address;
    string state;
    string nationality;
    string martialStatus;
    string bloodType;
    string occupation;

    UserInfo(int id, const string &first, const string &last, const string &full, const string &mail, const string &gen, int a,
             const string &phone, const string &addr, const string &st, const string &nation,
             const string &martial, const string &blood, const string &occ)
        : userID(id), firstName(first), lastName(last), fullName(full), email(mail), gender(gen), age(a),
          phoneNumber(phone), address(addr), state(st), nationality(nation), martialStatus(martial),
          bloodType(blood), occupation(occ) {}
};


// Set Data Structure - Singly Linked List 
struct UserNode
{
    UserInfo info;
    UserNode *next;

    UserNode(const UserInfo &userData) : info(userData), next(nullptr) {}
};


// User Class
class User
{
private:
    UserNode *head;

public:
    User() : head(nullptr) {}

    // Insert a new user at the front of the linked list
    void insertFront(const UserInfo &userData)
    {
        UserNode *newNode = new UserNode(userData);
        newNode->next = head;
        head = newNode;
    }

    // Delete a user with a specific userID
    bool deleteUser(int userID)
    {
        UserNode *current = head;
        UserNode *prev = nullptr;

        while (current)
        {
            if (current->info.userID == userID)
            {
                if (prev)
                {
                    prev->next = current->next;
                }
                else
                {
                    head = current->next;
                }

                delete current;
                return true; // User found and deleted
            }

            prev = current;
            current = current->next;
        }

        return false; // User with userID not found
    }

    // Search for a user by userID and return the UserData
    UserInfo *searchUser(int userID)
    {
        UserNode *current = head;

        while (current)
        {
            if (current->info.userID == userID)
            {
                return &current->info; // User found
            }

            current = current->next;
        }

        return nullptr; // User with userID not found
    }

    // Display all users in the linked list
    void displayUsers()
    {
        UserNode *current = head;

        while (current)
        {
            cout << "User ID: " << current->info.userID << endl;
            cout << "Name: " << current->info.firstName << " " << current->info.lastName << endl;
            // Display other user attributes as needed
            cout << "-----------------------" << endl;

            current = current->next;
        }
    }

    // Destructor to free memory
    ~User()
    {
        UserNode *current = head;
        while (current)
        {
            UserNode *temp = current;
            current = current->next;
            delete temp;
        }
    }
};