"C언어 링크드리스트 구현"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdio.h>
#include <stdio.h>


10번째 줄: 10번째 줄:
} *head;
} *head;


void add(char* name, int score, int rank) {
void append(char* name, int score, int rank) {
     struct student *newone;
     struct student *newone;
     newone = (struct student *) malloc(sizeof(struct student));
     newone = (struct student *) malloc(sizeof(struct student));
30번째 줄: 30번째 줄:
}
}


int main()
int main() {
{
     append("홍길동", 10, 2);
     add("홍길동", 10, 2);
     append("임꺽정", 20, 1);
     add("임꺽정", 20, 1);
      
      
     struct student *temp;
     struct student *temp;
46번째 줄: 45번째 줄:
     return 0;
     return 0;
}
}
</source>
</syntaxhighlight>
<source lang='text'>
<syntaxhighlight lang='text'>
이름    점수 등수
이름    점수 등수
======== ==== ====
======== ==== ====
  홍길동  10     2
  홍길동  10   1
  임꺽정  20     1
  임꺽정  20   1
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:40 기준 최신판

1 개요[ | ]

#include <stdio.h>

struct student {
    char name[10];
    int score;
    int rank;
    struct student *next;
} *head;

void append(char* name, int score, int rank) {
    struct student *newone;
    newone = (struct student *) malloc(sizeof(struct student));
    strcpy(newone->name, name);
    newone->score = score;
    newone->rank = rank;
    newone->next = NULL;

    if( head == NULL ) {
        head = newone;
        return;
    }
    struct student *cursor;
    cursor = head;
    while( cursor->next != NULL ) {
        cursor = cursor->next;
    }
    cursor->next = newone;
}

int main() {
    append("홍길동", 10, 2);
    append("임꺽정", 20, 1);
    
    struct student *temp;
    temp = head;
    printf( "이름     점수 등수\n");
    printf( "========== ==== ====\n");
    while( temp != NULL ) {
        printf( "%10s %4d  %4d\n", temp->name, temp->score, temp->rank );
        temp = temp->next;
    }
    
    return 0;
}
이름     점수 등수
======== ==== ====
 홍길동   10    1
 임꺽정   20    1

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}