메뉴 건너뛰기

목록
profile
조회 수 14 댓글 3 예스잼 0 노잼 0

No Attached Image

이제야 알게됨 

C언어에서 화살표연산자는 구조체에서만 사용된다...

그러므로 이제는 연결리스트를 할 수 있게됨 ㅋㅋ

#include <stdio.h>

#include <stdlib.h>

struct NODE {

char* data;

struct NODE* link;

};

int main() {

 

struct NODE* week = (struct NODE*)calloc(1, sizeof(struct NODE));

struct NODE* node1 = (struct NODE*)calloc(1, sizeof(struct NODE));

struct NODE* node2 = (struct NODE*)calloc(1, sizeof(struct NODE));

struct NODE* node3 = (struct NODE*)calloc(1, sizeof(struct NODE));

 

week->link = node1;

node1->data = "MON";

node1->link = node2;

node2->data = "TUE";

node2->link = node3;

node3->data = "WED";

node3->link = NULL;

 

 

struct NODE *curr = week->link;

while (curr != NULL) {

printf("Today is %s\n", curr->data);

curr = curr->link;

}

free(week);

free(node1);

free(node2);

free(node3);

 

return 0; 

}

이거 이해한건 아님. 

  • profile
    그리드 2022.11.23 14:43
    struct NODE* node2 = (struct NODE*)calloc(1, sizeof(struct NODE));
    node2->data = 6;
    node2->link= node1->link;
    node1->link = node2;
  • profile
    그리드 2022.11.23 15:39(수정됨)
    순환 연결리스트는 쉽다.
    시작점을 head로 설정하게 하고
    head = node1;
    끝노드를 node->link = head 로 만들면 됨
  • profile
    그리드 2022.11.23 15:41
    이중 연결리스트는 복잡하기에 그려가면서 이해할 예정