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.0199 sec