-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask1
More file actions
88 lines (74 loc) · 1.63 KB
/
Copy pathtask1
File metadata and controls
88 lines (74 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data; // данные
struct node *next; //адрес следующего узла
} node;
typedef struct queue // хранение кол-ва узлов, передней и задней части
{
node *head;
node *tail;
}queue;
int empty(queue *q) // проверка пуста ли очередь. задняя часть null
{
return (q->tail == NULL);
}
void push(queue *q, int value)
{
node *tmp;
tmp = malloc(sizeof(node));
tmp->data = value;
tmp->next = NULL;
if(!empty(q))
{
q->tail->next = tmp;
}
else
{
q->head = tmp;
}
q->tail = tmp;
}
int pop(queue *q)
{
node *tmp;
tmp = malloc(sizeof(node));
int n = q->head->data;
tmp = q->head;
q->head = q->head->next;
if(q->head == NULL){
q->tail = NULL;
}
free(tmp);
return n;
}
int main()
{
int n;
scanf("%d", &n);
queue q;
(&q)->head = NULL; // инициализация
(&q)->tail = NULL;
int arr[n]; //массив, куда закидывается ответ
int count=0;
for (int i=0; i<n; i++){
char oper;
scanf ("%c", &oper);
while(oper != '+' && oper != '-'){
scanf ("%c", &oper);
}
if (oper == '+'){
int value;
scanf("%d", &value);
push(&q, value);
} else {
arr[count] = pop(&q);
count ++;
}
}
for (int i = 0; i < count; i++){
printf ("%d \n", arr[i]);
}
return 0;
}