#####################################################################################################
class Node():
def __init__(self,data, next = None, prev = None):
#constructure
self.data = data #data the mid turm
self.nextNode = next #next pointer
self.prevNode = prev #previous pointer
def get_prev(self): #method for getting previous
node
if self.prevNode != None: #if previioue node is present
return self.prevNode #return the node
def get_next(self): #getting next node
if self.nextNode != None: #if next node is present
return self.nextNode #return the next bode
def get_value(self): #getting value of current node
if self != None: #is current node is present
return self.data #returnthe value of urrent node
def set_prev(self,prev): #method to set previous node
self.prevNode = prev #set
def set_next(self,next): #method for settinf next node
self.nextNode = next
def set_value(self,value): #seting value
self.data = value
class DoublyLinkedList(): #class linked list
def __init__(self): #all the constructure
self.head = None #head tail and count initialize
self.count = 0 #count
self.tail = None #tail
def add_to_front(self,data): #data added to front of the
doubly linked list
self.count+=1 #count incremented
newNode = Node(data) #new node witth data as value
if not self.head: # if it is first node
self.head = newNode #head tail initialize by same
self.tail = newNode
else: #else new node added front and head moved
newNode.nextNode = self.head
self.head.prevNode = newNode
self.head = newNode
def add_to_last(self,data): #add to last
self.count+=1
newNode = Node(data) #new node created
if self.head == None:
add_to_front(data)
return
else:
self.tail.nextNode = newNode #node added to tail
newNode.prevNode = self.tail #and tail moved next
self.tail = newNode
def delete(self, data): #method to delete node
currentNode = self.head
while(currentNode!=self.tail and currentNode.data != data):
#iterate to the desired node
currentNode = currentNode.nextNode
if currentNode == self.tail and currentNode.data != data:
#if last node is still not equall
print("Not found") #the not found prinetd
else:
prevNode = currentNode.prevNode #else deleted and next node preve
node balances
nextNode = currentNode.nextNode
prevNode.nextNode = nextNode
nextNode.prevNode = prevNode
self.count-=1
def reverse(self): #reversing the linked list
temp = None
current = self.head
self.tail = self.head
while current.nextNode!=None:#iterate over the linked
list
temp = current.prevNode
current.prevNode = current.nextNode
current.nextNode = temp
current = current.prevNode #jsut swapping the nextNode with
prevNode
temp = current.prevNode
current.prevNode = current.nextNode #last node also swapped
current.nextNode = temp
if temp.nextNode is not None:
self.head = temp.prevNode #head assign to the last node
def printall(self): #function for printing Not asked but used to
print each step
currentNode = self.head
while currentNode.nextNode:
print(currentNode.data,end = " ")
currentNode = currentNode.nextNode
print(currentNode.data)
def compare(self,lis): #compare with the list
if len(lis) == self.count: #if size of list and linked list
equall
flag = True #flag for equality
if(lis[0] == self.head.data and lis[-1] == self.tail.data):
#if first and last element is same
current = self.head
indx = 0
while current.nextNode: #iterate over linked list
if current.data != lis[indx]: #if any data not
matched
flag = False
return False #return false
current = current.nextNode
indx+=1
else: #if first and last element not match
flag = False
return False
else:#if size not matched
flag = False
return False
return flag #if they are equall
def find(self,data): #find data
indx = 0;
current =self.head
while current.nextNode!=None: #iterate over the list
if current.data == data: #if data found
return indx #return index
current = current.nextNode #next node
indx+=1
l = DoublyLinkedList() #linked list created
for i in range(7,15):
l.add_to_front(i) #insert 10-1 reverse order front
print("_________________________________________________________________")
l.printall() #print the linked list
print("_________________________________________________________________")
for i in range(20,25): #enter 110-115
l.add_to_last(i) #added to last
l.printall() #print the list
print("_________________________________________________________________")
print(l.find(15)) #find 10
print("_________________________________________________________________")
l.delete(11) #delete 114
l.printall() #printall
print("_________________________________________________________________")
l.reverse() #reverse
l.printall() #print all
print("_________________________________________________________________")
list1 = [1,2,3,4,5,6,7,8,9]
print(list1)
l.printall()
print(l.compare(list1)) #compare to list for true
print("_________________________________________________________________")
list2 = [24,23,22,21,20,7,8,9,10,12,13,14]
print(list2)
l.printall()
print(l.compare(list2)) #compare for false
print("_________________________________________________________________")
##################################################################################################
Code Snippet:
OUTPUT:
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: """...