Question

In: Computer Science

implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't...

implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't return anything and make sure it executes without errors on lists with no items or one item

example:

fruits = DoublyLinkedList()
fruits.append('apple')
fruits.append('banana')
fruits.append('cherry')
for i in fruits:
    print(i)
>> apple
>> banana
>> Charlie

fruits.reverse()
for i in fruits:
    print(i)
>> cherry
>> banana
>> apple

Solutions

Expert Solution

Code:

class Node:
  
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
  
class DoublyLinkedList:
# Constructor for empty Doubly Linked List
def __init__(self):
self.head = None

# Function reverse a Doubly Linked List
def reverse(self):
temp = None
current = self.head
  
# Swap next and prev for all nodes of
# doubly linked list
while current is not None:
temp = current.prev
current.prev = current.next
current.next = temp
current = current.prev
  
# Before changing head, check for the cases like
# empty list and list with only one node
if temp is not None:
self.head = temp.prev
  
# Given a reference to the head of a list and an
# integer,inserts a new node on the front of list
def push(self, new_data):

# 1. Allocates node
# 2. Put the data in it
new_node = Node(new_data)

# 3. Make next of new node as head and
# previous as None (already None)
new_node.next = self.head

# 4. change prev of head node to new_node
if self.head is not None:
self.head.prev = new_node

# 5. move the head to point to the new node
self.head = new_node
  
  
def printList(self, node):
while(node is not None):
print(node.data)
node = node.next
  
# Driver program to test the above functions
dll = DoublyLinkedList()
dll.push("Apple");
dll.push("Banana");
dll.push("Cherry");
  
print("\nOriginal Linked List:\n")
dll.printList(dll.head)
  
# Reverse doubly linked list
dll.reverse()
  
print("\n\nReversed Linked List:\n")
dll.printList(dll.head)

Note: refer the screenshots for indentation purposes


Related Solutions

Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the node that contains the val. .
Implement a class called DoublyLinkedList. In the main function, instantiate the DoublyLinkedList class and make sure that there is a user loop and a menu so that the user can access all the list operators.
C++  Implement a class called DoublyLinkedList. In the main function, instantiate the DoublyLinkedList class and make sure that there is a user loop and a menu so that the user can access all the list operators. You should implement the following operators, and any others that you may deem best. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
Please use C++ to complete this question follow the requirement. Question: Implement a class called DoublyLinkedList....
Please use C++ to complete this question follow the requirement. Question: Implement a class called DoublyLinkedList. In the main function, instantiate the DoublyLinkedList class and make sure that there is a user loop and a menu so that the user can access all the list operators. You should implement the following operators, and any others that you may deem best. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class to solve: (Gaddis) Programming Challenger 3. Car Class p. 802 Ch. 13 • Implement a menu of options • Add a motor vehicle to the tree • Remove a motor vehicle to the tree • Search by vehicle model • Vehicle Inventory Updates • Take one of the tours to verify the contents of the tree. Car Class Write a class named Car that...
I need to implement a method that removes any duplicate items in a list and returns...
I need to implement a method that removes any duplicate items in a list and returns the new values    private static linkedlist removeDuplicates(linkedlist list) {    return list; }
JAVA Implement a public class method named comparison on a public class Compare that accepts two...
JAVA Implement a public class method named comparison on a public class Compare that accepts two Object arguments. It should return 0 if both references are equal. 1 if both objects are equal. and -1 otherwise. (SUPER IMPORTANT) Either reference can be null, so you'll need to handle those cases carefully! Here is what I have so far: public class Compare { public static int comparison(Object a, Object b) {   if (a == null || b == null) {    return...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should take in an int and return arr at that index if it is a valid index notEqual The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal. operator-(float) Will subtract the float value that is passed in from each of the values in arr Copy constructor Will take...
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT