Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP (PYTHON): Implementing an iterator for the doubly linked list Implement an

ID: 3757292 • Letter: H

Question

HELP (PYTHON): Implementing an iterator for the doubly linked list

Implement an iterator class, LinkedListIterator that will traverse the linked list from the first node in the list to the last node in the list. The LinkedListDLL class should return an instance of the LinkedListIterator such that it is compatible with a for loop as shown in the example below. The iterator should stop traversal once every node in the list has been visited.

This will produce the output:

Contents: 8 6 4 2 1 3 5 7 9

Contents: 6 4 2 1 3 5

my-list-LinkedListDLL() for node in my list: print (node,end-"") print () for x in range (1,10) if x % 2-0: my_list.add_to_head (x) else: my list.add to tai1 (x) 11 Im print(" Contents:",end-"") for node in my list: print (node,end-"") print () my list.remove from head my list.remove from tail () my list.remove from tail print(" Contents:",end-" ") for node in my list: 1 Im print (node,end") print ()

Explanation / Answer

class DoubleListNode:

    def __init__(self,data):

        self.data=data

        self.prev = None

        self.next= None

class ListIterator:

    def __init__(self):

        self._current = self.head

    def __iter__(self):

        return self

    def next(self):

        if self.size == 0 :

            raise StopIteration

        else:

            item = self._current.data

            self._current=self._current.next

            return item

class DoublyLinkedList:

    def __init__(self):

        self.head= None

        self.tail= None

        self.size = 0

    def add(self,data):

        newnode= DoubleListNode(data)

        self.size+=1

        if self.head is None:

            self.head = newnode

            self.tail = self.head

        elif data < self.head.data: # before head

            newnode.next = self.head

            self.head.prev= newnode

            self.head= newnode

        elif data > self.tail.data: # at the end

            newnode.prev= self.tail

            self.tail.next= newnode

            self.tail=newnode

        else:

            curNode = self.head

            while curNode is not None and curNode.data < data:

                curNode=curNode.next           

            newnode.next= curNode

            newnode.prev=curNode.prev

            curNode.prev.next= newnode

            curNode.prev=newnode

    def remove(self,data):

        curNode=self.head

        while curNode is not None and curNode.data!= data:

            curNode= curNode.next

        if curNode is not None:

            self.size -= 1

            if curNode is self.head:

                self.head= curNode.next

            else:

                curNode.prev.next=curNode.next

            if curNode is self.tail:

                self.tail=curNode.prev

            else:

                curNode.next.prev=curNode.prev