Question
Python Linked List Implementation Can someone please help me complete the insert method under the SLinkedList class? I'm not sure if I'm on the right
Python Linked List Implementation
Can someone please help me complete the insert method under the SLinkedList class? I'm not sure if I'm on the right track.. And here is my code so far...
class SLinkedListNode: def __init__(self, initData, initNext): self.data = initData self.next = initNext #excessors - get them and set them #getters def getNext(self): return self.next def getData(self): return self.data #getters def setData(self, newData): self.data = newData def setNext(self, newNext): self.next = newNext class SLinkedList: def __init__(self): self.head = None self.size = 0 def add(self, item): # add an item -- add will happen at the beginning of the list == add a new node # move the head, so that it moves to the new node # its next should point to the one that was existing before new_node = SLinkedListNode(item, None) new_node.setNext(self.head) self.head = new_node self.size = self.size + 1 def append(self, item): # item will be added at the end of the list # new node's next will always be none new_node = SLinkedListNode(item, None) current = self.head # if you wanna travel the list if self.size == 0: self.add(item) else: while (current.getNext() != None): #why? current != None, just want to reach the nod that references None current = current.getNext() # traversing the list current.setNext(new_node) self.size = self.size + 1 def insert(self, pos, item): # must check the position before inserting assert isinstance(pos, int), 'position must be of type int' # checks if position is an integer assert pos >=0, 'position must be of positive number' # not accepting a negative index if pos == 0: self.add(item) # add the position at the beginning of the list elif pos >= self.size: self.append(item) else: new_node = SLinkedListNode(item,None) current = self.head previous = None found = True index = 0 while not found: current = current.getNext() index += 1 def index(self,item): #return the location/index of the give item # if the item does not exist, returns an appropriate message # must check if the list is empty or not # check size or if head is pointing to None if self.size == 0: #self.head == None raise Exception('List is Empty!') current = self.head # travel through the list until we find the position or reach None found = False index = 0 while current != None and not found: #when you do something repeatedly use while loop if current.getData() == item: found = True else: current = current.getNext() index = index + 1 if found == True: return index else: return 'Item not found' # not an exception, just reporting def pop(self): # remove the last item of the list if self.size == 0: # self.head == None raise Exception('List is Empty') current = self.head previous = None while current.getNext() != None: # not go up to None, but the node that references None previous = current # store current value of current before changing it current = current.getNext() if previous == None: # is there only one item in the list?? self.head = None # list is completely empty -- need to set head to None else: previous.setNext(None) # set the previous as the end of the list self.size = self.size - 1 return current.getData() # return the data inside the node that was removed def remove(self, item): # remove item from the list -- data is gone, reference is gone # get the current's next -- save it # go to the previous item in the list, and set the next to the new next # new_next = current's next # getNext and then setNext # also need a reference to the previous' node
current = self.head #traversing the list previous = None found = False while current != None and not found: if current.getData() == item: found = True else: previous = current current = current.getNext() # keep going through the list
if not found: print('Item not in the list') # raise Exception('Item not in list') else: if previous == None: # the item is in the first node of the list self.head = current.getNext() else: previous = setNext(current.getNext()) def __str__(self): current = self.head string = '' while current != None: string = string + str(current.getData()) + '->' current = current.getNext() return string
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started