Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uff-Atividades-pilha-fila
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alan de Oliveira
uff-Atividades-pilha-fila
Commits
83e791e6
Commit
83e791e6
authored
Jun 13, 2022
by
Alan de Oliveira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Comentários Completos
parent
5769c424
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
91 additions
and
27 deletions
+91
-27
exercicio5.cpp
exercicio5.cpp
+57
-23
exercicio6.cpp
exercicio6.cpp
+20
-0
exercicio7.cpp
exercicio7.cpp
+14
-4
No files found.
exercicio5.cpp
View file @
83e791e6
...
...
@@ -11,6 +11,7 @@ template <class T, class Container=container<T>>
class
Fila
{
public
:
void
enfilera
(
T
elem
)
{
N
++
;
auto
cont
=
new
container
<
T
>
();
cont
->
elemento
=
elem
;
...
...
@@ -26,6 +27,7 @@ class Fila {
T
desenfilera
()
{
if
(
inicio
!=
nullptr
)
{
N
--
;
T
elemento
=
inicio
->
elemento
;
Container
*
i
=
inicio
;
inicio
=
inicio
->
proximo
;
...
...
@@ -44,8 +46,15 @@ class Fila {
else
return
-
1
;
}
int
tamanho
()
{
return
N
;
}
private
:
int
N
=
0
;
Container
*
inicio
=
nullptr
;
Container
*
fim
=
nullptr
;
...
...
@@ -75,6 +84,10 @@ class Pilha {
else
return
-
1
;
}
int
tamanho
()
{
return
N
;
}
private
:
static
const
int
MAX
=
900
;
int
N
=
0
;
...
...
@@ -82,44 +95,65 @@ class Pilha {
};
template
<
class
T
>
Fila
<
T
>
inverterFila
(
Fila
<
T
>
fila
)
{
Fila
<
T
>
filaAux
,
filaInvertida
;
/**
* @brief
* Para inverter uma fila utilizando duas filas:
* A idéia é percorrer toda a fila e pegando apenas o último elemento, e repetindo até finalizar
* 1º Passo: Enfilero os itens em uma fila auxiliar, salvando o último elemento
* 2° Passo: Enfilero o último elemento numa segunda fila auxiliar (essa fila que vai armazenar a fila invertida)
* 3° Passo: Volto para a fila original todos os ítens, exceto o último elemento
* 4º Passo: Verifico se chegou ao fim, se não chegou repito os passos
*
* @tparam T
* @param fila
*/
template
<
class
T
>
void
inverterFila
(
Fila
<
T
>
*
fila
)
{
Fila
<
T
>
filaAux
,
filaAux2
;
T
ultimo
;
bool
fim
=
false
;
while
(
!
fim
)
{
while
(
fila
.
frente
()
!=
-
1
)
{
ultimo
=
fila
.
frente
();
filaAux
.
enfilera
(
fila
.
desenfilera
());
while
(
fila
->
frente
()
!=
-
1
)
{
ultimo
=
fila
->
frente
();
filaAux
.
enfilera
(
fila
->
desenfilera
());
}
fila
Invertida
.
enfilera
(
ultimo
);
fila
Aux2
.
enfilera
(
ultimo
);
while
(
filaAux
.
frente
()
!=
-
1
&&
filaAux
.
frente
()
!=
ultimo
)
{
fila
.
enfilera
(
filaAux
.
desenfilera
());
fila
->
enfilera
(
filaAux
.
desenfilera
());
}
filaAux
.
desenfilera
();
if
(
fila
.
frente
()
==
-
1
)
if
(
fila
->
frente
()
==
-
1
)
fim
=
true
;
}
return
filaInvertida
;
while
(
filaAux2
.
frente
()
!=
-
1
)
{
fila
->
enfilera
(
filaAux2
.
desenfilera
());
}
}
template
<
class
T
>
Pilha
<
T
>
inverterFilaComPilha
(
Fila
<
T
>
fila
)
{
/**
* @brief Inverter uma fila utilizando uma pilha.
*
* Simplesmente quando empilhamos os elementos da fila, e quandos desempilhamos, enfileirando
* na fila, automaticamente os elementos estarão invertidos.
*
* @tparam T
* @param fila
*/
template
<
class
T
>
void
inverterFilaComPilha
(
Fila
<
T
>
*
fila
)
{
Pilha
<
T
>
pilha
;
while
(
fila
.
frente
()
!=
-
1
)
{
pilha
.
empilha
(
fila
.
desenfilera
());
while
(
fila
->
frente
()
!=
-
1
)
{
pilha
.
empilha
(
fila
->
desenfilera
());
}
while
(
pilha
.
topo
()
!=
-
1
)
{
fila
->
enfilera
(
pilha
.
desempilha
());
}
//printarPilha(pilha, "Fila invertida com Pilha");
return
pilha
;
}
...
...
@@ -181,12 +215,12 @@ int main(int argc, char* argv[]) {
preencherFila
(
fila
);
fila
=
inverterFila
(
fila
);
printarFila
(
fila
,
"Fila invertida com
outra fila ???
"
);
inverterFila
(
&
fila
);
printarFila
(
fila
,
"Fila invertida com
duas filas
"
);
preencherFila
(
fila
);
Pilha
<
char
>
pilha
=
inverterFilaComPilha
(
fila
);
printar
Pilha
(
pilh
a
,
"Fila invertida com Pilha"
);
inverterFilaComPilha
(
&
fila
);
printar
Fila
(
fil
a
,
"Fila invertida com Pilha"
);
...
...
exercicio6.cpp
View file @
83e791e6
...
...
@@ -31,6 +31,26 @@ class Pilha {
};
/**
* @brief
* Para fazer o algorítimo da Pilha Min
* é necessário mantermos duas pilas, uma pilha onde armazenamos os elementos
* e outra pilha onde armazenamos os elementos na ordem mínima.
*
* Para fazermos isso, precisamos verificar a cada inserção na pilha se o elemento
* que está sendo inserido é menor do que o que já está no topo da pilha mínima.
* Se o elemento for menor, inserimos ele na pilha mínima
* caso o elemento for maior, inserimos novamente o topo da pilha mínima nela mesma
*
* dessa forma a quantidade de ítens ficam sincronizados, e mantemos sempre o último
* elemento mínimo atualizado.
*
* para assegurar que o elemento esteja sincronizado, quando removemos algum íten
* da pilha principal, devemos também remover o topo da pilha mínima.
*
* @tparam T
*/
template
<
class
T
>
class
PilhaMin
{
...
...
exercicio7.cpp
View file @
83e791e6
...
...
@@ -93,10 +93,18 @@ bool operador(char s) {
if
(
o
==
s
)
return
true
;
}
return
false
;
}
/**
* @brief Para fazer uma conversão de expressão para polonesa reversa.
* Devemos percorrer os caracteres da expressão verificando:
* Caso: Se o caracter for um número, adicionamos ele em uma fila
* Caso: Se o caracter for um operador, adicionamos em uma pilha
* Caso: Se o caractere for um ')', desempilhamos os operadores enfileirando na fila da expressão
*
* @param expr
* @return string
*/
string
polonisar
(
string
expr
)
{
auto
stream
=
stringstream
{
expr
};
...
...
@@ -117,13 +125,15 @@ string polonisar(string expr) {
else
fila_retorno
.
enfilera
(
aux
);
}
while
(
operadores
.
topo
()
!=
-
1
){
fila_retorno
.
enfilera
(
operadores
.
desempilha
());
}
string
exp_retorno
;
while
(
fila_retorno
.
frente
()
!=
-
1
)
{
exp_retorno
+=
fila_retorno
.
desenfilera
();
}
return
exp_retorno
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment