728x90
https://www.acmicpc.net/problem/5430
5430번: AC
각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.
www.acmicpc.net
#include <stdio.h> // AC
#include <stdlib.h>
#include <string.h>
typedef struct
{
struct node* next;
struct node* prev;
int data;
}node;
typedef struct
{
node* front;
node* rear;
int cnt;
}DEQUE;
int is_empty(DEQUE* d)
{
if (d->cnt <= 0)
return 1;
else
return 0;
}
void push_front(DEQUE* d, int data)
{
node* newnode = malloc(sizeof(node));
newnode->next = d->front;
newnode->prev = NULL;
newnode->data = data;
if (is_empty(d))
{
d->rear = newnode;
}
else
{
d->front->prev = newnode;
}
d->front = newnode;
d->cnt++;
}
void push_rear(DEQUE* d, int data)
{
node* newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->prev = d->rear;
newnode->data = data;
if (is_empty(d))
{
d->front = newnode;
}
else
{
d->rear->next = newnode;
}
d->rear = newnode;
d->cnt++;
}
int pop_front(DEQUE* d)
{
if (is_empty(d))
return -1;
else
{
node* temp;
temp = d->front;
int data = temp->data;
d->front = temp->next;
if (d->front == NULL)
d->rear = NULL;
else
d->front->prev = NULL;
d->cnt--;
free(temp);
return data;
}
}
int pop_rear(DEQUE* d)
{
if (is_empty(d))
return -1;
else
{
node* temp;
temp = d->rear;
int data = temp->data;
d->rear = temp->prev;
if (d->rear == NULL)
d->front = NULL;
else
d->rear->next = NULL;
d->cnt--;
free(temp);
return data;
}
}
void print_front(DEQUE* d)
{
int check = d->cnt - 1;
printf("[");
while (!is_empty(d))
{
printf("%d", pop_front(d));
if (check > 0)
printf(",");
check--;
}
printf("]\n");
}
void print_rear(DEQUE* d)
{
int check = d->cnt - 1;
printf("[");
while (!is_empty(d))
{
printf("%d", pop_rear(d));
if (check > 0)
printf(",");
check--;
}
printf("]\n");
}
char p[100001];
int main()
{
int t;
scanf("%d", &t);
DEQUE d;
d.front = d.rear = NULL;
d.cnt = 0;
for (int i = 0; i < t; i++)
{
int swap = 0, temp = 0;
scanf("%s", p);
int len = strlen(p);
int n;
scanf("%d", &n);
getchar();
char c;
if (n != 0)
{
// '[' 버리기
scanf(" %c", &c);
for (int j = 0; j < n; j++)
{
int num;
scanf("%d", &num);
// ',' 및 ']' 버리기
char trash;
scanf("%c", &trash);
push_rear(&d, num);
}
}
else
{
// '[', ']' 받기
scanf("%c %c", &c, &c);
}
for (int j = 0; j < len; j++)
{
if (p[j] == 'R')
{
swap = !swap;
}
else if (p[j] == 'D')
{
if (swap == 0)
{
temp = pop_front(&d);
}
else
{
temp = pop_rear(&d);
}
}
}
if (temp == -1)
printf("error\n");
else
{
if (swap == 0)
print_front(&d);
else
print_rear(&d);
}
}
return 0;
}
MEMO
-- 덱을 활용, swap 변수를 통해서 앞에서 빼거나 뒤에서 뺄지, 앞에서부터 출력할지 뒤에서부터 출력할지 결정
-- 직접 값의 위치를 변경하는 방법으로 하다가 힘들어서 구글링을 통해 해결
!! scanf() 사이에 getchar()을 통해서 버퍼 지워주는 점 기억하기 !!
728x90
728x90
'백준[baekjoon] > C언어' 카테고리의 다른 글
백준(baekjoon) [C] - 1012번: 유기농 배추 (0) | 2023.02.01 |
---|---|
백준(baekjoon) [C] - 1302번: 베스트셀러 (0) | 2023.01.31 |
백준(baekjoon) [C] - 1715번: 카드 정렬하기 (0) | 2023.01.30 |
백준(baekjoon) [C] - 11286번: 절댓값 힙 (0) | 2023.01.30 |
백준(baekjoon) [C] - 1620번: 나는야 포켓몬 마스터 이다솜 (0) | 2023.01.28 |