C++ <ADT:单链表追加、选择排序、删除>

input.txt

Doe                            John                        555555555        333-333-3333    65000 
Doe                            Jan                          444444444        333-333-3333    75000
Hardworker                John                       888888888        888-888-8888    55000
Programmer                Travis                      555446666        666-666-6666    76000

output.txt

Last Name    First Name      ID      Phone   Salary
-----------------------------------------------------------------
Goal-keeper Martin  777777777   888-666-8888    86000
Programmer  Travis  555446666   666-666-6666    76000
Doe Jan 444444444   333-333-3333    75000
Ironman Iron    444444443   828-888-8888    73000
Doe John    555555555   333-333-3333    65000
Wood    Carpenter   333556666   222-222-2222    40000

运行过程:

Original list:
Last Name    First Name      ID      Phone   Salary
-----------------------------------------------------------------
Programmer  Travis  555446666   666-666-6666    76000
Hardworker  John    888888888   888-888-8888    55000
Doe Jan 444444444   333-333-3333    75000
Doe John    555555555   333-333-3333    65000
Sort decrease list:
Last Name    First Name      ID      Phone   Salary
-----------------------------------------------------------------
Programmer  Travis  555446666   666-666-6666    76000
Doe Jan 444444444   333-333-3333    75000
Doe John    555555555   333-333-3333    65000
Hardworker  John    888888888   888-888-8888    55000
Enter you want to delete id: 888888888
After delete:
Last Name    First Name      ID      Phone   Salary
-----------------------------------------------------------------
Programmer  Travis  555446666   666-666-6666    76000
Doe Jan 444444444   333-333-3333    75000
Doe John    555555555   333-333-3333    65000
Do you want to continue <y/n>?: n
After append:
Last Name    First Name      ID      Phone   Salary
-----------------------------------------------------------------
Goal-keeper Martin  777777777   888-666-8888    86000
Ironman Iron    444444443   828-888-8888    73000
Wood    Carpenter   333556666   222-222-2222    40000
Programmer  Travis  555446666   666-666-6666    76000
Doe Jan 444444444   333-333-3333    75000
Doe John    555555555   333-333-3333    65000
Sort decrease list:
Last Name    First Name      ID      Phone   Salary
-----------------------------------------------------------------
Goal-keeper Martin  777777777   888-666-8888    86000
Programmer  Travis  555446666   666-666-6666    76000
Doe Jan 444444444   333-333-3333    75000
Ironman Iron    444444443   828-888-8888    73000
Doe John    555555555   333-333-3333    65000
Wood    Carpenter   333556666   222-222-2222    40000
Program ended with exit code: 0

Info.h

#ifndef __EmployeeCompany__info__
#define __EmployeeCompany__info__

#include <string>
#include <cstddef>
#include <fstream>

class Info {
private:
    struct employees {
        std::string lastName;
        std::string firstName;
        std::string id;
        std::string phone;
        int salary;
        struct employees* link;
    };
    employees* head;
    employees* findFrontId;
public:
    typedef employees* emPtr;
    Info();
    ~Info();
    void addLink(std::ifstream& in);
    void app(std::string lastName, std::string firstName, std::string id, std::string phone, int salary);
    void sortDecrease();
    employees* smallest(employees* here, emPtr& smallPtr);
    void outFile(std::ofstream& out);
    bool find(std::string id, emPtr& findPtr);
    void remove(emPtr findPtr);
    void show() const;
};

#endif 

Info.cpp

#include "info.h"
#include <iostream>
#include <cstdlib>

Info::Info() : head(nullptr) {};

Info::~Info() {
    employees* temp;
    if (head != nullptr) {
        temp = head;
        head = head->link;
        delete temp;
    }
};

void Info::addLink(std::ifstream& in) {
    employees* temp;
    while (! in.eof()) {
        temp = new employees;
        in >> temp->lastName >> temp->firstName >> temp->id >> temp->phone >> temp->salary;
        temp->link = head;
        head = temp;
    }
}

