ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 연결 리스트
    C연습 2021. 1. 17. 11:41
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int element;
    typedef struct {
    	element data;
    	struct ListNode *link;
    } ListNode;
    
    void error(char *message)
    {
    	fprintf(stderr, "%s\n", message);
    	exit(1);
    }
    
    ListNode* insert_first(ListNode  *head, int value) {
    	ListNode *p = (ListNode *)malloc(sizeof(ListNode));
    	p->data = value;
    	p->link = head;
    	head = p;
    	return head;
    }
    
    ListNode* insert(ListNode  *head, int value) {
    	ListNode *p = (ListNode *)malloc(sizeof(ListNode));
    	p->data = value;
    	p->link = NULL;
    	head->link = p;
    	return head;
    }
    
    int count_ListNode(ListNode *head) {
    	int ListNode_count_num = 0;
    
    	for (ListNode *p = head; p != NULL; p = p->link)
    	{
    		ListNode_count_num++;
    	}
    	
    	return ListNode_count_num;
    }
    int main()
    {
    	ListNode *head = NULL;
    	int count = 0;
    	int value = 0;
    
    	printf("노드의 개수 : ");
    	scanf_s("%d", &count);
    
    	for (int i = 0; i < count; i++)
    	{
    		printf("노드 #%d 데이터 : ", i + 1);
    		scanf_s("%d", &value);
    		head = insert_first(head, value);
    	}
    
    	printf("연결 리스트 노드의 개수: %d", count_ListNode(head));
    
    	return 0;
    }

    'C연습' 카테고리의 다른 글

    이진 검색 트리  (0) 2021.01.17
    원형큐_소수고리  (0) 2021.01.17
    stack 괄호 짝 맞추기  (0) 2021.01.17
    stack  (0) 2021.01.17
    수열의 합  (0) 2021.01.17

    댓글

Designed by Tistory.