Question

In: Computer Science

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, 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

Solutions

Expert Solution

ANSWER:

  • As complete details of linkedlist class not provided, few assumption for the name of predefined functions of linkedlist class is made to work on the problem.
  • I have provided the properly commented  and indented code so you can easily copy the code as well as check for correct indentation.
  • Have a nice and healthy day!!

CODE

#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
        
    # As complete details of linkedlist class not provided, 
    # Assuming name of predefined variables and methods of class,
    # a. self.head = gives head node of linked list
    # b. node.next = gives next node attached to node
    # c. node.value = gives value stored in node
    
    # implementation
    # fetching head node of linkedlist and storing in node
    node = self.head
    
    # looping for next node for (position-1) times to fetch node at required pos.
    for i in range(position-1):
        node = node.next
        
    # after reaching at node of required position, returning value of the node
    return node.value
    

#replace the data value at requested position(index). return nothing
def __setpos__(self,position,value):

    #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
        
    # As complete details of linkedlist class not provided, 
    # Assuming name of predefined variables and methods of class,
    # a. self.head = gives head node of linked list
    # b. node.next = gives next node attached to node
    # c. node.value = gives/assigns value stored in node
    
    # implementation
    # fetching head node of linkedlist and storing in node
    node = self.head
    
    # looping for next node for (position-1) times to fetch node at required pos.
    for i in range(position-1):
        # node.next gives next node from node
        node = node.next
        
    # after reaching at node of required position, replacing value of the node
    # using node.value for the same
    node.value = value

Related Solutions

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...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
Please create a python module named homework.py and implement the classes and methods outlined below. Below...
Please create a python module named homework.py and implement the classes and methods outlined below. Below you will find an explanation for each class and method you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to represent a group of students in a...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Language: Python Implement the Book class as demonstrated below. You should not change the Book methods...
Language: Python Implement the Book class as demonstrated below. You should not change the Book methods and implementation as provided. The docstrings in the template contain descriptions of each method. >>> b=Book ( ' Code' , 'Charles Ptzold' 'Computing' , 2001) >>> b Book ( ' Code' , 'Charles Ptzold' 'Computing' , 2001) >>> str (b) ' Code : Charles Ptzold: Computing: 2001 ' >>> b. getTitle ( ) ' Code ' >>> b. getAuthor() ' Charles Ptzold' >>> b....
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public...
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public class PolkaDots { private ArrayList<Circle> dots;// an arrayList field to hold an ArrayList. /** * Create a new list of Circles, starts with 5 default circles. */ public PolkaDots() { dots = new ArrayList<Circle>(); // an arrayList to hold a bunch of circles Circle circle1 = new Circle(30, 10, 10, "blue");//create the 1st circle dots.add(circle1);// add the 1st circle to the arrayList    Circle...
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps...
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps classes 2. In the CompareByPlayoffsAndSalary the compare method should list: (a) teams that made the playoffs last year before teams that did not. (b) If this is the same then it should list teams with lower salary first. 3. In the CompareByWinsLossesChamps list: (a) teams with more wins first. (b) If the same then list by teams with fewer losses. (c) If this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT