Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. class SLinkedList:
  2.     # an instance of this class is a Singly-Linked List object
  3.     # it has reference to the first node in the list
  4.     def __init__(self):
  5.         self.head = None
  6.         self.size = 0
  7.     def add(self,item):
  8.         # adds an item at the start of the list
  9.         new_node = SLinkedListNode(item,None)
  10.         new_node.setNext(self.head)
  11.         self.head = new_node
  12.         self.size = self.size + 1
  13.     def append(self,item):
  14.         # adds an item at the end of the list
  15.         new_node = SLinkedListNode(item,None)
  16.         current = self.head # Start the traversal
  17.         if self.size == 0: # check if list is empty
  18.             self.add(item)
  19.         else:
  20.             while (current.getNext()!=None):
  21.                 current= current.getNext() # traversing the list
  22.             current.setNext(new_node)
  23.             self.size = self.size +1
  24.  
  25.  
  26.     def insert(self,pos,item):
  27.         # inserts the item at pos
  28.         # pos should be a positive number of type int
  29.         # TO DO: write assert statement that tests if pos is int
  30.         # TO DO: write assert statement that tests that pos is not negative
  31.  
  32.         # TO DO: COMPLETE THE METHOD
  33.         assert isinstance(pos,int), 'pos has to be int'
  34.         assert pos >= 0, 'pos has to be positive'
  35.        
  36.         if pos == self.size:
  37.             self.append(item)
  38.         elif pos == 0:
  39.             self.add(item)
  40.         else:
  41.             current = self.head
  42.             for i in range(pos-1):
  43.                 current = current.getNext()
  44.                
  45.             new_node = SLinkedListNode(item,current.getNext())
  46.             current.setNext(new_node)
  47.  
  48.  
  49.     def remove(self,item):
  50.         # remove the node containing the item from the list
  51.         if self.size == 0:
  52.             raise Exception('List is Empty')
  53.         current = self.head
  54.         previous = None
  55.         found = False
  56.         while current != None and not found:
  57.             if current.getData() == item:
  58.                 found = True
  59.             else:
  60.                 previous = current
  61.                 current = current.getNext()
  62.         if not found:
  63.             raise Exception('Item not in list')
  64.         else:
  65.             if previous == None: # the item is in the first node of the list
  66.                 self.head = current.getNext()
  67.             else: # item is not in the first node
  68.                 previous.setNext(current.getNext())
  69.             self.size = self.size -1
  70.     def index(self,item):
  71.         # finds the location of the item in the list
  72.         if self.size == 0:
  73.             raise Exception('List is empty')
  74.         position = 0
  75.         found = False
  76.         current = self.head
  77.         while current != None and not found:
  78.             if current.getData() == item:
  79.                 found = True
  80.             else:
  81.                 current = current.getNext()
  82.                 position = position + 1
  83.         if found:
  84.             return position
  85.         else:
  86.             return 'Item not found'
  87.     def pop(self):
  88.         # removes the node from the end of the list and returns the item
  89.         if self.size == 0:
  90.             raise Exception('List is empty')
  91.         current = self.head
  92.         previous = None
  93.         while current.getNext() != None:
  94.             previous = current
  95.             current = current.getNext()
  96.         if previous == None:
  97.             self.head = None
  98.         else:
  99.             previous.setNext(None)
  100.         self.size = self.size -1
  101.         return current.getData()
  102.     def __str__(self):
  103.         # returns a string representation of the list
  104.         current = self.head
  105.         string = ''
  106.         while current != None:
  107.             string = string + str(current.getData())+'->'
  108.             current = current.getNext()
  109.         return string
  110.     def getSize(self):
  111.         return self.size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement