Question
python

Programming assignment: Lets think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, a
# ListNode 2.PY class ListNode2: def __init__(self, item - None, leftL - None, rightL - None): creates a ListNode with the s
# Testing # create a linked list of 5 values: 34, 1, 23, 7, and 10. # Displays it. # put the code here, make nl to be 34, and


# ListNode.py class ListNode: definit__(self, item = None, link = None): creates a ListNode with the specified data value an
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

class ListNode2():

    def __init__(self, item=None, leftL=None, rightL=None):

        self.item = item

        self.leftL = leftL

        self.rightL = rightL

        if leftL != None:

            leftL.rightL = self

        if rightL != None:

            rightL.leftL = self

    def __str__(self):

        return str(self.item)


def printLR(headNode):

    node = headNode

    while node is not None:

        print(node.item)

        node = node.rightL

    print("end of linked list")


def printRL(tailNode):

    node = tailNode

    listV = []

    while node is not None:

        listV.append(node.item)

        node = node.leftL

    listV = listV[::-1]

    print("here is list of elements, following left links:", listV)

    return listV


# Testing

n5 = ListNode2(10)

n4 = ListNode2(7, None, n5)

n3 = ListNode2(23, None, n4)

n2 = ListNode2(1, None, n3)

n1 = ListNode2(34, None, n2)

# printing

printLR(n1)

printRL(n5)

# Inserting 8 btw 34 and 1

print("\nInserting 8...")

n6 = ListNode2(8, n1, n2)

# printing

printLR(n1)

printRL(n5)


# Deleting 7

print("\nDeleting 7....")

n3.rightL = n5

n5.leftL = n3

del n4


# printing

printLR(n1)

printRL(n5)

Output:

34
1
23
7
10
end of linked list
here is list of elements, following left links: [34, 1, 23, 7, 10]

Inserting 8...
34
8
1
23
7
10
end of linked list
here is list of elements, following left links: [34, 8, 1, 23, 7, 10]

Deleting 7....
34
8
1
23
10
end of linked list
here is list of elements, following left links: [34, 8, 1, 23, 10]

Add a comment
Know the answer?
Add Answer to:
python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Create a linked list with the following features. A Node class that stores the data type...

    Create a linked list with the following features. A Node class that stores the data type of your choice. A constructor that creates a dummy header node. void display() -- A method for printing the list. void add(item) -- A method for adding a value to the beginning of the list void addEnd(item) -- A method of adding a value to the end of the list. bool contains(item) -- A method for finding a specified value in the list. int...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

  • from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked...

    from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...

  • Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list...

    Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list using node. (from chapter 24). Your code should utilize the selectionsort method from attached and create another method call selectionsort in linkedlist class. To demonstrate the usage of the selection sort method, you should manually create a link list of random integer (say of 5 numbers), and you need to demonstrate the use the selection sort to sorted the link list. Please submit your...

  • Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value...

    Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Question 2.1. Empty Node In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None. Add...

  • PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None   

    PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None        self.__size = 0    # Return the head element in the list    def getFirst(self):        if self.__size == 0:            return None        else:            return self.__head.element        # Return the last element in the list    def getLast(self):        if self.__size == 0:            return None        else:            return self.__tail.element    # Add an element to the beginning of the list    def addFirst(self, e):        newNode = Node(e) # Create a new node        newNode.next = self.__head # link...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT