Answered step by step
Verified Expert Solution
Link Copied!

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 1
------
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:
print(currentNode.data)
As you loop, you must update currentNode to reference the next item.
Part 2
------
Use tail to add nodes to the end of the list, one-by-one 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 3
------
Create the new node.
Then, change tail's next link to reference it.
Part 4
------
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 statement(s)
# to accomplish the specified task.
# DO NOT CHANGE ANY OTHER CODE.
from arrays import Array
from node import Node
# Part 1:
# This function prints a linked list of nodes.
# Parameters:
# head - a reference to the first node in the linked list.
def printLinkedList(head):
# 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(10)
for i in range(len(theArray)):
theArray[i]= i +1
# Print the array:
print("The array:")
print(theArray)
print() # blank line
# You must use these variables in your code:
head = Node(theArray[0], None)
tail = head
# Part 2:
# 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.
#
print("After creating the list from the array items:")
printLinkedList(head)
print() # blank line
# Part 3:
# Add a node with the value 99 to the end of the list:
#
print("After adding 99 to the end of the list:")
printLinkedList(head)
print() # blank line
# Part 4:
# Add a node with the value 55 after the node with the value 5:
# You must go through the list until you arrive at the 5,
# then link in the new node correctly.
#
print("After adding 55 after the node with the value 5:")
printLinkedList(head)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions