Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
algoritmos_EDB2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jonas Rocha
algoritmos_EDB2
Commits
b0c719ac
Commit
b0c719ac
authored
6 years ago
by
Jonas Rocha
Browse files
Options
Downloads
Patches
Plain Diff
Add new file
parent
3383ec8d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lista.py
+136
-0
136 additions, 0 deletions
lista.py
with
136 additions
and
0 deletions
lista.py
0 → 100644
+
136
−
0
View file @
b0c719ac
class
NodoLista
:
"""
Esta classe representa um nodo de uma lista encadeada.
"""
def
__init__
(
self
,
dado
=
0
,
proximo_nodo
=
None
,
anterior_nodo
=
None
):
self
.
dado
=
dado
self
.
proximo
=
proximo_nodo
self
.
anterior
=
anterior_nodo
class
ListaEncadeada
():
"""
Esta classe representa uma lista encadeada.
"""
def
__init__
(
self
):
self
.
cabeca
=
None
def
__repr__
(
self
):
return
"
[
"
+
str
(
self
.
cabeca
)
+
"
]
"
def
print_lista
(
self
):
corrente
=
self
.
cabeca
print
(
"
[
"
,
end
=
""
)
while
corrente
.
proximo
!=
None
:
print
(
str
(
corrente
.
dado
),
"
-> <-
"
,
end
=
""
)
corrente
=
corrente
.
proximo
print
(
corrente
.
dado
,
"
]
\n
"
)
def
insere_no_inicio
(
lista
,
novo_dado
):
if
lista
.
cabeca
==
None
:
novo_nodo
=
NodoLista
(
novo_dado
)
novo_nodo
.
proximo
=
None
novo_nodo
.
anterior
=
None
lista
.
cabeca
=
novo_nodo
else
:
novo_nodo
=
NodoLista
(
novo_dado
)
first
=
lista
.
cabeca
novo_nodo
.
proximo
=
first
novo_nodo
.
anterior
=
None
first
.
anterior
=
novo_nodo
lista
.
cabeca
=
novo_nodo
def
swap
(
lista
,
valor1
,
valor2
):
a
=
busca
(
lista
,
valor1
)
b
=
busca
(
lista
,
valor2
)
if
id
(
a
)
<
id
(
b
):
temp
=
a
a
=
b
b
=
temp
if
a
.
anterior
==
None
and
a
.
proximo
==
b
:
#juntos no comeco
lista
.
cabeca
=
b
a
.
proximo
=
b
.
proximo
a
.
anterior
=
b
b
.
anterior
=
None
b
.
proximo
=
a
elif
b
.
proximo
==
None
:
#juntos no final
b
.
anterior
=
a
.
anterior
b
.
proximo
=
a
a
.
anterior
.
proximo
=
b
a
.
anterior
=
b
a
.
proximo
=
None
elif
a
.
anterior
!=
None
and
b
.
proximo
!=
None
and
a
.
proximo
==
b
and
b
.
anterior
==
a
:
#juntos no meio
a
.
anterior
.
proximo
=
b
b
.
proximo
.
anterior
=
a
b
.
anterior
=
a
.
anterior
a
.
proximo
=
b
.
proximo
a
.
anterior
=
b
b
.
proximo
=
a
elif
a
.
anterior
==
None
and
b
.
proximo
!=
None
and
a
.
proximo
==
b
and
b
.
anterior
==
a
:
# cabeca e meio
lista
.
cabeca
=
b
a
.
proximo
.
anterior
=
b
aux
=
a
.
proximo
a
.
proximo
=
b
.
proximo
a
.
anterior
=
b
.
anterior
b
.
anterior
.
proximo
=
a
b
.
proximo
.
anterior
=
a
b
.
proximo
=
aux
b
.
anterior
=
None
elif
a
==
b
:
print
(
"
Posicoes iguais, ou seja, o mesmo node
"
)
def
busca
(
lista
,
valor
):
corrente
=
lista
.
cabeca
while
corrente
and
corrente
.
dado
!=
valor
:
corrente
=
corrente
.
proximo
return
corrente
items
=
[
10
,
9
,
8
,
7
,
6
,
5
,
4
,
3
,
2
,
1
]
lista
=
ListaEncadeada
()
for
i
in
items
:
insere_no_inicio
(
lista
,
i
)
print
(
"
Lista Original:
"
)
lista
.
print_lista
()
print
(
"
juntos no comeco
"
,
1
,
2
)
swap
(
lista
,
2
,
1
)
lista
.
print_lista
()
print
(
"
juntos no final
"
,
9
,
10
)
swap
(
lista
,
10
,
9
)
lista
.
print_lista
()
print
(
"
juntos no meio
"
,
5
,
6
)
swap
(
lista
,
6
,
5
)
lista
.
print_lista
()
print
(
"
comeco e meio
"
,
2
,
5
)
swap
(
lista
,
2
,
5
)
lista
.
print_lista
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment