linkedlist Crashcourse-2
linkedlist Crashcourse-2
startPointer = 0
emptyList = 5
# CREATE NODE
newNode = node(datatoadd, -1)
#NOW STORE IT IN THE freelist
linkedList[freelist] = newNode
# YOUR NODE IS STORE NOW JUST HAVE TO ADJUST THE POINTER AND SEARCH FOR THE
LAST NODE
previousPointer = 0
while currentPointer != -1:
previousPointer = currentPointer
currentPointer = linkedList[currentPointer].nextNode
# WHEN YOUR WHILE LOOP WILL BE FINISHED YOU WILL HAVE THE LOCATION OF THE
LAST NODE IN PREVIOUS POINTER
linkedList[previousPointer].nextNode = freelist
return True
def deleteNode():
global linkedList
global emptyList
global startPointer
currentpointer = startPointer
#Now take the input of the value that you want to remove
DataToRemove = int(input("Enter the Data that you want to remove: "))
#SEARCH THE INDEX VALUE OR LOCATION OF THE NODE THAT YOU WANT TO REMOVE
previouspointer = 0
while currentpointer != -1 and linkedList[currentpointer].data != DataToRemove:
previouspointer = currentpointer
currentpointer = linkedList[currentpointer].nextNode
# WHENEVER YOU WILL COME OUT FROM THE WHILE LOOP YOU WILL HAVE THE LOCATION IN
C.P
if currentpointer == -1:
return False # WHICH MEANS THE VALUE DOES NOT EXIST
else:
#CHANGE THE POINTERS
# THERE ARE TWO CASE EITHER FIRST NODE TO BE REMOVED OR THE IN BETWEEN ONE
if currentpointer == startPointer:
startPointer = linkedList[currentpointer].nextNode
else:
linkedList[previouspointer].nextNode =
linkedList[currentpointer].nextNode
linkedList[currentpointer].data = 0
linkedList[currentpointer].nextNode = emptyList
emptyList = currentpointer
return True
def OutputNode():
global linkedList
global startPointer
currentpointer = startPointer
while currentpointer != -1:
print(linkedList[currentpointer].data)
currentpointer = linkedList[currentpointer].nextNode
return currentpointer
def OrderedInsertion(CurrentPointer):
global linkedList
global emptyList
global startPointer
# When the while loop will be finished you will have the correct position
#There can be two cases first is that you are suppose to add it in the
first
#place or in between
if CurrentPointer == startPointer:
linkedList[freelist].nextNode = startPointer
startPointer = freelist
else:
linkedList[freelist].nextNode = linkedList[PreviousPointer].nextNode
linkedList[PreviousPointer].nextNode = freelist
return True
OrderedInsertion(startPointer)
OutputNode()