More actions
imported>Unknown No edit summary |
(Table transclusion repair v1) |
||
| Line 1: | Line 1: | ||
[[강희경]]이 타과 친구의 부탁으로 대신 해준 숙제. 스펙에 있는 배열로 구현하라는 말을 못 보고 혼자 신나서 링크드 리스트로 구현했다. 이걸 어째... | [[강희경]]이 타과 친구의 부탁으로 대신 해준 숙제. 스펙에 있는 배열로 구현하라는 말을 못 보고 혼자 신나서 링크드 리스트로 구현했다. 이걸 어째... | ||
== 스팩 == | == 스팩 == | ||
메모리 이용 학생관리 프로그램 | 메모리 이용 학생관리 프로그램 | ||
-배열이용 | -배열이용 | ||
| Line 9: | Line 8: | ||
(검색-binary search??, sequential search...) | (검색-binary search??, sequential search...) | ||
(option-파일에 쓰고 읽는 부분 첨가) | (option-파일에 쓰고 읽는 부분 첨가) | ||
== 소스 == | == 소스 == | ||
| Line 27: | Line 25: | ||
struct Student{ | struct Student{ | ||
char dept | char dept[10]; | ||
char name | char name[10]; | ||
int number; | int number; | ||
struct Student* nextStudent; | struct Student* nextStudent; | ||
| Line 35: | Line 33: | ||
int Menu(int aPopulation);//메뉴선택 | int Menu(int aPopulation);//메뉴선택 | ||
int Process(int aMenu, int aPopulation, Student* aListPointer | int Process(int aMenu, int aPopulation, Student* aListPointer[]); | ||
int AddStudent(int aPopulation, Student* aListPointer | int AddStudent(int aPopulation, Student* aListPointer[]);//새로운 학생 추가 | ||
void SearchStudent(Student* aHead);//단순 찾기 | void SearchStudent(Student* aHead);//단순 찾기 | ||
int DelStudent(int aPopulation, Student* aListPointer | int DelStudent(int aPopulation, Student* aListPointer[]);//찾아서 지우기 | ||
void InputStudentInfo(Student* aStudent);//정보 입력 | void InputStudentInfo(Student* aStudent);//정보 입력 | ||
void FreeMemory(Student* aHead);//메모리 해제 | void FreeMemory(Student* aHead);//메모리 해제 | ||
| Line 46: | Line 44: | ||
int main() | int main() | ||
{ | { | ||
Student* listPointer | Student* listPointer[2];//리스트의 머리와 꼬리 | ||
int population = 0;//등록된 학생수 | int population = 0;//등록된 학생수 | ||
| Line 53: | Line 51: | ||
printf("\n");//보기 좋게 한 칸 띈다. | printf("\n");//보기 좋게 한 칸 띈다. | ||
}while(population != -1);//프로그램 종료 조건 | }while(population != -1);//프로그램 종료 조건 | ||
FreeMemory(listPointer | FreeMemory(listPointer[HEAD]);//메모리 해제 | ||
system("PAUSE"); | system("PAUSE"); | ||
return 0; | return 0; | ||
| Line 71: | Line 69: | ||
return selectedMenu; | return selectedMenu; | ||
} | } | ||
int Process(int aMenu, int aPopulation, Student* aListPointer | int Process(int aMenu, int aPopulation, Student* aListPointer[]){ | ||
if(aMenu == 1)//추가 | if(aMenu == 1)//추가 | ||
aPopulation = AddStudent(aPopulation, aListPointer); | aPopulation = AddStudent(aPopulation, aListPointer); | ||
| Line 78: | Line 76: | ||
else if(aPopulation){//등록된 학생이 있으면 | else if(aPopulation){//등록된 학생이 있으면 | ||
if(aMenu == 2)//찾기 | if(aMenu == 2)//찾기 | ||
SearchStudent(aListPointer | SearchStudent(aListPointer[HEAD]); | ||
if(aMenu == 3)//삭제 | if(aMenu == 3)//삭제 | ||
aPopulation = DelStudent(aPopulation, aListPointer); | aPopulation = DelStudent(aPopulation, aListPointer); | ||
| Line 86: | Line 84: | ||
return aPopulation; | return aPopulation; | ||
} | } | ||
int AddStudent(int aPopulation, Student* aListPointer | int AddStudent(int aPopulation, Student* aListPointer[]){ | ||
Student* newStudent; | Student* newStudent; | ||
newStudent = (Student*)malloc(sizeof(Student)); | newStudent = (Student*)malloc(sizeof(Student)); | ||
InputStudentInfo(newStudent); | InputStudentInfo(newStudent); | ||
if(!aPopulation){//첫 등록인 경우 | if(!aPopulation){//첫 등록인 경우 | ||
aListPointer | aListPointer[HEAD] = newStudent; | ||
} | } | ||
else{ | else{ | ||
aListPointer | aListPointer[TAIL]->nextStudent = newStudent; | ||
} | } | ||
aListPointer | aListPointer[TAIL] = newStudent; | ||
aPopulation++; | aPopulation++; | ||
printf("\n현재 등록된 학생명단\n");//현재 등록되어 있는 명단을 출력한다. | printf("\n현재 등록된 학생명단\n");//현재 등록되어 있는 명단을 출력한다. | ||
ListOutput(aListPointer | ListOutput(aListPointer[HEAD]); | ||
return aPopulation; | return aPopulation; | ||
} | } | ||
| Line 114: | Line 112: | ||
printf("검색 실패, 없는 번호입니다.\n"); | printf("검색 실패, 없는 번호입니다.\n"); | ||
} | } | ||
int DelStudent(int aPopulation, Student* aListPointer | int DelStudent(int aPopulation, Student* aListPointer[]){ | ||
int deleteNumber; | int deleteNumber; | ||
Student* searchedFormer; | Student* searchedFormer; | ||
| Line 120: | Line 118: | ||
printf("\n삭제할 학생의 번호를 입력하세요: "); | printf("\n삭제할 학생의 번호를 입력하세요: "); | ||
scanf("%d", &deleteNumber); | scanf("%d", &deleteNumber); | ||
searchedFormer = Searching(deleteNumber, aListPointer | searchedFormer = Searching(deleteNumber, aListPointer[HEAD], DELETIONSEARCH); | ||
if(!searchedFormer || searchedFormer->nextStudent){ | if(!searchedFormer || searchedFormer->nextStudent){ | ||
if(!searchedFormer){ //가장 처음 일때 | if(!searchedFormer){ //가장 처음 일때 | ||
searched = aListPointer | searched = aListPointer[HEAD]; | ||
aListPointer | aListPointer[HEAD] = searched->nextStudent; | ||
} | } | ||
else if(!(searched->nextStudent)){//끝 일때 | else if(!(searched->nextStudent)){//끝 일때 | ||
searched = searchedFormer->nextStudent; | searched = searchedFormer->nextStudent; | ||
searchedFormer->nextStudent = NULL; | searchedFormer->nextStudent = NULL; | ||
aListPointer | aListPointer[TAIL] = searchedFormer; | ||
} | } | ||
else{ //중간일때 | else{ //중간일때 | ||
| Line 138: | Line 136: | ||
aPopulation--; | aPopulation--; | ||
printf("\n현재 남아있는 학생명단\n");//현재 남아있는 명단 출력 | printf("\n현재 남아있는 학생명단\n");//현재 남아있는 명단 출력 | ||
ListOutput(aListPointer | ListOutput(aListPointer[HEAD]); | ||
} | } | ||
else | else | ||
| Line 190: | Line 188: | ||
---- | ---- | ||
[[강희경]] [[LinkedList]] | [[강희경]] [[LinkedList]] | ||
Latest revision as of 12:46, 27 March 2026
강희경이 타과 친구의 부탁으로 대신 해준 숙제. 스펙에 있는 배열로 구현하라는 말을 못 보고 혼자 신나서 링크드 리스트로 구현했다. 이걸 어째...
스팩
메모리 이용 학생관리 프로그램 -배열이용 -student 구조체 사용(dept, name, num(1~20) -기능(콘솔에서 사용자 입력 원소 삽입
( 삭제-num을 이용해서...,
(검색-binary search??, sequential search...)
(option-파일에 쓰고 읽는 부분 첨가)
소스
/*
링크드 리스트를 이용한 학생관리 프로그램
*링크드 리스트
학생->학생->학생->NULL
*/
#include <stdio.h>
#define HEAD 0
#define TAIL 1
#define ORIGINALSEARCH 0
#define DELETIONSEARCH 1
struct Student{
char dept[10];
char name[10];
int number;
struct Student* nextStudent;
};
typedef struct Student Student;
int Menu(int aPopulation);//메뉴선택
int Process(int aMenu, int aPopulation, Student* aListPointer[]);
int AddStudent(int aPopulation, Student* aListPointer[]);//새로운 학생 추가
void SearchStudent(Student* aHead);//단순 찾기
int DelStudent(int aPopulation, Student* aListPointer[]);//찾아서 지우기
void InputStudentInfo(Student* aStudent);//정보 입력
void FreeMemory(Student* aHead);//메모리 해제
void ListOutput(Student* aHead);//목록 출력
Student* Searching(int aNumber, Student* aHead, int aType);//찾기
int main()
{
Student* listPointer[2];//리스트의 머리와 꼬리
int population = 0;//등록된 학생수
do{
population = Process(Menu(population), population, listPointer);
printf("\n");//보기 좋게 한 칸 띈다.
}while(population != -1);//프로그램 종료 조건
FreeMemory(listPointer[HEAD]);//메모리 해제
system("PAUSE");
return 0;
}
int Menu(int aPopulation){
int selectedMenu;
printf("☞안녕하세요. 학생 관리 프로그램입니다.\n");
printf("1. 학생 정보 추가\n");
if(aPopulation){//등록된 학생이 있으면
printf("2. 학생 정보 검색\n");
printf("3. 학생 정보 삭제\n");
}
printf("4. 종료\n");
printf("\n메뉴를 선택해 주세요: ");
scanf("%d", &selectedMenu);
return selectedMenu;
}
int Process(int aMenu, int aPopulation, Student* aListPointer[]){
if(aMenu == 1)//추가
aPopulation = AddStudent(aPopulation, aListPointer);
else if(aMenu == 4)//종료
aPopulation = -1;
else if(aPopulation){//등록된 학생이 있으면
if(aMenu == 2)//찾기
SearchStudent(aListPointer[HEAD]);
if(aMenu == 3)//삭제
aPopulation = DelStudent(aPopulation, aListPointer);
}
else
printf("\n잘못된 메뉴선택입니다.\n");
return aPopulation;
}
int AddStudent(int aPopulation, Student* aListPointer[]){
Student* newStudent;
newStudent = (Student*)malloc(sizeof(Student));
InputStudentInfo(newStudent);
if(!aPopulation){//첫 등록인 경우
aListPointer[HEAD] = newStudent;
}
else{
aListPointer[TAIL]->nextStudent = newStudent;
}
aListPointer[TAIL] = newStudent;
aPopulation++;
printf("\n현재 등록된 학생명단\n");//현재 등록되어 있는 명단을 출력한다.
ListOutput(aListPointer[HEAD]);
return aPopulation;
}
void SearchStudent(Student* aHead){
int searchNumber;
Student* searched;
printf("\n검색할 학생의 번호를 입력하세요: ");
scanf("%d", &searchNumber);
searched = Searching(searchNumber, aHead, ORIGINALSEARCH);
if(searched)
printf("검색 성공!\n부서: %s\n이름: %s\n번호: %d\n",
searched->dept, searched->name, searched->number);
else
printf("검색 실패, 없는 번호입니다.\n");
}
int DelStudent(int aPopulation, Student* aListPointer[]){
int deleteNumber;
Student* searchedFormer;
Student* searched;
printf("\n삭제할 학생의 번호를 입력하세요: ");
scanf("%d", &deleteNumber);
searchedFormer = Searching(deleteNumber, aListPointer[HEAD], DELETIONSEARCH);
if(!searchedFormer || searchedFormer->nextStudent){
if(!searchedFormer){ //가장 처음 일때
searched = aListPointer[HEAD];
aListPointer[HEAD] = searched->nextStudent;
}
else if(!(searched->nextStudent)){//끝 일때
searched = searchedFormer->nextStudent;
searchedFormer->nextStudent = NULL;
aListPointer[TAIL] = searchedFormer;
}
else{ //중간일때
searched = searchedFormer->nextStudent;
searchedFormer->nextStudent = searched->nextStudent;
}
free(searched);//메모리 해제
aPopulation--;
printf("\n현재 남아있는 학생명단\n");//현재 남아있는 명단 출력
ListOutput(aListPointer[HEAD]);
}
else
printf("삭제 실패, 없는 번호입니다.\n");
return aPopulation;
}
void InputStudentInfo(Student* aStudent){
printf("부서: ");
scanf("%s", aStudent->dept);
printf("이름: ");
scanf("%s", aStudent->name);
printf("번호: ");
scanf("%d", &(aStudent->number));
aStudent->nextStudent = NULL;
}
void FreeMemory(Student* aHead){
Student* temp;
while(aHead){//링크를 쭉~따라가면서 해제
temp = aHead;
free(temp);
aHead = aHead->nextStudent;
}
}
void ListOutput(Student* aHead){
int counter = 0;
Student* temp;
temp = aHead;
while(temp){//링크를 쭉~따라가면서 출력
counter++;
printf("%s\n",temp->name);
temp = temp->nextStudent;
}
printf("현재 총 %d명의 학생이 등록되었습니다.\n", counter);
}
Student* Searching(int aNumber, Student* aHead, int aType){
Student* searched = aHead;
Student* searchedFormer = NULL;
while(searched){////링크를 쭉~따라가면서 검색
if(searched->number == aNumber)
break;
else{
searchedFormer = searched;
searched = searched->nextStudent;
}
}
if(aType == ORIGINALSEARCH)
return searched;
else
return searchedFormer;
}