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...
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...
(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...
117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: . A constructor with no parameters which creates an empty list. . void add (int data) -adds data to the front of the list. .A constructor IntlinkedList(int[]) which will...
In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...