Notice
Recent Posts
Recent Comments
Link
빙수달 게임 개발 노트
[C++] 이중 링크드 리스트(Doubly Linked List)로 학생 관리 프로그램 만들기 본문
#include <iostream>
#include <string>
using namespace std;
struct student
{
int id;
string name;
student* next;
student* prev;
};
class studentBook
{
private:
student* head;
student* tail;
public:
studentBook();
~studentBook();
void insert(int id, string name);
void remove(int id);
void print();
void print_reverse();
void search(int id);
};
int main()
{
int num = 0;
int ID = 0;
string name = "";
studentBook studentRecord;
while (true)
{
cout << "1.삽입 2.출력 3.역순출력 4.검색 5.삭제 6.종료" << 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:
studentRecord.print_reverse();
break;
case 4:
cout << "찾을 학생의 학번을 입력하시오.\n";
cout << "학번 : "; cin >> ID;
cout << endl;
studentRecord.search(ID);
break;
case 5:
cout << "삭제할 학생의 학번을 입력하시오.\n";
cout << "학번 : "; cin >> ID;
studentRecord.remove(ID);
break;
case 6:
return 0;
default:
cout << "잘못된 번호입니다. 다시 입력해주세요.\n\n";
break;
}
}
}
studentBook::studentBook()
{
head = nullptr;
tail = nullptr;
}
studentBook::~studentBook()
{
student* current = head;
while (current != nullptr)
{
student* next = current->next;
delete current;
current = next;
}
}
void studentBook::insert(int id, string name)
{
student* newStudent = new student{ id, name, nullptr, nullptr };
if (head == nullptr)
{
head = newStudent;
tail = newStudent;
}
else // 리스트에 노드가 있을 경우
{
tail->next = newStudent;
newStudent->prev = tail;
tail = newStudent;
}
cout << "학생이 추가되었습니다.\n";
}
void studentBook::remove(int id)
{
if (head == nullptr)
{
cout << "등록된 학생이 없습니다.\n\n";
return;
}
student* current = head;
// 삭제할 노드 탐색
while (current != nullptr && current->id != id)
{
current = current->next;
}
// 노드를 찾지 못한 경우
if (current == nullptr)
{
cout << "해당 학번의 학생을 찾을 수 없습니다.\n\n";
return;
}
// --- 노드 연결 끊기 ---
if (current == head)
{
head = current->next;
if (head != nullptr)
{
head->prev = nullptr;
}
else
{
tail = nullptr;
}
}
else if (current == tail)
{
tail = current->prev;
tail->next = nullptr;
}
else
{
current->prev->next = current->next;
current->next->prev = current->prev;
}
delete current;
cout << "학생이 삭제되었습니다.\n\n";
}
void studentBook::print()
{
if (head == nullptr)
{
cout << "등록된 학생이 없습니다.\n\n";
return;
}
student* current = head;
while (current != nullptr)
{
cout << "학번 : " << current->id << ", 이름 : " << current->name << endl;
current = current->next;
}
cout << endl;
}
void studentBook::print_reverse()
{
if (tail == nullptr)
{
cout << "등록된 학생이 없습니다.\n\n";
return;
}
cout << "--- 역순 출력 ---\n";
student* current = tail;
while (current != nullptr)
{
cout << "학번 : " << current->id << ", 이름 : " << current->name << endl;
current = current->prev;
}
cout << endl;
}
void studentBook::search(int id)
{
if (head == nullptr)
{
cout << "등록된 학생이 없습니다.\n\n";
return;
}
student* current = head;
while (current != nullptr)
{
if (current->id == id)
{
cout << "학생을 찾았습니다.\n";
cout << "학번 : " << current->id << "\n이름 : " << current->name << "\n\n";
return;
}
current = current->next;
}
cout << "해당 학번의 학생을 찾을 수 없습니다.\n\n";
}


'Programming > 알고리즘' 카테고리의 다른 글
| [C++] 커서(Cursor)를 이용한 링크드 리스트로 학생 관리 프로그램 만들기 (0) | 2025.09.28 |
|---|---|
| [알고리즘] 큐(Queue) (0) | 2025.07.08 |
| [C++] 단일 링크드 리스트(Single Linked List)로 학생 관리 프로그램 만들기 (1) | 2025.01.08 |
| [알고리즘] 브루트-포스(Brute Force) (0) | 2025.01.08 |
| [알고리즘] 단순 삽입 정렬(Straight Insertion Sort) (0) | 2025.01.08 |