반응형
#include<iostream>
#include<string>
#include <stdlib.h>
using namespace std;
typedef struct node
{
int value;
node *next;
}node;
typedef struct Que
{
node *front = 0;
node *end=0;
int cnt;
}Que;
void init(Que *que)
{
que->front=que->end=NULL; //front:꺼내, end:새로넣어
que->cnt = 0;
}
int isEmpty(Que *que)
{
if (que->cnt == 0)return 1;
else return 0;
}
void push(Que *que, int data)
{
node *now = (node *)malloc(sizeof(node)); //새로운 노드
now->value = data;
now->next = NULL;
if (isEmpty(que))
{
que->front= now;
}
else
{
que->end->next= now;
}
que->end= now;
que->cnt += 1;
}
int pop(Que *que)
{
if (isEmpty(que))return -1;
if (que->front == que->end) {
que->end = NULL;
}
node *cur=NULL;
cur = que->front;
que->front = cur->next;
que->cnt -= 1;
free(cur);
return 0;
}
int Size(Que *que)
{
return que->cnt;
}
int front(Que *que)
{
if (isEmpty(que))return -1;
else return que->front->value;
}
int back(Que *que)
{
if (isEmpty(que))return -1;
else return que->end->value;
}
int main()
{
Que que;
init(&que);
int n = 0, data = 0;
string input;
cin >> n;
while (n--)
{
cin >> input;
if (input == "push")
{
cin >> data;
push(&que,data);
}
else if (input == "pop")
{
cout << front(&que) << endl;
pop(&que);
}
else if (input == "size")
{
cout << Size(&que) << endl;
}
else if (input == "empty")
{
cout << isEmpty(&que) << endl;
}
else if (input == "front")
{
cout << front(&que) << endl;
}
else if (input == "back")
{
cout << back(&que) << endl;
}
}
}
반응형
'알고리즘문제풀이' 카테고리의 다른 글
[백준 2042] 구간 합 구하기 (0) | 2019.10.14 |
---|---|
[백준11438 ]LCA2 (0) | 2019.10.11 |
[백준1158]조세퍼스문제 -연결리스트로 구현해보기 (0) | 2019.10.07 |
KMP알고리즘-문자열 찾기 (0) | 2019.10.01 |
next_permutation STL 구현 (1) | 2019.09.30 |