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

PYTHON - please finish the methods below that are apart of a linkedlist class #return the...
PYTHON - please finish the methods below that are apart of a linkedlist class #return the data value at index(position) in the list. values remain unchanged def __getpos__(self, position): #do not change, checks for valid index if self.size == 0: raise IndexError elif position is None: return self.pop(self.size - 1) elif type(position) != int: raise TypeError elif position < 0 or position >= self.size: raise IndexError #replace the data value at requested position(index). return nothing def __setpos__(self,position,value): #do not change,...
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
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.
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...
I have defined the class. Rectangle in Python. How do I define a class Canvas with...
I have defined the class. Rectangle in Python. How do I define a class Canvas with the following description: Class Canvas represents a collection of Rectangles. It has 8 methods. In addition, to the constructor (i.e. __init__ method) and two methods that override python's object methods (and make your class user friendly as suggested by the test cases), your class should contain 5 more methods: add_one_rectangle, count_same_color, total_perimeter, min_enclosing_rectangle, and common_point.
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"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT