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

Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the...
Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the elements in the list, and returns the resulting list. For example: The given list is 'a' 'b' 'c' 'd' 'e' The return list should be 'e' 'd' 'c' 'b' 'a' How can we do this in Java?
Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2)...
Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2) that receives 2 DoublLinked lists of characters. The method will return true if s1 is subset of s2. Then don’t forget to test the method in the main method in Test class in Doubly package.
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...
You will implement and test the sequence class using an array to store the sequence's items...
You will implement and test the sequence class using an array to store the sequence's items in C++. sequence1.h: The header file for the sequence class. Actually, you don't have to write much of this file. Start with the sequence1.h header file provided and add your name and other information at the top. Also, decide on appropriate private member variables, and declare these in the sequence class definition at the bottom of the header file. If some of your member...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT