Advertisement
nyhryan

Untitled

Aug 22nd, 2023
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | Source Code | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef int element;   // 노드에 담을 자료형
  5. typedef struct _MyNode // 노드 구조체
  6. {
  7.     struct _MyNode* next;
  8.     element data;
  9. } MyNode;
  10.  
  11. typedef struct _Queue  // 큐 구조체
  12. {
  13.     MyNode* front;
  14.     MyNode* rear;
  15. } Queue;
  16.  
  17. typedef struct _Stack  // 스택 구조체
  18. {
  19.     MyNode* top;
  20. } Stack;
  21.  
  22. // 노드 생성 함수
  23. MyNode* createNode(element e)
  24. {
  25.     MyNode* n = (MyNode*)malloc(sizeof(MyNode));
  26.     if (n == NULL)
  27.     {
  28.         fprintf(stderr, "failed to create new node!\n");
  29.         exit(1);
  30.     }
  31.     n->data = e;
  32.     n->next = NULL;
  33.  
  34.     return n;
  35. }
  36.  
  37. // 스택 초기화
  38. void Stack_Init(Stack* s)
  39. {
  40.     s->top = NULL;
  41. }
  42.  
  43. // 스택이 비었으면 1 리턴
  44. int Stack_IsEmpty(Stack* s)
  45. {
  46.     return s->top == NULL;
  47. }
  48.  
  49. // 스택에 삽입
  50. void Stack_Push(Stack* s, element e)
  51. {
  52.     MyNode* newNode = createNode(e);
  53.  
  54.     if (Stack_IsEmpty(s))
  55.         s->top = newNode;
  56.     else
  57.     {
  58.         newNode->next = s->top;
  59.         s->top = newNode;
  60.     }
  61. }
  62.  
  63. // 스택에서 제거
  64. element Stack_Pop(Stack* s)
  65. {
  66.     if (Stack_IsEmpty(s))
  67.     {
  68.         fprintf(stderr, "cannot pop from an empty stack!\n");
  69.         exit(1);
  70.     }
  71.     MyNode* removed = s->top;
  72.     s->top = removed->next;
  73.  
  74.     element removedElement = removed->data;
  75.     free(removed);
  76.     return removedElement;
  77. }
  78.  
  79. // 스택 출력
  80. void Stack_Print(Stack* s)
  81. {
  82.     if (Stack_IsEmpty(s))
  83.         printf("Empty stack\n");
  84.     else
  85.     {
  86.         for (MyNode* i = s->top; i != NULL; i = i->next)
  87.         {
  88.             if (i == s->top)
  89.                 printf("[TOP : %d] ", i->data);
  90.             else
  91.                 printf("%d ", i->data);
  92.            
  93.             printf("-> ");
  94.         }
  95.         printf("NULL\n");
  96.     }
  97. }
  98.  
  99. // 큐 초기화
  100. void Queue_Init(Queue* q)
  101. {
  102.     q->front = q->rear = NULL;
  103. }
  104.  
  105. // 큐가 비었다면 1 리턴
  106. int Queue_IsEmpty(Queue* q)
  107. {
  108.     return q->front == NULL || q->rear == NULL;
  109. }
  110.  
  111. // 큐에 삽입
  112. void Queue_Enqueue(Queue* q, element e)
  113. {
  114.     MyNode* newNode = createNode(e);
  115.  
  116.     if (Queue_IsEmpty(q))
  117.     {
  118.         q->front = q->rear = newNode;
  119.     }
  120.     else
  121.     {
  122.         q->rear->next = newNode;
  123.         q->rear = newNode;
  124.     }
  125. }
  126.  
  127. // 큐에서 제거
  128. element Queue_Dequeue(Queue* q)
  129. {
  130.     if (Queue_IsEmpty(q))
  131.     {
  132.         fprintf(stderr, "cannot removed from an empty queue!\n");
  133.         exit(1);
  134.     }
  135.  
  136.     MyNode* removed = q->front;
  137.     element removedElement = removed->data;
  138.  
  139.     // 큐에 하나만 남았다면
  140.     if (q->front == q->rear)
  141.     {
  142.         // 큐는 이제 빈 상태
  143.         q->front = q->rear = NULL;
  144.         // 원래 첫 노드 제거
  145.         free(removed);
  146.         return removedElement;
  147.     }
  148.     // 두개 이상 있다면 front 노드만 제거하면 됨
  149.     else
  150.     {
  151.         // 큐의 첫 노드를 두번째 노드로 이동
  152.         q->front = removed->next;
  153.         // 원래 첫 노드 제거
  154.         free(removed);
  155.         return removedElement;
  156.     }
  157. }
  158.  
  159. // 큐 출력
  160. void Queue_Print(Queue* q)
  161. {
  162.     if (Queue_IsEmpty(q))
  163.         printf("Empty Queue\n");
  164.     else
  165.     {
  166.         for (MyNode* i = q->front; i != NULL; i = i->next)
  167.         {
  168.             if (q->front == q->rear)
  169.                 printf("[FRONT/REAR: %d] ", i->data);
  170.             else if (i == q->front)
  171.                 printf("[FRONT: %d] ", i->data);
  172.             else if (i == q->rear)
  173.                 printf("[REAR: %d] ", i->data);
  174.             else
  175.                 printf("%d ", i->data);
  176.            
  177.             printf("-> ");
  178.         }
  179.         printf("NULL\n");
  180.     }
  181. }
  182.  
  183. int main()
  184. {
  185.     printf("==== STACK ====\n");
  186.  
  187.     Stack s;
  188.     Stack_Init(&s);
  189.  
  190.     Stack_Push(&s, 10);
  191.     Stack_Push(&s, 20);
  192.     Stack_Push(&s, 30);
  193.     Stack_Print(&s);
  194.  
  195.     Stack_Pop(&s);
  196.     Stack_Pop(&s);
  197.     Stack_Print(&s);
  198.  
  199.     Stack_Pop(&s);
  200.     Stack_Print(&s);
  201.  
  202.     printf("\n==== QUEUE ====\n");
  203.  
  204.     Queue q;
  205.     Queue_Init(&q);
  206.  
  207.     Queue_Enqueue(&q, 10);
  208.     Queue_Enqueue(&q, 20);
  209.     Queue_Enqueue(&q, 30);
  210.     Queue_Print(&q);
  211.  
  212.     Queue_Dequeue(&q);
  213.     Queue_Print(&q);
  214.  
  215.     Queue_Dequeue(&q);
  216.     Queue_Print(&q);
  217.  
  218.     Queue_Dequeue(&q);
  219.     Queue_Print(&q);
  220.  
  221.     return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement