C
C : 자료구조 : LinkedList : 학생등록관리
아랑팡팡
2023. 7. 25. 00:41
728x90
Linked List Struct
- 연결리스트(Linked List)
사용자가 원하는 노드를 insert, delete, add, search, modify
- 재귀함수를 이용하여 연결리스트를 구현한다.
아래는 학생관리프로그램으로 학생 정보를 랜덤으로 생성, 확인, 수정, 삭제를 하는 프로그램입니다.
ㅇ
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<string.h>
#define SCORE_MAX 3
typedef struct Student
{
char Name[256];
int Age;
int Grade;
int Score[SCORE_MAX];
float Average;
char Rank;
struct Student* Link;
}Student;
char* CreateName();
char GetRank(float Average);
Student* CreateStudent();
Student* Insert(Student* node);
void PrintStudent(Student* student, int Number);
void Print(Student* node);
char* CreateName()
{
switch (rand() % 10)
{
case 0:
return "박민영";
case 1:
return "홍길동";
case 2:
return "김민수";
case 3:
return "이지은";
case 4:
return "강설아";
case 5:
return "최윤정";
case 6:
return "백이진";
case 7:
return "문도형";
case 8:
return "강갑찬";
case 9:
default:
return "한예진";
}
}
//A : 100 ~ 80, B : 79 ~ 60,C : 59 ~ 40, D : 39 ~ 20,F : 19 ~ 0
char GetRank(float Average)
{
if (Average <= 100 && Average >= 80)
return 'A';
else if (Average >= 60)
return 'B';
else if (Average >= 40)
return 'C';
else if (Average >= 20)
return 'D';
else
return 'F';
}
void SetScore(Student* Node)
{
int sum = 0;
for (int i = 0; i < SCORE_MAX; i++)
sum += Node->Score[i];
Node->Average = (float)sum / (float)SCORE_MAX;
Node->Rank = GetRank(Node->Average);
}
Student* CreateStudent()
{
Student* student = (Student*)malloc(sizeof(Student));
strcpy(student->Name, CreateName());
student->Age = rand() % 4 + 20;// 20 ~ 23
student->Grade = rand() % 4 + 1;//1 ~ 4
for (int i = 0; i < SCORE_MAX; i++)
student->Score[i] = rand() % 101;// 0 ~ 100
SetScore(student);
student->Link = NULL;
return student;
}
Student* Insert(Student* node)
{
if (node == NULL)
node = CreateStudent();
else
node->Link = Insert(node->Link);
return node;
}
void PrintStudent(Student* student, int Number)
{
printf("%3d번", Number);
printf("%10s", student->Name);
printf("%4d살", student->Age);
printf("%3d학년", student->Grade);
for (int i = 0; i < SCORE_MAX; i++)
printf("%5d", student->Score[i]);
printf("%7.2f", student->Average);
printf("%2c\n", student->Rank);
}
void Print(Student* node)
{
//후치 vs 전치 -> 전치
int Number = 0;
while (node != NULL)
{
PrintStudent(node, ++Number);
node = node->Link;
}
}
void Release(Student* node, int Number)
{
if (node == NULL)
return;
Release(node->Link, Number + 1);
PrintStudent(node, Number);
free(node);
}
void SearchGrade(Student* node, int Grade)
{
int Number = 0;
printf("=======%d학년=======\n", Grade);
while (node != NULL)
{
++Number;
if (node->Grade == Grade)
PrintStudent(node, Number);
node = node->Link;
}
printf("====================\n");
}
void SearchRank(Student* node, char Rank)
{
int Number = 0;
printf("========%c등급========\n", Rank);
while (node != NULL)
{
++Number;
if (node->Rank == Rank)
PrintStudent(node, Number);
node = node->Link;
}
printf("====================\n");
}
void SearchName(Student* node)
{
char Name[256];
int Number = 0;
printf("검색할 이름 : ");
scanf("%s", Name);
while (node != NULL)
{
++Number;
if (strcmp(node->Name, Name) == 0)
PrintStudent(node, Number);
node = node->Link;
}
}
Student* SearchNumber(Student* Node, int Num, int SearchNum)
{
if (Node == NULL)
return NULL;
else if (Num == SearchNum)
return Node;
else
return SearchNumber(Node->Link, Num + 1, SearchNum);
}
void Modify(Student* Head)
{
int Number;
printf("수정할 학생 번호 : ");
scanf("%d", &Number);
Student* FindNode = SearchNumber(Head, 1, Number);
if (FindNode == NULL)
return;
while (TRUE)
{
system("cls");
PrintStudent(FindNode, Number);
printf("==== 수정 항목 ====\n");
printf(" 1.이름\n");
printf(" 2.점수\n");
printf(" 3.학년\n");
printf(" 4.나이\n");
printf(" 5.종료\n");
int Select;
printf("==================\n");
printf("선택 : ");
scanf("%d", &Select);
switch (Select)
{
case 1:
printf("수정할 이름 :");
scanf("%s", FindNode->Name);
break;
case 2:
for (int i = 0; i < SCORE_MAX; i++)
{
printf("Score[%d] : ", i);
scanf("%d", &FindNode->Score[i]);
}
SetScore(FindNode);
break;
case 3:
printf("수정할 학년 :");
scanf("%d", &FindNode->Grade);
break;
case 4:
printf("수정할 나이 :");
scanf("%d", &FindNode->Age);
break;
case 5:
return;
}
}
}
Student* Delete(Student* Node, char Name[256])
{
if (Node == NULL)
return NULL;
else if (strcmp(Node->Name, Name) == 0)
{
Student* DeleteNode = Node;
Node = Node->Link;
printf("%s 학생 삭제완료\n", DeleteNode->Name);
free(DeleteNode);
}
else
{
Node->Link = Delete(Node->Link, Name);
}
return Node;
}
Student* AllDelete(Student* Node, char Name[256])
{
if (Node == NULL)
return NULL;
Node->Link = AllDelete(Node->Link, Name);
if (strcmp(Node->Name, Name) == 0)
{
Student* DeleteNode = Node;
Node = Node->Link;
printf("%s 학생 삭제완료\n", DeleteNode->Name);
free(DeleteNode);
}
return Node;
}
void main()
{
srand(time(NULL));
Student* Head = NULL;
int StudentCount = 0;
char Name[256];
while (TRUE)
{
system("cls");
printf("=====Score Manager(%d명)=====\n", StudentCount);
printf(" 1.학생 등록\n");
printf(" 2.학생 전체 출력(번호순)\n");
printf(" 3.학생 전체 출력(학년순)\n");
printf(" 4.학생 전체 출력(등급순)\n");
printf(" 5.학생 검색(이름검색)\n");
//2번문제 : 수정(번호로 검색해서 정보수정)
//수정 요소 : 이름,점수,학년,나이
printf(" 6.학생 정보 수정\n");
//3번문제 : 삭제(이름으로 검색해서 삭제)
//동명이인 모두 일괄 삭제
printf(" 7.학생 정보 삭제\n");
printf(" 8.종료\n");
printf("=====================\n");
printf("선택 : ");
int Select;
scanf("%d", &Select);
system("cls");
switch (Select)
{
case 1:
++StudentCount;
Head = Insert(Head);
break;
case 2:
Print(Head);
break;
case 3:
for (int grade = 1; grade <= 4; ++grade)
SearchGrade(Head, grade);
break;
case 4:
for (char rank = 'A'; rank <= 'F'; rank++)
{
if (rank == 'E')
continue;
SearchRank(Head, rank);
}
break;
case 5:
SearchName(Head);
break;
case 6:
Modify(Head);
break;
case 7:
printf("학생 이름 : ");
scanf("%s", Name);
Head = AllDelete(Head, Name);
break;
case 8:
Release(Head, 1);
return;
}
system("pause");
}
}
728x90
반응형
LIST