Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SLinkedList:
- # an instance of this class is a Singly-Linked List object
- # it has reference to the first node in the list
- def __init__(self):
- self.head = None
- self.size = 0
- def add(self,item):
- # adds an item at the start of the list
- new_node = SLinkedListNode(item,None)
- new_node.setNext(self.head)
- self.head = new_node
- self.size = self.size + 1
- def append(self,item):
- # adds an item at the end of the list
- new_node = SLinkedListNode(item,None)
- current = self.head # Start the traversal
- if self.size == 0: # check if list is empty
- self.add(item)
- else:
- while (current.getNext()!=None):
- current= current.getNext() # traversing the list
- current.setNext(new_node)
- self.size = self.size +1
- def insert(self,pos,item):
- # inserts the item at pos
- # pos should be a positive number of type int
- # TO DO: write assert statement that tests if pos is int
- # TO DO: write assert statement that tests that pos is not negative
- # TO DO: COMPLETE THE METHOD
- assert isinstance(pos,int), 'pos has to be int'
- assert pos >= 0, 'pos has to be positive'
- if pos == self.size:
- self.append(item)
- elif pos == 0:
- self.add(item)
- else:
- current = self.head
- for i in range(pos-1):
- current = current.getNext()
- new_node = SLinkedListNode(item,current.getNext())
- current.setNext(new_node)
- def remove(self,item):
- # remove the node containing the item from the list
- if self.size == 0:
- raise Exception('List is Empty')
- current = self.head
- previous = None
- found = False
- while current != None and not found:
- if current.getData() == item:
- found = True
- else:
- previous = current
- current = current.getNext()
- if not found:
- 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: # item is not in the first node
- previous.setNext(current.getNext())
- self.size = self.size -1
- def index(self,item):
- # finds the location of the item in the list
- if self.size == 0:
- raise Exception('List is empty')
- position = 0
- found = False
- current = self.head
- while current != None and not found:
- if current.getData() == item:
- found = True
- else:
- current = current.getNext()
- position = position + 1
- if found:
- return position
- else:
- return 'Item not found'
- def pop(self):
- # removes the node from the end of the list and returns the item
- if self.size == 0:
- raise Exception('List is empty')
- current = self.head
- previous = None
- while current.getNext() != None:
- previous = current
- current = current.getNext()
- if previous == None:
- self.head = None
- else:
- previous.setNext(None)
- self.size = self.size -1
- return current.getData()
- def __str__(self):
- # returns a string representation of the list
- current = self.head
- string = ''
- while current != None:
- string = string + str(current.getData())+'->'
- current = current.getNext()
- return string
- def getSize(self):
- return self.size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement