Key ideas:
- When you insert an element linear table, all the elements are inserted into the position after a shift
- Considerations:
- Linear table is a linear table is full
- Table linear insertion position feasibility
1 class linearlist_insert_elem(): 2 def __init__(self, data, maxsize): 3 self.data = data 4 self.maxsize = maxsize 5 self.length = len(data) 6 def list_insert(self, i, e): 7 if self.length == self.maxsize: 8 print("线性表%s已满,无法插入元素" % (self.data)) 9 # return -1 10 if i < 1 or i > self.length: 11 Print ( " can not be inserted at a position element% s " % I) 12 is # return 0 13 is the else : 14 # line of a table append the tail element 0 15 self.data.append (0) 16 for I in Range (self.length , I, -1 ): . 17 self.data [I] = self.data [I -. 1 ] 18 is self.data [I] = E . 19 self.length +. 1 = 20 is Print ( " inserted at position element% s% s success " % (I, E)) 21 is return self.data 22 if __name__ == '__main__': 23 ob = linearlist_insert_elem([1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14], 20) 24 print(ob.data) 25 print(ob.length) 26 ob.list_insert(20, 17) 27 print(ob.data) 28 ob.list_insert(6, 17) 29 print(ob.data)