Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IMPLEMENTING A LINKED POSITIONAL LIST Please help me out with this code. Leave the node part as it is and please fill in parts with

IMPLEMENTING A LINKED POSITIONAL LIST

Please help me out with this code. Leave the node part as it is and please fill in parts with "TO DO" message in positional list. It can be done using java or python.

import core

class Node(core.Position): """ Node ---- Contains the elements that are placed into the Positional Linked List.

This is a small helper class. You do *NOT* need to modify this class. """

def __init__(self, element): self.element = element self.prev = None self.next = None

def get_element(self): return self.element

class LinkedPositionalList(core.PositionalList): """ Linked Positional List ----------------------

Keeps information in a linked list, each element is a position. It allows for insertion and removal anywhere in the list. It supports an 'undo' for the "insert" and "remove" that can be called multiple times.

Implement the PositionalList interface using the Node class """

def __init__(self): # The header sentinel self._header = Node(None) # The trailer sentinel self._trailer = Node(None)

# Setting the correct previous/next self._header.next = self._trailer self._trailer.prev = self._header

def print_list(self): """

Prints the current state of the list; Starting at the header and moving forwards. """ current = self._header.next list_str = "" while True: if current == self._trailer: break list_str += "{} ".format(current.get_element()) current = current.next

return list_str

def insert_first(self, element):

# TODO Implement # Create the new element as the first in the list. return None

def insert_last(self, element):

# TODO Implement # Create the new element as the last in the list. return None

def insert_before(self, position, element):

# TODO Implement: #Insert the new element in the position by shifting the previous element return None

def remove(self, position):

# TODO: Implement this (remember to return the ELEMENT at `position`). # Remove the element at the position specified remembering to update # the other node information! return None

def undo(self):

# TODO: Implement this: # Undo the latest action. return None

def main(): # This function is here for you to optionally use for your own # testing / running. This function will NOT be tested. pass

if __name__ == "__main__": main()

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions