Skip to content

Commit 3bb5b48

Browse files
committed
done with recreio and stack_of_lists
1 parent 756fa63 commit 3bb5b48

File tree

3 files changed

+214
-4
lines changed

3 files changed

+214
-4
lines changed

recreio.c

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
#define DEBUG if(0)
6+
//https://thehuxley.com/problem/414/code-editor/?quizId=6124
7+
typedef struct _node
8+
{
9+
int person;
10+
int priority;
11+
struct _node* next;
12+
}node;
13+
typedef struct queue
14+
{
15+
int size;
16+
node* head;
17+
}Q;
18+
Q* initQ(){
19+
Q* new_q = (Q*) malloc(sizeof(Q));
20+
new_q->size = 0;
21+
new_q->head = NULL;
22+
return new_q;
23+
}
24+
int isEmpty(Q* q)
25+
{
26+
return (q->head == NULL);
27+
}
28+
void priorityEnqueue(Q* q, int person, int priority)
29+
{
30+
node* newT = (node*) malloc(sizeof(node));
31+
newT->person = person;
32+
newT->priority = priority;
33+
if ((isEmpty(q)) || (priority > q->head->priority))
34+
{
35+
newT->next = q->head;
36+
q->head = newT;
37+
//printf("primeiro caso\n");
38+
}
39+
else
40+
{
41+
node* tmp = q->head;
42+
while ((tmp->next != NULL) && (tmp->next->priority >= priority))
43+
{
44+
tmp = tmp->next;
45+
}
46+
newT->next = tmp->next;
47+
tmp->next = newT;
48+
}
49+
//printf("pessoa: [%d] | prioridade [%d]\n",person,priority);
50+
}
51+
void printer(Q* q)
52+
{
53+
node* tmp = q->head;
54+
while (tmp != NULL)
55+
{
56+
printf("[%d,%d]→",tmp->priority,tmp->person);
57+
tmp = tmp->next;
58+
}
59+
}
60+
void flusher(Q* q)
61+
{
62+
while (q->head != NULL)
63+
{
64+
node* current = q->head->next;
65+
free(q->head);
66+
q->head = current;
67+
}
68+
69+
}
70+
int comp(Q* q, int n[], int size)
71+
{
72+
//printf("começo da comp\n");
73+
int ans = 0;
74+
int i = 0;
75+
node* tmp = q->head;
76+
while (tmp != NULL && i<size)
77+
{
78+
if (tmp->person == i)
79+
{
80+
//printf("[%d] != [%d]\n", tmp->person, i);
81+
ans++;
82+
}
83+
i++;
84+
tmp = tmp->next;
85+
}
86+
return ans;
87+
}
88+
int main() {
89+
int cases,du,zi;
90+
scanf("%d",&cases); //gets the number of tests
91+
Q* q = initQ();
92+
for (int i = 0; i < cases ; i++)
93+
{
94+
scanf("%d",&zi); //number of students
95+
int input[zi];
96+
for (int i = 0; i < zi; i++)
97+
{
98+
scanf("%d",&input[i]);
99+
priorityEnqueue(q,i,input[i]);
100+
DEBUG printer(q);
101+
DEBUG printf("\n");
102+
}
103+
du = comp(q,input,zi);
104+
printf("%d\n",du);
105+
flusher(q);
106+
}
107+
return 0;
108+
}

stack_of_lists.c

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
#define MAX_LENGHT 100011
6+
#define DEBUG if(0)
7+
//https://thehuxley.com/problem/548/code-editor/?quizId=6124
8+
typedef struct _node
9+
{
10+
char data[MAX_LENGHT];
11+
struct _node* next;
12+
}node;
13+
14+
typedef struct _stack
15+
{
16+
node* top;
17+
}stack;
18+
19+
stack* init()
20+
{
21+
stack* tito = (stack*) malloc(sizeof(stack));
22+
tito->top = NULL;
23+
return tito;
24+
}
25+
void push(stack* stack, char data[])
26+
{
27+
node* new_node = (node*) malloc (sizeof(node));
28+
strcpy(new_node->data,data);
29+
new_node->next = stack->top;
30+
stack->top = new_node;
31+
32+
}
33+
void pop(stack* stack)
34+
{
35+
if(stack->top == NULL){printf("EMPTY STACK\n"); return;}
36+
node* tmp = stack->top->next;
37+
DEBUG printf("next = [%p]",tmp);
38+
printf("%s", stack->top->data);
39+
free(stack->top);
40+
stack->top = tmp;
41+
42+
}
43+
int main() {
44+
45+
stack* pile = init();
46+
char input[MAX_LENGHT];
47+
strcmp("PUSH",input);
48+
char* flag;
49+
do
50+
{
51+
flag = fgets(input,MAX_LENGHT,stdin);
52+
DEBUG printf("%s",input);
53+
//fflush(stdin);
54+
//int len = strlen(input);
55+
//if(input[len] != '\n') strcat(input,"\n");
56+
if (!(strcmp("PUSH\n",input)))
57+
{
58+
DEBUG printf("entrou aqui\n");
59+
fgets(input,MAX_LENGHT,stdin);
60+
push(pile,input);
61+
fflush(stdin);
62+
}
63+
else if ((!(strcmp("POP\n",input)))) // || (!(strcmp("POP",input)))
64+
{
65+
DEBUG printf("entrou ali\n");
66+
pop(pile);
67+
68+
}
69+
if (!(strcmp("POP",input)))
70+
{
71+
//printf("blue\n");
72+
pop(pile);
73+
break;
74+
}
75+
if (!(strcmp("PUSH",input)))
76+
{
77+
//printf("ueeasdasgg\n");
78+
79+
fgets(input,MAX_LENGHT,stdin);
80+
push(pile,input);
81+
break;
82+
}
83+
84+
} while (flag != NULL);
85+
86+
87+
return 0;
88+
}
89+
90+
// PUSH
91+
// 5 2 2 2 3 1
92+
// PUSH
93+
// 1 2 3
94+
// PUSH
95+
// 9 8 7 6 5 4 3 2 1
96+
// POP
97+
// POP
98+
// PUSH
99+
// 0 0 0 0 0 1
100+
// PUSH
101+
// 10 22 11 4 2 3
102+
// POP

test.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#include <string.h>
33
#include <math.h>
44
#include <stdlib.h>
5+
#include <time.h>
56

67
int main() {
7-
for (int i = 0; i < 41; i++)
8+
srand(time(NULL));
9+
for (int i = 0; i < 30; i++)
810
{
9-
printf("%d\n",i);
11+
printf("%d ",rand()%1000);
1012
}
11-
12-
1313
return 0;
1414
}

0 commit comments

Comments
 (0)