Advertisement
nyhryan

Untitled

Aug 8th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // 리스트에 담을 데이터의 자료형
  5. typedef int element;
  6.  
  7. // 리스트의 노드 구조체
  8. typedef struct _Node
  9. {
  10.     element data;        // 각 노드에 담길 자료
  11.     struct _Node* next;  // 다음 노드
  12. } Node;
  13.  
  14. // 원형 연결리스트의 헤더 구조체
  15. typedef struct _CircularList
  16. {
  17.     Node* head;        // 리스트의 맨 앞 노드
  18.     unsigned int size; // 리스트의 노드 개수
  19. } CircularList;
  20.  
  21. // CircularList 헤더 구조체라는 이름이 기니깐 List라고 별명짓기
  22. typedef CircularList List;
  23.  
  24. // 연결리스트 초기화
  25. void List_Init(List* list)
  26. {
  27.     list->head = NULL;
  28.     list->size = 0;
  29. }
  30.  
  31. // 연결리스트의 노드를 만들어준다.
  32. Node* List_CreateNode(Node* n, element e)
  33. {
  34.     n = (Node*)malloc(sizeof(Node));
  35.     if (n == NULL)
  36.     {
  37.         fprintf(stderr, "malloc() failed while creating new node\n");
  38.         exit(1);
  39.     }
  40.     n->data = e;
  41.     n->next = NULL;
  42.  
  43.     return n;
  44. }
  45.  
  46. // 리스트의 내용물과 노드개수 출력
  47. void List_Print(List* list)
  48. {
  49.     printf(">> ");
  50.     if (list->size == 0)
  51.     {
  52.         printf("Empty list\n");
  53.         return;
  54.     }
  55.  
  56.     Node* head = list->head;
  57.     Node* i;
  58.     for (i = head->next; i != head; i = i->next)
  59.     {
  60.         printf("%d -> ", i->data);
  61.     }
  62.     printf("%d (size: %u)\n", i->data, list->size);
  63.     printf("------------\n");
  64. }
  65.  
  66. // 원형 연결리스트의 맨 앞에 삽입
  67. void List_InsertFirst(List* list, element item)
  68. {
  69.     Node* head = list->head;
  70.  
  71.     Node* newNode = NULL;
  72.     newNode = List_CreateNode(newNode, item);
  73.  
  74.     if (head == NULL)
  75.     {
  76.         head = newNode;
  77.         head->next = newNode;
  78.     }
  79.     else
  80.     {
  81.         newNode->next = head->next;
  82.         head->next = newNode;
  83.     }
  84.  
  85.     list->head = head;
  86.     ++list->size;
  87. }
  88.  
  89. // 원형 연결리스트의 맨 뒤에 삽입
  90. void List_InsertLast(List* list, element item)
  91. {
  92.     Node* head = list->head;
  93.     Node* newNode = NULL;
  94.     newNode = List_CreateNode(newNode, item);
  95.  
  96.     // 빈 리스트일때
  97.     if (head == NULL)
  98.     {
  99.         // 맨 앞과 뒤를 똑같이 첫 노드로 삽입
  100.         head = newNode;
  101.         head->next = newNode;
  102.     }
  103.     // 노드가 1개 이상일 때
  104.     else
  105.     {
  106.         newNode->next = head->next;
  107.         head->next = newNode;
  108.         head = newNode;
  109.     }
  110.     list->head = head;
  111.     ++list->size;
  112. }
  113.  
  114. // 원형 연결리스트의 맨 앞의 요소 제거
  115. element List_RemoveFirst(List* list)
  116. {
  117.     Node* head = list->head;
  118.     element e = -1;
  119.  
  120.     // 빈 리스트에서는 제거할 수 없다.
  121.     if (head == NULL)
  122.     {
  123.         fprintf(stderr, "Cannot remove from an empty list!\n");
  124.         exit(1);
  125.     }
  126.     // 노드가 1개일때 제거하면 빈 리스트가 됨
  127.     else if (list->size == 1)
  128.     {
  129.         e = head->data;
  130.         free(head);
  131.         head = NULL;
  132.         list->size = 0;
  133.     }
  134.     else
  135.     {
  136.         Node* removed = head->next;
  137.         e = removed->data;
  138.         head->next = removed->next;
  139.         free(removed);
  140.         --list->size;
  141.     }
  142.  
  143.     list->head = head;
  144.  
  145.     return e;
  146. }
  147.  
  148.  
  149. int main()
  150. {
  151.     List list;
  152.     List_Init(&list);
  153.  
  154.     List_InsertFirst(&list, 10);
  155.     List_InsertFirst(&list, 20);
  156.     List_InsertFirst(&list, 30);
  157.  
  158.     List_InsertLast(&list, 99);
  159.     List_InsertLast(&list, 88);
  160.     List_InsertLast(&list, 77);
  161.  
  162.     List_Print(&list);
  163.  
  164.     printf("Removed : %d\n", List_RemoveFirst(&list));
  165.     printf("Removed : %d\n", List_RemoveFirst(&list));
  166.     printf("Removed : %d\n", List_RemoveFirst(&list));
  167.     List_Print(&list);
  168.  
  169.     printf("Removed : %d\n", List_RemoveFirst(&list));
  170.     printf("Removed : %d\n", List_RemoveFirst(&list));
  171.     List_Print(&list);
  172.  
  173.     printf("Removed : %d\n", List_RemoveFirst(&list));
  174.     List_Print(&list);
  175.    
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement