Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Define a function that takes the head of a doubly linked list and 2 integers pos and data as arguments and adds a new node

image text in transcribed

image text in transcribed

image text in transcribed

Define a function that takes the head of a doubly linked list and 2 integers pos and data as arguments and adds a new node with value data at the index pos +1 in the doubly linked list. The definition of the linked list has already been given to you. The index starts from 0 . The program displays the modified linked list. Suppose the given linked list is 2>4>5, pos = 2 and data =6, then the linked list after modification will be 2>4>5>6. ng Window class Node: def__init_(self, data): self.item = data self.nref = None self.pref = None class DoublyLinkedList: def__init_(self): self.start_node = None def insert_in_emptylist(self, data): if self.start_node is None: new_node = Node ( data ) self.start_node = new_node else: print("list is not empty") def insert_at_end(self, data): if self.start_node is None: new_node = Node (data) self.start_node = new_node return n= self.start_node while n.nref is not None: n=n. nref new_node = Node ( data ) n. nref = new_node new_node.pref =n def insert_after_item(self, pos, data): \#write your code here def traverse_list(self): if self.start_node is None: print("List has no element") return else: n= self.start_node while n is not None: print(n.item , end=" ") n=n.nref def getNth(self, index): current = self.start_node count = while (current): if (count == index): return current. item count +=1 current = current. nref new_linked_list = DoublyLinkedList () n=int (input ()) arr = input ().split() for i in range (n) : b=int(arr[i]) if (i==0) : new_linked_list.insert_in_emptylist (b) else: new_linked_list.insert_at_end(b) x=int( input ()). y= int (input ()) b= new_linked_list.getNth (x) new_linked_list.insert_after_item (b,y) new_linked_list,traverse_list()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genetic Databases

Authors: Martin J. Bishop

1st Edition

ISBN: 0121016250, 978-0121016258

More Books

Students also viewed these Databases questions

Question

5. Describe the visual representations, or models, of communication

Answered: 1 week ago