In: Computer Science
In Python:
def _nodeAtIndex(self, index):
"""Returns the reference to the node at the given index;
If index is out of range, raise IndexError
"""
# PROBLEM 2
# You can assume that index is non-negative.
# You need to traverse the list and stop at the required
index.
# YOUR CODE HERE
(THIS IS WHAT I HAVE CURRENTLY WRITTEN BUT I"M STUCK AND
DON"T KNOW WHAT TO DO)
while current != None: # use a while-loop.
plist.append(current._element) # process the stored element.
count = 0 #Will give us the index of current node
if (count == index):
return (plist)
raise IndexError('The index is out of range')
Down below is the following code that give's more detail about the code. If you have any question's or need any more info, please don't hesitate to ask.
class SLinkedList:
"""Singly linked list with access to front and end, and with stored
size.
"""
#-------------------------- nested _Node class
--------------------------
class _Node:
__slots__ = '_element', '_next' # streamline memory usage
def __init__(self, element, next):
self._element = element
self._next = next
#------------------------------- queue methods
-------------------------------
def __init__(self):
"""Create an empty list."""
self._head = None
self._tail = None
self._size = 0
def __len__(self):
"""Return the number of elements in the list."""
return self._size
def isEmpty(self):
"""Return True if the list is empty."""
return self._size == 0
# READ THIS!
def __repr__(self):
plist = []
current = self._head
# This is how to traverse a list:
while current != None: # use a while-loop.
plist.append(current._element) # process the stored element.
current = current._next # jump to the next node.
return "SLinkedList(%s)" % repr(plist)
def first(self):
"""Return but do not remove the first element.
Raise EmptyError if the list is empty.
"""
if self.isEmpty():
raise EmptyError('The SLinkedList is empty')
return self._head._element
def deleteFirst(self):
"""Remove and return the first element.
Raise EmptyError if the list is empty.
"""
if self.isEmpty():
raise EmptyError('The SLinkedList is empty')
answer = self._head._element
self._head = self._head._next
self._size -= 1
if self.isEmpty(): # special case when list is empty
self._tail = None # removed head had been the tail
return answer
def addFirst(self, e):
"""Add element e to the front of the list."""
self._head = self._Node(e, self._head) # create and link a new
node
if self._tail == None: # special case when list was empty
self._tail = self._head # added head is the tail
self._size += 1
def addLast(self, e):
"""Add e to the end of the list."""
newest = self._Node(e, None) # node will be new tail node
if self.isEmpty():
self._head = newest # special case: previously empty
else:
self._tail._next = newest
self._tail = newest # update reference to tail node
self._size += 1
Updated code
class SLinkedList:
"""Singly linked list with access to front and end, and with stored
size."""
#-------------------------- nested _Node class
--------------------------
class _Node:
__slots__ = '_element', '_next' # streamline memory usage
def __init__(self, element, next):
self._element = element
self._next = next
#------------------------------- queue methods
-------------------------------
def __init__(self):
"""Create an empty list."""
self._head = None
self._tail = None
self._size = 0
def __len__(self):
"""Return the number of elements in the list."""
return self._size
def isEmpty(self):
"""Return True if the list is empty."""
return self._size == 0
# READ THIS!
def __repr__(self):
plist = []
current = self._head
# This is how to traverse a list:
while current != None: # use a while-loop.
plist.append(current._element) # process the stored element.
current = current._next # jump to the next node.
return "SLinkedList(%s)" % repr(plist)
def first(self):
"""Return but do not remove the first element.
Raise EmptyError if the list is empty.
"""
if self.isEmpty():
raise EmptyError('The SLinkedList is empty')
return self._head._element
def deleteFirst(self):
"""Remove and return the first element.
Raise EmptyError if the list is empty.
"""
if self.isEmpty():
raise EmptyError('The SLinkedList is empty')
answer = self._head._element
self._head = self._head._next
self._size -= 1
if self.isEmpty(): # special case when list is empty
self._tail = None # removed head had been the tail
return answer
def addFirst(self, e):
"""Add element e to the front of the list."""
self._head = self._Node(e, self._head) # create and link a new
node
if self._tail == None: # special case when list was empty
self._tail = self._head # added head is the tail
self._size += 1
def addLast(self, e):
"""Add e to the end of the list."""
newest = self._Node(e, None) # node will be new tail node
if self.isEmpty():
self._head =self._tail= newest # special case: previously
empty
else:
self._tail._next = newest
self._tail = newest # update reference to tail node
self._size += 1
def _nodeAtIndex(self, index):
"""Returns the reference to the node at the given index;
If index is out of range, raise IndexError
"""
if(index>self._size):
raise IndexError('The index is out of range')
#if index is 1 then just retun first node
if index==1:
return self._head
count=1
current = self._head
while count!=index and current!=None:
current=current._next
count+=1
return current
code snaps for indent
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.