빙수달 게임 개발 노트

[C++] 단일 링크드 리스트(Single Linked List)로 학생 관리 프로그램 만들기 본문

Programming/알고리즘

[C++] 단일 링크드 리스트(Single Linked List)로 학생 관리 프로그램 만들기

빙수달 2025. 1. 8. 23:39
#include <iostream>

using namespace std;

struct student               // 학생에 대한 정보
{
    int id;
    string name;
    student* next;      // 다음 학생을 가리킴
};

class studentBook
{
    student* head;         // 리스트의 맨 앞
    student* node;         // 가리키고자 하는 노드

public:
    studentBook();
    void insert(int id, string name);
    void remove(int id);
    void print();
    void search(int id);
};



int main()
{
    int num = 0;      // 원하는 행동 선택 변수 num

    int ID = 0;
    string name = "";

    studentBook studentRecord;      // 학생부 객체 생성

    while (1)
    {
        cout << "1.삽입 2.출력 3.검색 4.삭제 5.종료" << endl;
        cout << "번호를 입력하세오 : ";
        cin >> num;
        cout << "\n";

        switch (num)
        {
        case 1:
            cout << "학번 : ";
            cin >> ID;
            cout << "이름 : ";
            cin >> name;
            cout << endl;
            studentRecord.insert(ID, name);
            break;
        case 2:
            studentRecord.print();
            break;
        case 3:
            cout << "찾을 학생의 학번을 입력하시오.\n";
            cout << "학번 : ";
            cin >> ID;

            cout << endl;
            studentRecord.search(ID);
            break;
        case 4:
            cout << "삭제할 학생의 학번을 입력하시오.\n";
            cout << "학번 : ";
            cin >> ID;
            studentRecord.remove(ID);

            break;
        case 5:
            return 0;
        }
    }
}

studentBook::studentBook()
{
    head = nullptr;
    node = nullptr;
}

void studentBook::insert(int id, string name)
{
    student* newStudent = new student;
    newStudent->id = id;
    newStudent->name = name;
    newStudent->next = nullptr;

    if (head == nullptr)
    {
        head = newStudent;
        return;
    }
    node = head;

    while (node->next != nullptr)
    {
        node = node->next;
    }
    node->next = newStudent;
}

void studentBook::remove(int id)
{
    if (head == nullptr)
    {
        cout << "등록된 학생이 없습니다.\n\n";
        return;
    }

    // head의 학생이 삭제 대상일 경우
    if (head->id == id)
    {
        student* temp = head;
        head = head->next;
        delete temp;
        cout << "학생이 삭제되었습니다.\n\n";
        return;
    }

    student* prevRemoveStudent = head;

    while (prevRemoveStudent->next != nullptr)
    {
        if (prevRemoveStudent->next->id == id)
        {
            student* temp = prevRemoveStudent->next;
            prevRemoveStudent->next = temp->next;
            delete temp;
            cout << "학생이 삭제되었습니다.\n\n";
            return;
        }
        prevRemoveStudent = prevRemoveStudent->next;
    }

    cout << "해당 학번의 학생을 찾을 수 없습니다.\n\n";
}

void studentBook::print() // 출력
{
    if (head == nullptr)
    {
        cout << "등록된 학생이 없습니다.\n";
        return;
    }

    node = head;
    while (node != nullptr)
    {
        cout << "학번 : " << node->id << endl;
        cout << "이름 : " << node->name << endl << endl;
        node = node->next;
    }
}

void studentBook::search(int id)      // 검색
{
    if (head == nullptr)
    {
        cout << "등록된 학생이 없습니다.\n\n";
        return;
    }

    student* searchStudent = head;      // 찾고자하는 학생의 head

    cout << "검색하고자 하는 학생의 학번을 입력하세요 : ";


    while (searchStudent != nullptr)      // searchStudent를 찾을 때까지
    {
        if (searchStudent->id == id)
        {
            cout << "\n학번 : " << searchStudent->id << "\n이름 : " << searchStudent->name << "\n\n";
            return;
        }
        searchStudent = searchStudent->next;
    }
    cout << "검색에 실패하였습니다.\n\n";

}