본문 바로가기
DSA

연결리스트 (LinkedList) 활용 2

by KWONE 2024. 10. 29.

구조체 타입 element요소로 data 입력저장하는 연결리스트

구조체 element타입 정의:

typedef struct {
    char name[20];
    int age;
    double height;
}element;
typedef struct {
    element data;
    struct ListNode* link;
}ListNode;

출력:

void print_list(ListNode* head) {
    for(ListNode* p=head;p!=NULL;p=p->link) {
        printf("Name is %s.\n Age is %d.\n Height is %.2lf.\n",p->data.name,p->data.age,p->data.height);
        printf("------>\n");
    }
    printf("NULL\n");
}

메인함수:

int main(void) {
    ListNode* head = NULL;

    head = insert_first(head, (element){"Lee", 20, 1.4});	//(element)타입캐스팅으로 초기화
    head = insert_first(head, (element){"Kim", 34, 1.7});
    head = insert_first(head, (element){"Park", 27, 1.2});
    head = insert_first(head, (element){"Choi", 30, 1.3});
    print_list(head);
    return 0;
}

출력:

'DSA' 카테고리의 다른 글

미로탐색 레벨 순회(Level Order)  (0) 2024.11.17
원형 연결리스트(CircularLinkedList)  (1) 2024.10.29
리스트 2(Linked List)  (0) 2024.10.28
회문(Palindrome) 검사 (덱)  (1) 2024.10.27
회문(Palindrome) 검사 (스택)  (1) 2024.10.27