void Info::app(std::string lastName, std::string firstName, std::string id, std::string phone, int salary) {
    employees* temp = new employees;
    temp->lastName = lastName;
    temp->firstName = firstName;
    temp->id = id;
    temp->phone = phone;
    temp->salary = salary;
    temp->link = head;
    head = temp;
}

void Info::sortDecrease() {
    employees* here = head;
    employees* temp = nullptr;
    employees* smallPtr = nullptr;
    employees* p;

    while (here != nullptr) {
        smallPtr = smallest(here, p);

        if (p == here) {
            head = smallPtr;
            here = here->link;
        }
        else {
            head = p;
            smallPtr->link = p->link;
        }
        head->link = temp;
        temp = head;
    }
}

Info::employees* Info::smallest(employees* current, emPtr& p) {
    employees* smaller = current;
    employees* smallFront = current;

    while (current->link != nullptr) {
        if (smaller->salary > current->link->salary) {
            smaller = current->link;
            smallFront = current;
        }
        current = current->link;
    }
    p = smaller;

    return smallFront;
}

void Info::outFile(std::ofstream& out) {
    employees * here = head;

    out << "Last Name \t First Name \t ID \t Phone \t Salary\n"
    << "-----------------------------------------------------------------\n";
    while (here != nullptr) {
        out << here->lastName << "\t" << here->firstName << "\t" << here->id << "\t"
        << here->phone << "\t" << here->salary << '\n';
        here = here->link;
    }
}

bool Info::find(std::string id, emPtr& findPtr) {
    using std::cout;

    employees* here = head;
    findFrontId = head;

    if (here == nullptr)
        cout << "The employees list is empty.\n";
    else {
        while (here != nullptr) {
            if (here->id == id) {
                findPtr = here;
                return true;
            }
            findFrontId = here;
            here = here->link;
        }
        cout << "Not found on the employees list.\n";
    }

    return false;
}

void Info::remove(emPtr findPtr) {
    if (findFrontId == findPtr) {
        head = head->link;
        delete findFrontId;
    } else {
        employees* here = findFrontId->link;
        findFrontId->link = findFrontId->link->link;
        delete here;
    }
}

void Info::show() const {
    using std::cout;

    employees* here = head;
    cout << "Last Name \t First Name \t ID \t Phone \t Salary\n"
     << "-----------------------------------------------------------------\n";
    while (here != nullptr) {
        cout << here->lastName << "\t" << here->firstName << "\t" << here->id << "\t"
        << here->phone << "\t" << here->salary << '\n';
        here = here->link;
    }
}

main.cpp

#include "info.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

int main() {
    using std::cout;
    using std::cin;
    using std::ifstream;
    using std::ofstream;
    using std::string;

    ifstream input;
    ofstream output;

    input.open("/Users/br/Documents/C:C++/EmployeeCompany/EmployeeCompany/input.txt");
    if(input.fail()) {
        cout << "Open input failed.";
        exit(EXIT_FAILURE);
    }

    output.open("/Users/br/Documents/C:C++/EmployeeCompany/EmployeeCompany/output.txt");
    if (output.fail()) {
        cout << "Open input failed.";
        exit(EXIT_FAILURE);
    }

    char c;
    string id;
    Info employee;

    employee.addLink(input);

    cout << "Original list:\n";
    employee.show();

    employee.sortDecrease();
    cout << "Sort decrease list:\n";

    employee.show();

    do {
        cout << "Enter you want to delete id: ";
        cin >> id;

        Info::emPtr findPtr;
        if (employee.find(id, findPtr))
            employee.remove(findPtr);

        cout << "After delete:\n";
        employee.show();

        cout << "Do you want to continue <y/n>?: ";
        cin >> c;
    } while (c == 'y' || c == 'Y');

    employee.app("Wood", "Carpenter", "333556666", "222-222-2222", 40000);
    employee.app("Ironman", "Iron", "444444443", "828-888-8888", 73000);
    employee.app("Goal-keeper", "Martin", "777777777", "888-666-8888", 86000);

    cout << "After append:\n";
    employee.show();

    employee.sortDecrease();
    cout << "Sort decrease list:\n";
    employee.show();

    employee.outFile(output);

    input.close();
    output.close();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值