Question

In: Computer Science

PYTHON-- how do i change these linkedlist methods into doublylinkedlist methods? A doublylinked list is the...

PYTHON-- how do i change these linkedlist methods into doublylinkedlist methods? A doublylinked list is the same as a linked list, except that each node has a reference to the node after it(next) and the node before it(prev). 

#adds item to the head of list.
def add(self, item):
   
    node = Node(item, self.head)  
   
    self.head = node  
    if self.size == 0:  
        self.tail = node
    self.size += 1

#appends item to tail of list
def append(self, item):

    node = Node(item)
    tail = self.head
    node.next = None
    if self.size == 0:
        node.prev = None
        self.head = node
        return

    while tail.next is not None:
        tail = tail.next

    tail.next = node
    node.prev = tail
    self.size += 1   

#removes the node at position pos. returns node's data
 #make pop O(1)
def pop(self, pos=None):
    
    current = self.head
    previous = None

    i = 0
    while current != None and i != pos:
        previous = current
        current = current.next
        i += 1
if current == self.tail:
    previous.next = None
    self.tail = previous
    self.size -= 1
    return current.data
elif prev is None:
    if self.size == 1:
        self.tail = None
    self.head = current.next
    self.size -= 1
    return current.data
else:
    previous.next = current.next
    self.size -= 1
    return current.data

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

class Node:
    def __init__(self, item):

        self.item = item
        self.next = None
        self.prev = None
    
class DoubleLinkedList:
    def __init__(self):

        self.dllnode = None
    # adds item to the head of list

    def add(self, item):

        if(self.dllnode == None):
            new_node = Node(item)
            self.dllnode = new_node
        new_node = Node(item)
        new_node.next = self.dllnode
        self.dllnode.prev = new_node
        self.dllnode = new_node

        # adds item to the end of list

    def append(self, item):

        new_node = Node(item)
        new_node.next = None
        if self.dllnode is None:
            new_node.prev = None
            self.dllnode = new_node
            return
        temp = self.dllnode
        if temp.next is not None:
            temp = temp.next
        temp.next = new_node
        new_node.prev = temp

        # print list

    def printList(self):

        while(self.dllnode is not None):
            print(self.dllnode.item, end="->")
            self.dllnode = self.dllnode.next
        # removes the node at position pos return node data

    def pop(self, pos):

        if(self.dllnode is None):
            return
        cur = self.dllnode
        i = 1
        while(cur != None and i != pos):
            cur = cur.next
            i = i+1
            if(cur == None):
                return

        # node to be deleted
        if(self.dllnode == None or cur == None):
            return
        # if the node is the first or head node
        if(self.dllnode == cur):
            self.dllnode = cur.next
        # if the node is the last node
        if(cur.next is not None):
            cur.next.prev = cur.prev
        if(cur.prev is not None):
            cur.prev.next = cur.next
            return self.dllnode.item


node = DoubleLinkedList()

node.add(20)
node.add(30)
node.append(50)
print(node.pop(2))
print(node.printList())

Related Solutions

How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
Python: How would I write a function using list comprehensions to list of integers into two...
Python: How would I write a function using list comprehensions to list of integers into two halves with the evens first, followed by odds? And on top of that, the numbers should be listed in their original order.
how to do the tf-idf vectorization in python ? how do we fit the model? i...
how to do the tf-idf vectorization in python ? how do we fit the model? i am trying like this but i am getting confused please explain with complete program vectorizer = Tfidvectorizer() x=vectorizer.fit_transofrm() // what data should be passed here ? do we split it in tokens ?? do we use vocabulary?? do we use s.split() ?? x.toarray() ? == pleae provide complete program with output because i am not understanding
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
How do I Install a Python module that is not included with the Anaconda Distribution.
How do I Install a Python module that is not included with the Anaconda Distribution.
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
How do I write a program in MIPS that will change all the characters in a...
How do I write a program in MIPS that will change all the characters in a string into lowercase. For instance: string: " CANBERRA AUSTRALIA" Output: "canberra australia"
I have questions on how to do this in R/Rstudio or smoothing methods. I have been...
I have questions on how to do this in R/Rstudio or smoothing methods. I have been struggling with using the codes. I have been practicing and tryign to do it, but I am really unsure at where I am with it. Just need something to compare it too! I don't understand why there isn't any information under the demand section! (1) A memo written to the Chief Operations Officer of Transantiago that describes the model and method and presents the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT