Question
python3

Question 2.4. Find element at index Linked List are not arrays. It means you have to start at the beginning of the linked lis
0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

class LinkNode:

def __init__(self,value,nxt=None):
  
assert isinstance(nxt, LinkNode) or nxt is None

self.value = value

self.next = nxt

def print_list(lst):

temp = lst

while temp:

print(temp.value, end=" ")

temp = temp.next
  
def is_empty(lst):
   if lst.value is None:
       return True
   else:
       return False


def get_item(self, data):
   current = self
   found = False
   if data<0:
       return "Index is out of Bounds"
   if (is_empty(self)):
       return "list is empty"
   for k in range(0,data):
       if current and found is False:
           if k == data:
               found = True
           else:
               current = current.next
       if current is None :
           return "Index is out of Bounds"
   return current.value
  


print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),1))

print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),0))

print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,LinkNode(0,LinkNode(17))))),4))

print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),4))

print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),-4))

print(get_item(LinkNode(None,None),0))

output:

7 Index is out of Bounds Index is out of Bounds list is empty

Add a comment
Know the answer?
Add Answer to:
python3 Question 2.4. Find element at index Linked List are not arrays. It means you have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value...

    Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Question 2.1. Empty Node In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None. Add...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • Python3 You should use the getters and setters I post in the first pic 157 158...

    Python3 You should use the getters and setters I post in the first pic 157 158 class LinkNode: 159 160 def-initー(self.value,nxt One): assert isinstance(nxt,Lin㎾ode) or nxt is None self.value value self.next = nxt 61 162 163 164 165 166 167 168 169 170 def get_value(self): def set_value(self, value): def get_next(seif): def set_next(self,nxt): def _repr (self): return self.value self.value = value return self.next self.next = nxt return repr(self.value)+ ", "+repr(self.next) 172 173 A permutation can be considered as an injective (one-to-one)...

  • 1. Create a node class and linked list class. 2. Add the following function from lecture...

    1. Create a node class and linked list class. 2. Add the following function from lecture to your linked list class addFirst(e) addLast (e) add(index, e) removeFirst removeLast remove(index) 3. Add the following methods to the linked list class. a. # Return the size of the list def getSize(self): b. # Clear the list */ def clear(self): c. # Return the element from this list at the specified index def get(self, index):

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method...

    Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Suppose you want to remove an item from a singly linked list at "index". Which of...

    Suppose you want to remove an item from a singly linked list at "index". Which of the following will NOT accomplish this? Group of answer choices 1. Node<E> before = getNode(index); Node<E> toRemove = before.next; toReturn = toRemove.item; before.next = toRemove.next; 2. Node<E> before = getNode(index - 1); toReturn = before.next.item; before.next = before.next.next; 3. Node<E> before = getNode(index - 1); Node<E> toRemove = before.next; toReturn = toRemove.item; before.next = toRemove.next;

  • Below is the Graph file that needs to be modified(using Python3) : #!/usr/bin/python3 # Simple Vertex...

    Below is the Graph file that needs to be modified(using Python3) : #!/usr/bin/python3 # Simple Vertex class class Vertex: """ Lightweight vertex structure for a graph. Vertices can have the following labels: UNEXPLORED VISITED Assuming the element of a vertex is string type """ __slots__ = '_element', '_label' def __init__(self, element, label="UNEXPLORED"): """ Constructor. """ self._element = element self._label = label def element(self): """ Return element associated with this vertex. """ return self._element def getLabel(self): """ Get label assigned to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT