Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Two variables are initialized for you in the starter code. They are named head and tail. head always references the first node in a linked
Two variables are initialized for you in the starter code.
They are named head and tail.
head always references the first node in a linked list.
tail always references the last node in a linked list.
You must use these variables in your code.
Part
Start at head and traverse loop through the linked nodes.
Print each node's data value as you encounter it
Use a variable named currentNode as you traverse the list.
In your loop, to print the current node's value, use:
printcurrentNodedata
As you loop, you must update currentNode to reference the next item.
Part
Use tail to add nodes to the end of the list, onebyone in a loop.
As they are added, you must update tail so it always references the last node.
The variable head should not be used during this step.
Part
Create the new node.
Then, change tail's next link to reference it
Part
Read the comments in the starter code.
Use a variable named currentNode as you traverse the list.
To link the new node in correctly:
the new node's next link must be set to currentNode's next link,
then currentNode's next link must be set to reference the new node.
File: useArraysAndLinkedNodes.py
Author: Derrf Seitz
This program exercises arrays and linked nodes.
The following files must be in the same folder:
arrays.py
node.py
#
# Replace with your name.
# Replace any comments with your own code statements
# to accomplish the specified task.
# DO NOT CHANGE ANY OTHER CODE.
from arrays import Array
from node import Node
# Part :
# This function prints a linked list of nodes.
# Parameters:
# head a reference to the first node in the linked list.
def printLinkedListhead:
# Print the linked list with each nodes's data on a separate line.
# You must use some form of a loop to print the linked list.
#
# Here is the array:
theArray Array
for i in rangelentheArray:
theArrayi i
# Print the array:
printThe array:"
printtheArray
print # blank line
# You must use these variables in your code:
head NodetheArray None
tail head
# Part :
# Copy the array items to a linked list of nodes:
# The linked list must consist of Node class items.
# You must use some form of a loop to create the linked list.
#
printAfter creating the list from the array items:"
printLinkedListhead
print # blank line
# Part :
# Add a node with the value to the end of the list:
#
printAfter adding to the end of the list:"
printLinkedListhead
print # blank line
# Part :
# Add a node with the value after the node with the value :
# You must go through the list until you arrive at the
# then link in the new node correctly.
#
printAfter adding after the node with the value :
printLinkedListhead
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started