Question

In: Computer Science

(Python) Define a function makeTwoWay that expects a singly linked structure as its argument. The function...

(Python) Define a function makeTwoWay that expects a singly linked structure as its argument. The function builds and returns a doubly linked structure that contains the items in the singly linked structure. (Note: The function should not alter the argument's structure.)

node.py source code:

"""
File: node.py

Node classes for one-way linked structures and two-way
linked structures.
"""

class Node(object):

    def __init__(self, data, next = None):
        """Instantiates a Node with default next of None"""
        self.data = data
        self.next = next

class TwoWayNode(Node):

    def __init__(self, data, previous = None, next = None):
        Node.__init__(self, data, next)
        self.previous = previous

# Just an empty link
node1 = None

# A node containing data and an empty link
node2 = Node("A", None)

# A node containing data and a link to node2
node3 = Node("B", node2)

Solutions

Expert Solution

#Phython code

class Node(object):

    def __init__(self, data, next = None):
        """Instantiates a Node with default next of None"""
        self.data = data
        self.next = next

class TwoWayNode(Node):

    def __init__(self, data, previous = None, next = None):
        Node.__init__(self, data, next)
        self.previous = previous

def makeTwoWay(sll):
    head = None
    prev = None
    while sll != None:
        if head == None:
            head = TwoWayNode(sll.data , prev)
            prev = head
        else:
            prev.next = TwoWayNode(sll.data , prev)
            prev = prev.next

        sll = sll.next

    return head
      
      

# Just an empty link
node1 = None

# A node containing data and an empty link
node2 = Node("A", None)

# A node containing data and a link to node2
node3 = Node("B", node2)

head = makeTwoWay(node3)

#Screeshot


Related Solutions

PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
1. Answer “True” or “False”. (a) A doubly linked list structure is worse than a singly...
1. Answer “True” or “False”. (a) A doubly linked list structure is worse than a singly linked list if we plan to do a lot of insertions. (b) A doubly linked list structure is worse than a singly linked list if we plan to do a lot of deletions. (c) A doubly linked list structure is worse than a singly linked list if we plan to print the entire list frequently. (d) An array implementation of a queue is more...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last()...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last() - returns element located at third-to-last in the list - reverse() - reveres the linked list, note, this is not just printing elements in reverse order, this is actually reversing the list
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly linked lists if you can make it clean and short that be nice
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT