Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Miscellaneous
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
practice problem folder
Miscellaneous
Commits
ad048c7c
Commit
ad048c7c
authored
2 months ago
by
Duncan Holmes
Browse files
Options
Downloads
Patches
Plain Diff
finalized lab.
i'm not doing the bonus may god have mercy on me for not doing so
parent
7a266135
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lab - linked list lab/linked_list_example.py
+70
-9
70 additions, 9 deletions
lab - linked list lab/linked_list_example.py
with
70 additions
and
9 deletions
lab - linked list lab/linked_list_example.py
+
70
−
9
View file @
ad048c7c
...
@@ -75,9 +75,6 @@ class LinkedStack(object):
...
@@ -75,9 +75,6 @@ class LinkedStack(object):
def
is_empty
(
self
):
def
is_empty
(
self
):
return
self
.
head
is
None
return
self
.
head
is
None
def
main
():
test_linked_queue_duncan_version
()
def
test_linked_stack_duncan_version
():
def
test_linked_stack_duncan_version
():
testing_stack
=
LinkedStack
()
testing_stack
=
LinkedStack
()
print
(
"
testing_stack :
"
+
str
(
testing_stack
)
)
print
(
"
testing_stack :
"
+
str
(
testing_stack
)
)
...
@@ -209,7 +206,7 @@ class LinkedDeque(object):
...
@@ -209,7 +206,7 @@ class LinkedDeque(object):
implementation is very similar to code you wrote earlier.)
implementation is very similar to code you wrote earlier.)
"""
"""
def
add_back
(
self
,
item
):
def
add_back
(
self
,
item
):
new_node
=
N
ode
(
item
)
new_node
=
doubly_linked_n
ode
(
item
)
if
self
.
head
is
None
:
if
self
.
head
is
None
:
self
.
head
=
new_node
self
.
head
=
new_node
self
.
tail
=
new_node
self
.
tail
=
new_node
...
@@ -226,7 +223,7 @@ class LinkedDeque(object):
...
@@ -226,7 +223,7 @@ class LinkedDeque(object):
for cleanly dealing with this kind of DRY problem.)
for cleanly dealing with this kind of DRY problem.)
"""
"""
def
add_front
(
self
,
item
):
def
add_front
(
self
,
item
):
new_node
=
N
ode
(
item
)
new_node
=
doubly_linked_n
ode
(
item
)
if
self
.
head
is
None
:
if
self
.
head
is
None
:
self
.
head
=
new_node
self
.
head
=
new_node
self
.
tail
=
new_node
self
.
tail
=
new_node
...
@@ -240,17 +237,44 @@ class LinkedDeque(object):
...
@@ -240,17 +237,44 @@ class LinkedDeque(object):
item from the front of the deque.
item from the front of the deque.
"""
"""
def
remove_front
(
self
):
def
remove_front
(
self
):
"""
this also works but it
'
s best to have symmetry with the twin removes
returnValue = self.head.item
returnValue = self.head.item
self.head = self.head.next_node
self.head = self.head.next_node
return returnValue
return returnValue
"""
if
self
.
head
is
None
:
raise
IndexError
(
"
Deque is empty!
"
)
returnValue
=
self
.
head
.
item
if
self
.
head
==
self
.
tail
:
self
.
head
=
None
self
.
tail
=
None
else
:
self
.
head
=
self
.
head
.
next_node
self
.
head
.
previous_node
=
None
return
returnValue
"""
"""
Implement a remove_back method that removes and returns an
Implement a remove_back method that removes and returns an
item from the back of the deque.
item from the back of the deque.
"""
"""
def
remove_back
(
self
):
def
remove_back
(
self
):
if
self
.
tail
is
None
:
raise
IndexError
(
"
Deque is empty!
"
)
returnValue
=
self
.
tail
.
item
returnValue
=
self
.
tail
.
item
if
self
.
tail
==
self
.
head
:
self
.
head
=
None
self
.
tail
=
None
else
:
self
.
tail
=
self
.
tail
.
previous_node
self
.
tail
=
self
.
tail
.
previous_node
self
.
tail
.
next_node
=
None
return
returnValue
return
returnValue
"""
"""
...
@@ -260,19 +284,56 @@ class LinkedDeque(object):
...
@@ -260,19 +284,56 @@ class LinkedDeque(object):
return
self
.
head
is
None
return
self
.
head
is
None
def
print_linked_list
(
self
):
def
print_linked_list
(
self
):
string_spit_out
=
""
current_node
=
self
.
head
current_node
=
self
.
head
while
current_node
is
not
None
:
while
current_node
is
not
None
:
p
rin
t
(
current_node
.
item
)
st
rin
g_spit_out
+=
str
(
current_node
.
item
)
+
"
"
current_node
=
current_node
.
next_node
current_node
=
current_node
.
next_node
print
(
f
"
Our deque String as a whole is
\n
{
string_spit_out
}
"
)
def
test_linked_deque_duncan_version
():
def
test_linked_deque_duncan_version
():
"""
load it up frontwards
"""
testing_linked_deque
=
LinkedDeque
()
testing_linked_deque
=
LinkedDeque
()
testing_linked_deque
.
add_front
(
1
)
testing_linked_deque
.
add_front
(
2
)
testing_linked_deque
.
add_front
(
3
)
testing_linked_deque
.
add_front
(
3
)
testing_linked_deque
.
add_front
(
2
)
testing_linked_deque
.
add_front
(
1
)
#see that it's structured correclty
testing_linked_deque
.
print_linked_list
()
testing_linked_deque
.
print_linked_list
()
print
(
"
\n
"
)
#unload it
while
not
testing_linked_deque
.
is_empty
():
testing_linked_deque
.
remove_front
()
testing_linked_deque
.
add_back
(
1
)
testing_linked_deque
.
add_back
(
2
)
testing_linked_deque
.
add_back
(
3
)
testing_linked_deque
.
print_linked_list
()
print
(
"
\n
i
'
m gonna take 1 from the front
"
)
testing_linked_deque
.
remove_front
()
testing_linked_deque
.
print_linked_list
()
print
(
"
\n
replacing the 1 i just took away
"
)
testing_linked_deque
.
add_front
(
1
)
testing_linked_deque
.
print_linked_list
()
print
(
"
\n
i
'
m gonna take 3 from the back
"
)
testing_linked_deque
.
remove_back
()
testing_linked_deque
.
print_linked_list
()
def
main
():
test_linked_deque_duncan_version
()
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
main
()
main
()
\ 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