C언어 링크드리스트 구현

Jmnote (토론 | 기여)님의 2017년 10월 30일 (월) 22:17 판 (새 문서: ==개요== <source lang='c'> /****************************************************************************** Online C Compiler. Code, Comp...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

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

void add(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()
{
    add("홍길동", 10, 1);
    add("임꺽정", 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 }}