U E D R , A S I H C RSS

Linked List/학생관리프로그램

. 는 배 는 말 못 보 . ...

{{|
메모리
-배
-student (dept, name, num(1~20)
-능(
( -num ...,
(-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;       
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:38
Processing time 0.0111 sec