Page 5 - DATA STRUCTURE-LISTS
P. 5
def Insert(L, a):
l=len(L)
L.append(None)
for i in range (l-1, -1, -1):
if L[i]>a:
L[i+1]=L[i]
if i==0:
i=-1
else:
break
L[i+1]=a
X=[11, 33, 55, 77, 88, 99]
a=int(input("Enter element to insert: "))
Insert(X, a)
print(X)
Insertion in a sorted array using bisect module:
(bisect works with sorted(ASCENDING order) list only)
import bisect
def Insert(L, a):
loc=bisect.bisect(L, a) #returns location for inserting element
bisect.insort(L, a) #inserts element in proper place in a sorted list
print("Element", a, "inserted at location",loc)
X=[11, 33, 55, 77, 88, 99]
print("Original list is:")
print(X)
p=int(input("\nEnter element to insert: "))
Insert(X, p)
print("\nUpdated list is:")
print(X)
Deletion (General method):
Search the element, delete it, shift element upward, update number of elements in the list
Deletion