본문 바로가기
Programming/DataStructure

for문 vs while문 linked list sorting할 때 코드 차이점(가시성)

by soccerman 2020. 2. 26.
반응형

while문과 for문 비교는 Doubly linked list를 sorting하는 것에 관련된 글에서 한번 언급한 적이 있다.

참고자료 : https://soccer-programming.tistory.com/6

 

sorting the element of the doubly linked list (feat.for loop)

참고자료 https://www.javatpoint.com/program-to-sort-the-elements-of-the-doubly-linked-list Program to Sort the Elements of the Doubly Linked List - javatpoint Program to Sort the Elements of the Dou..

soccer-programming.tistory.com

 

위의 글을 게시하기 전에는 for문의 구성요소에 단순 int 변수 사칙연산 혹은 부등호 비교 말고 해본 적이 없었다. for문의 구성요소에 다소 복잡한 식을 넣는 것이 굉장히 불편하고 오바한다는 느낌을 받았다.

 

하지만 한 두번 사용하고 나니 짧은 프로그래밍 경력이지만 Doubly or circular linked list 를 sorting 할 때에는 while문보다 for문을 사용하는 것이 더 편한 것 같다. 가시성 면에서도 월등하다고 나는 생각한다.

 

circular linked list를 sorting하는 것을 while 문과 for문으로 구현하는 것을 비교해보자.

 

*circular linked list : 마지막 tail node가 head node를 next pointer로 point하는 리스트 구조, circulation이 존재

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void sort(){
    struct node *current = head;
    struct node *temp = NULL;
    int swap = 0;
    
    if(head==NULL){
        return;
    }
 
while(current->next!=head){
        cur_temp=current->next;
        while(cur_temp!=head){
            if(current->data > cur_temp->data){
            temp = current->data;
            current->data = cur_temp->data;
            cur_temp->data = temp;
            }
            cur_temp=cur_temp->next;
        }
        current=current->next;
    }
 

void sort(){

    struct node *current = head;

    struct node *temp = NULL;

    int swap = 0;

    

    if(head==NULL){

        return;

    }

    

    for(current=head ; current->next!=head ; current=current->next){

        for(temp = current->next ; temp!=head ; temp = temp->next){

            if(temp->data < current->data){

                swap = temp->data;

                temp->data = current->data;

                current->data = swap;

            }

        }

    }

}

 

 

 while 문으로 코드를 짤 경우에 첫번째 while문의 구성요소 즉, 앞쪽에 위치한 cur_temp = current->next 와 가장 뒤쪽에 위치한 current=current->next가 떨어져 있어 코드가 길어질 경우 구조 파악이 복잡해진다.

 반면 for문의 경우에는 for문의 괄호 안에 해당 loop의 조건과 업데이트 사항이 다 포함되어 있어 코드가 길어지더라도 파악하기가 용이하다.

개인차가 있을 수 있지만 나는 개인적으로 for문을 사용하는 것이 코드 작성이나 코드를 이해하는 부분에서 더 낫다고 생각한다.

 

*for문 구성요소

for(첫 loop때 사용되는 변수 선언 ; 조건 ; loop마다 업데이트되는 사항)

{

loop

}

 

plus.

원문의 링크에서는 do-while문을 사용하여 circular linked list를 sorting했는데 이는 더욱 코드를 이해하기가 어렵다.

링크 : https://www.javatpoint.com/program-to-sort-the-elements-of-the-circular-linked-list

 

Program to Sort the Elements of the Circular Linked List - javatpoint

Program to Sort the Elements of the Circular Linked List on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

www.javatpoint.com

코드 : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void sortList() {  
//Current will point to head  
struct node *current = head, *index = NULL;  
int temp;  
if(head == NULL) {  
printf("List is empty");  
}  
else {  
do{  
//Index will point to node next to current  
index = current->next;  
while(index != head) {  
//If current node is greater than index data, swaps the data  
if(current->data > index->data) {  
temp =current->data;  
current->data= index->data;  
index->data = temp;  
}  
index= index->next;  
}  
current =current->next;  
}while(current->next != head);   
}  
}  

 

do-while 문은 circular linked list를 sorting할 때 사용하기에는 코드를 보는 면에서 복잡해서 별로인 것 같다. do-while문의 구조가 아래와 같은데,

do{

    loop

}while();

위의 코드와 같이 이 구조 안에 다른 while이 존재하면 코드를 이해하는데 조금 불편함이 있기 때문이다.

하지만 단순히 circular linked list를 traverse할 때에는 do-while 문 만큼 좋은 것이 없다.

참고링크 : https://soccer-programming.tistory.com/7

 

To Create circular linked list and count . (feat. do-while)

참고자료 https://www.javatpoint.com/program-to-create-a-circular-linked-list-of-n-nodes-and-count-the-number-of-nodes Program to Create a Circular Linked List of N Nodes and Count the Number of Node..

soccer-programming.tistory.com

 

 

반응형

댓글