VioletaBabel
10828번: 스택 본문
#include <stdio.h>
#include <stdlib.h>//malloc
#include <string.h>
#define mal (node *)malloc(sizeof(node))
typedef struct _node
{
int data;
struct _node *next;
}node;
node *head, *end, *indexnode;
int count = 0;
void push(int num);
void pop();
void size();
void empty();
void top();
void push(int num)
{
indexnode = (node *)malloc(sizeof(node));
indexnode->data = num;
indexnode->next = head->next;
head->next = indexnode;
++count;
}
void pop()
{
int num;
if (count > 0)
{
indexnode = head->next;
num = indexnode->data;
head->next = indexnode->next;
free(indexnode);
--count;
printf("%d\n", num);
}
else printf("-1\n");
}
void size()
{
printf("%d\n", count);
}
void empty()
{
if (count == 0) printf("1\n");
else printf("0\n");
}
void top()
{
if (count == 0) printf("-1\n");
else printf("%d\n", head->next->data);
}
int main()
{
head = mal;
end = mal;
int N, a;
char command[6];
head->next = end;
end->next = end;
scanf("%d", &N);
for (int i = 0; i < N; ++i)
{
scanf("%s", &command);
if (strcmp(command, "push") == 0)
{
scanf("%d", &a);
push(a);
}
else if (strcmp(command, "pop") == 0)
pop();
else if (strcmp(command, "size") == 0)
size();
else if (strcmp(command, "empty") == 0)
empty();
else if (strcmp(command, "top") == 0)
top();
}
}
'백준 > 백준-C' 카테고리의 다른 글
| 2750번: 수 정렬하기 (0) | 2017.05.02 |
|---|---|
| 11866번: 조세퍼스 문제 (0) | 2017.04.25 |
| 10845번: 큐 (0) | 2017.04.21 |
| 9012번: 괄호 (0) | 2017.04.20 |
| 1874번:스택 수열 (0) | 2017.04.19 |