개념/자료구조

Linked-list 구현하기 (정렬, 소멸)

웅드 2023. 11. 28. 17:33

sortList함수

  • FunctionName
    • sortList - 노드 정렬 (오름차순)
  • Parameters
    • lp - 리스트 관리 구조체의 주소
  • Returns
    • 없다.
void shortList(List *lp){

    Node *curp;
    Node *nextp;
    int temp;
    
    if(lp==NULL) return;
    
    curp = lp -> head -> next;
    while(curp->next != lp -> tail){
    	nextp = curp -> next;
        while(nextp != lp -> tail){
        	if(curp -> data > nextp -> data){
            	temp = curp -> data;
                curp -> data = nextp -> data;
                nextp -> data = temp;
            }
            nextp = nextp -> next;
        }
        curp = curp -> next;
    }
}

 

destroyList함수

  • FunctionName
    • destroyList - 리스트 내의 모든 node를 삭제
  • Parameters
    • lp - 리스트 관리 구조체의 주소
  • Returns
    • 없다.
void destoryList(List *lp){

    Node *curp;
    Node *nextp;
    
    if(lp==NULL) return;
    
    curp = lp -> head -> next
    
    while(curp != lp -> tail){ // tail node까지 curp를 옮기면서 node를 하나씩 제거한다.
    	nextp = curp -> next;
        free(curp);
        curp = nextp;
    }
    free(lp -> head);
    free(lp -> tail);
    
    lp -> head = lp -> tail = NULL;
    lp -> size = 0;
    return 0;
}
반응형