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
5820bb18
Commit
5820bb18
authored
Jun 12, 2022
by
Alan de Oliveira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Comentando exercício 4
parent
6a0fe149
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
19 deletions
+67
-19
exercicio4.cpp
exercicio4.cpp
+67
-19
No files found.
exercicio4.cpp
View file @
5820bb18
#include <iostream>
#include <iostream>
template
<
class
D
>
class
container
{
template
<
class
D
>
class
container
{
public
:
public
:
container
*
proximo
;
container
*
proximo
;
...
@@ -75,6 +76,10 @@ class Pilha {
...
@@ -75,6 +76,10 @@ class Pilha {
else
else
return
-
1
;
return
-
1
;
}
}
int
tamanho
()
{
return
N
;
}
private
:
private
:
static
const
int
MAX
=
900
;
static
const
int
MAX
=
900
;
int
N
=
0
;
int
N
=
0
;
...
@@ -82,45 +87,88 @@ class Pilha {
...
@@ -82,45 +87,88 @@ class Pilha {
};
};
template
<
class
T
>
Pilha
<
T
>
inverterPilha
(
Pilha
<
T
>
pilha
)
{
/**
* Dado uma pilha, inverter ela utilizando uma pilha auxiliar
*
* para isso eu percorro toda a pilha
* 1° passo: Guardo o topo da pilha numa aux
* 2° passo: Desempilha o primeiro ítem da pilha
* 3° passo: Passo (todos os itens que ainda não foram trocados) da pilha para a pilha auxiliar
* 4° passo: Empilho o item salvo no aux para ficar como base da pilha.
* dessa forma, o que eu fiz foi inverter o elemento que estava
* no topo, indo para o começo.
* 5° passo: volto (todos os ítens que ainda não foram trocados) da pilhaAux para a pilha
* 6° passo: repito os passos até finalizar a troca
*
*
*/
template
<
class
T
>
void
inverterPilha
(
Pilha
<
T
>
*
pilha
)
{
Pilha
<
T
>
pilhaAux
;
Pilha
<
T
>
pilhaAux
;
while
(
pilha
.
topo
()
!=
-
1
)
{
int
n
=
pilha
->
tamanho
();
pilhaAux
.
empilha
(
pilha
.
desempilha
());
T
aux
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
aux
=
pilha
->
topo
();
pilha
->
desempilha
();
for
(
int
j
=
0
;
j
<
n
-
i
-
1
;
j
++
)
{
pilhaAux
.
empilha
(
pilha
->
desempilha
());
}
pilha
->
empilha
(
aux
);
for
(
int
j
=
0
;
j
<
n
-
i
-
1
;
j
++
)
{
pilha
->
empilha
(
pilhaAux
.
desempilha
());
}
}
}
return
pilhaAux
;
}
}
template
<
class
T
>
Pilha
<
T
>
inverterPilhaComFila
(
Pilha
<
T
>
pilha
)
{
/**
* @brief Dada uma pilha, inverter os valores utilizando uma fila
* 1° passo: percorrer toda a pilha, desempilhando e enfileirando numa fila
* 2° passo: desinfilerar toda a fila, empilhando na pilha
*
* @tparam T
* @param pilha
*/
template
<
class
T
>
void
inverterPilhaComFila
(
Pilha
<
T
>
*
pilha
)
{
Fila
<
T
>
filaAux
;
Fila
<
T
>
filaAux
;
while
(
pilha
.
topo
()
!=
-
1
)
{
while
(
pilha
->
topo
()
!=
-
1
)
{
filaAux
.
enfilera
(
pilha
.
desempilha
());
filaAux
.
enfilera
(
pilha
->
desempilha
());
}
}
while
(
filaAux
.
frente
()
!=
-
1
)
{
while
(
filaAux
.
frente
()
!=
-
1
)
{
pilha
.
empilha
(
filaAux
.
desenfilera
());
pilha
->
empilha
(
filaAux
.
desenfilera
());
}
}
return
pilha
;
}
}
template
<
class
T
>
Pilha
<
T
>
inverterPilhaComDuasPilhas
(
Pilha
<
T
>
pilha
)
{
/**
* @brief dado uma pilha, inverta utilizando duas pilhas
*
* 1° Passo: passo todos os itens para uma pilha auxiliar
* 2° Passo: passo todos os iten para uma segunda pilha auxiliar
* 3° Passo: volto todos os itens para a primeira pilha
*
* Dessa forma a pilha ficará invertida
*
* @tparam T
* @param pilha
*/
template
<
class
T
>
void
inverterPilhaComDuasPilhas
(
Pilha
<
T
>
*
pilha
)
{
Pilha
<
T
>
pilhaAux
,
pilhaAux2
;
Pilha
<
T
>
pilhaAux
,
pilhaAux2
;
while
(
pilha
.
topo
()
!=
-
1
)
{
while
(
pilha
->
topo
()
!=
-
1
)
{
pilhaAux
.
empilha
(
pilha
.
desempilha
());
pilhaAux
.
empilha
(
pilha
->
desempilha
());
}
}
while
(
pilhaAux
.
topo
()
!=
-
1
)
{
while
(
pilhaAux
.
topo
()
!=
-
1
)
{
pilhaAux2
.
empilha
(
pilhaAux
.
desempilha
());
pilhaAux2
.
empilha
(
pilhaAux
.
desempilha
());
}
}
while
(
pilhaAux2
.
topo
()
!=
-
1
)
{
while
(
pilhaAux2
.
topo
()
!=
-
1
)
{
pilha
Aux
.
empilha
(
pilhaAux2
.
desempilha
());
pilha
->
empilha
(
pilhaAux2
.
desempilha
());
}
}
return
pilhaAux
;
}
}
void
preencherPilha
(
Pilha
<
char
>
&
pilha
)
{
void
preencherPilha
(
Pilha
<
char
>
&
pilha
)
{
...
@@ -171,15 +219,15 @@ int main(int argc, char* argv[]) {
...
@@ -171,15 +219,15 @@ int main(int argc, char* argv[]) {
preencherPilha
(
pilha
);
preencherPilha
(
pilha
);
pilha
=
inverterPilha
(
pilha
);
inverterPilha
(
&
pilha
);
printarPilha
(
pilha
,
"Pilha invertida com pilha"
);
printarPilha
(
pilha
,
"Pilha invertida com pilha"
);
preencherPilha
(
pilha
);
preencherPilha
(
pilha
);
pilha
=
inverterPilhaComFila
(
pilha
);
inverterPilhaComFila
(
&
pilha
);
printarPilha
(
pilha
,
"Pilha invertida com FILA"
);
printarPilha
(
pilha
,
"Pilha invertida com FILA"
);
preencherPilha
(
pilha
);
preencherPilha
(
pilha
);
pilha
=
inverterPilhaComDuasPilhas
(
pilha
);
inverterPilhaComDuasPilhas
(
&
pilha
);
printarPilha
(
pilha
,
"Pilha invertida com duas Pilhas"
);
printarPilha
(
pilha
,
"Pilha invertida com duas Pilhas"
);
...
...
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