Answer :
Python code for above problem
class Node:
def __init__(self,value,prev,next):
self.value=value
self.prev=prev
self.next=next
def get_prev(self):
return self.prev
def get_next(self):
return self.next
def get_value(self):
return self.value
def set_prev(self,node):
self.prev=node
def det_next(self,node):
self.next=node
def set_value(self,val):
self.value=val
class DoubleLinkedList:
def __init__(self):
self.head=None
self.tail=None
self.size=0
def add_to_front(self,val):
node=Node(val,None,None)
if self.head is None:
self.head=node
self.tail=node
else:
self.head.prev=node
node.next=self.head
self.head=node
self.size+=1
def add_to_end(self,val):
node=Node(val,None,None)
if self.head is None:
self.head=node
self.tail=node
else:
self.tail.next=node
node.prev=self.tail
self.tail=node
self.size+=1
def delete(self,val):
node=self.head
prev=None
while node is not None and node.value!=val:
prev=node
node=node.next
if node is None:
return
if prev is None:
self.head=self.head.next
if self.head is None:
self.tail=None
else:
self.head.prev=None
else:
prev.next=node.next
node.prev.next=prev
self.size-=1
def find(self,val):
node=self.head
index=0
while node is not None:
if node.value==val:
return index
index+=1
node=node.next
return -1
def reverse(self):
if self.size==0 or self.size==1:
return
n1=self.head
n2=self.tail
for i in range(self.size//2):
temp=n1.value
n1.value=n2.value
n2.value=temp
n1=n1.next
n2=n2.prev
def compare(self,lst):
if(self.size!=len(lst)):
return False
node=self.head
index=0
while node is not None:
if node.value!=lst[index]:
return False
index+=1
node=node.next
return True
def print_list(self):
node=self.head
while node is not None:
print(node.value,end=" ")
node=node.next
print("")
# testing main method
def main():
dll=DoubleLinkedList()
for i in range(0,5,2):
dll.add_to_front(i)
dll.add_to_end(i+1)
dll.print_list() # prints [4,2,0,1,3,5]
dll.reverse()
dll.print_list() # prints [5 3 1 0 2 4]
print(dll.compare([5,3,1,0,2,4])) # prints True
print(dll.compare([5,3,1,2,4])) # prints False
dll.delete(5)
dll.print_list() # prints [3 1 0 2 4]
dll.delete(8)
dll.print_list() # prints [3 1 0 2 4]
print(dll.find(5)) # prints -1
print(dll.find(3)) # prints 0
main()
Mention in comments if any mistakes or errors are found. please give me up vote. Thank you.
use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...
use python
In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...
use python
In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...
This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...
(20 points) ) Write a Python program which
merges two sorted singly linked lists and return it as a new list.
The new list should be made by splicing together the nodes of the
first two lists.
Example:
Example: Input: 1->2- >4, 1->3->4 Output: 1->1->2->3 ->4->4
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...
python
Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well: 2. Add the methods to the MLL class: a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately. b. addFirst( value) that adds a value to...