Question

In: Computer Science

First, create a project that creates an ordered linked list class (use the code provided, make...

First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list).

To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way.

Submit a .zip of your entire project.

Solutions

Expert Solution

The python code for the program for reversing an ordered linked list with predefined input is given below.

# Node class  
class Node: 
  
    # Constructor to initialize the node object 
    def __init__(self, data): 
        self.data = data 
        self.next = None
  
class LinkedList: 
  
    # Function to initialize head 
    def __init__(self): 
        self.head = None
  
    def sortedInsert(self, new_node): 
          
        # Special case for the empty linked list  
        if self.head is None: 
            new_node.next = self.head 
            self.head = new_node 
  
        # Special case for head at end 
        elif self.head.data >= new_node.data: 
            new_node.next = self.head 
            self.head = new_node 
  
        else : 
  
            # Locate the node before the point of insertion 
            current = self.head 
            while(current.next is not None and
                 current.next.data < new_node.data): 
                current = current.next
              
            new_node.next = current.next
            current.next = new_node 




    # Function to reverse the linked list
    def reverse(self):
        prev = None
        current = self.head
        while(current is not None):
            next = current.next
            current.next = prev
            prev = current
            current = next
        self.head = prev

  
    # Function to insert a new node at the beginning 
    def push(self, new_data): 
        new_node = Node(new_data) 
        new_node.next = self.head 
        self.head = new_node 
  
    # Utility function to print it the linked LinkedList 
    def printList(self): 
        temp = self.head 
        while(temp): 
            print(temp.data, end=" ")
            temp = temp.next
  
  
# Driver program 
l1 = LinkedList() 
new_node = Node(5) 
l1.sortedInsert(new_node) 
new_node = Node(10) 
l1.sortedInsert(new_node) 
new_node = Node(7) 
l1.sortedInsert(new_node) 
new_node = Node(3) 
l1.sortedInsert(new_node) 
new_node = Node(1) 
l1.sortedInsert(new_node) 
new_node = Node(9) 
l1.sortedInsert(new_node) 
print("\nInput Linked List ")
l1.printList()
l1.reverse() 
print("\nReversed Linked List ")
l1.printList()



Related Solutions

Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Assignment: The Ordered List In this assignment, you will create an ordered list of movies. The...
Assignment: The Ordered List In this assignment, you will create an ordered list of movies. The list will always be maintained in sorted order (ascending order, by movie title) by assuring that all new movies are inserted in the correct location in the list. Create a new project to hold your implementation of an ordered singly-linked list. You will need a main.cppto use as a test driver, as well as header and implementation files as described below. Movie To represent...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Create a linked list of Poem objects using the Poem.java file provided. You may create Poem...
Create a linked list of Poem objects using the Poem.java file provided. You may create Poem objects by hard coding (minimum 4), reading from a file, or getting user input. Print the list using ListIterator. Here is Poem.Java: public class Poem {    private String title;    private String poet;    /**    * No arg constructor    */    public Poem()    {        title = "not set";        poet = "not set";    }    /**...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT