fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct Node
  7. {
  8. char elem;
  9. struct Node *prev;
  10. struct Node *next;
  11. }Node; // 노드 정의
  12.  
  13. typedef struct List
  14. {
  15. struct Node *header;
  16. struct Node *trailer;
  17. }List; // 리스트 정의
  18.  
  19. void init_list(List* list);
  20. void add(List list, int r, char e);
  21. void delete(List list, int r);
  22. void get_entry(List list, int r);
  23. void print(List list);
  24.  
  25. int main(void)
  26. {
  27. List list; // 리스트 선언
  28.  
  29. char op, item; // 입력 연산의 종류, 아이템
  30. int position; // 입력 위치
  31. int num_op; // 연산의 개수
  32.  
  33. init_list(&list); // 리스트 초기화 (헤더, 트레일러 노드 할당 및 초기화)
  34.  
  35. scanf("%d", &num_op); // 연산 개수 입력
  36.  
  37. for (int i = 0; i < num_op; ++i)
  38. {
  39. getchar(); // 공백 또는 개행문자 읽기
  40. scanf("%c", &op); // 연산 종류
  41.  
  42. switch (op)
  43. {
  44. case 'A': // 삽입
  45. scanf("%d %c", &position, &item);
  46. add(list, position, item);
  47. break;
  48. case 'D': // 삭제
  49. scanf("%d", &position);
  50. delete(list, position);
  51. break;
  52. case 'G': // 참조
  53. scanf("%d", &position);
  54. get_entry(list, position);
  55. break;
  56. case 'P': // 출력
  57. print(list);
  58. break;
  59. }
  60. }
  61.  
  62. free(list); // 리스트의 노드 해제
  63.  
  64. return 0;
  65. }
  66.  
  67. void init_list(List* list)
  68. {
  69. Node *H;
  70. Node *T;
  71. H = (Node*)malloc(sizeof(Node));
  72. T = (Node*)malloc(sizeof(Node));
  73.  
  74. list->header = H;
  75. H->prev = NULL;
  76. H->next = T;
  77.  
  78. list->trailer = T;
  79. T->prev = H;
  80. T->next = NULL;
  81. }
  82.  
  83. void add(List list, int r, char e)
  84. {
  85. Node *p;
  86. p = list.header;
  87.  
  88. Node *newp = (Node*)malloc(sizeof(Node));
  89. newp->elem = e;
  90.  
  91. int i;
  92. for(i=0; i<r ; i++)
  93. {
  94. p = p->next;
  95. }
  96. if(p->elem == ' ')
  97. {
  98. printf("invalid position\n");
  99. return;
  100. }
  101. newp->next = p;
  102. newp->prev = p->prev;
  103. p->prev = newp;
  104. newp->prev->next = newp;
  105. }
  106.  
  107. void delete(List list, int r)
  108. {
  109. Node *p;
  110. p = list.header;
  111.  
  112. int i;
  113. for(i=0; i<r ; i++)
  114. {
  115. p = p->next;
  116. }
  117. if(p->elem == ' ')
  118. {
  119. printf("invalid position\n");
  120. return;
  121. }
  122. p->prev->next = p->next;
  123. p->next->prev = p->prev;
  124. free(p);
  125. }
  126.  
  127. void get_entry(List list, int r)
  128. {
  129. Node *p;
  130. p = list.header;
  131.  
  132. int i;
  133. for(i=0; i<r ; i++)
  134. {
  135. p = p->next;
  136. }
  137. if(p->elem == ' ')
  138. {
  139. printf("invalid position\n");
  140. return;
  141. }
  142. printf("%c\n", p->elem);
  143. }
  144.  
  145. void print(List list)
  146. {
  147. Node *p;
  148. p = list.header;
  149.  
  150. while(p->elem != NULL)
  151. {
  152. p = p->next;
  153. printf("%c", p->elem);
  154. }
  155. printf("\n");
  156. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
5
A 1 S
A 2 t
A 3 r
A 3 a
P
compilation info
prog.c: In function ‘main’:
prog.c:62:7: error: incompatible type for argument 1 of ‘free’
  free(list); // 리스트의 노드 해제
       ^~~~
In file included from prog.c:4:
/usr/include/stdlib.h:563:25: note: expected ‘void *’ but argument is of type ‘List’ {aka ‘struct List’}
 extern void free (void *__ptr) __THROW;
                   ~~~~~~^~~~~
prog.c: In function ‘print’:
prog.c:150:16: warning: comparison between pointer and integer
  while(p->elem != NULL)
                ^~
prog.c: In function ‘main’:
prog.c:35:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &num_op); // 연산 개수 입력
  ^~~~~~~~~~~~~~~~~~~~
prog.c:40:3: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%c", &op); // 연산 종류
   ^~~~~~~~~~~~~~~~
prog.c:45:4: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %c", &position, &item);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:49:4: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &position);
    ^~~~~~~~~~~~~~~~~~~~~~
prog.c:53:4: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &position);
    ^~~~~~~~~~~~~~~~~~~~~~
stdout
Standard output is empty