강경 과 구 부로 대 . 는 배로 구라는 말 못 보고 나 링드 리로 구다. ...
¶
{{|
메모리 관리 로그램
-배
-student 구 (dept, name, num(1~20)
-기능( 력
메모리 관리 로그램
-배
-student 구 (dept, name, num(1~20)
-기능( 력
( -num ...,
(검-binary search??, sequential search...)
(option- 고 는 부 가)
|}}(검-binary search??, sequential search...)
(option- 고 는 부 가)
¶
~cpp /* 링드 리를 관리 로그램 *링드 리 ->->->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; }