C언어 링크드리스트 구현

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 같이 보기[편집]

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