0% found this document useful (0 votes)
5 views

linkedlist Crashcourse-2

Uploaded by

Azan Munir
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

linkedlist Crashcourse-2

Uploaded by

Azan Munir
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

class node:

#PUBLIC data : INTEGER


#PUBLIC nextNode : INTEGER
def __init__(self, dataP, nextNodeP):
self.data = dataP
self.nextNode = nextNodeP

# DECLARE linkedList : ARRAY[0:9] OF node


linkedList = [node(1, 1), node(5, 4), node(10, 7), node(85, -1), node(8, 2),
node(0, 6), node(0, 8), node(56, 3), node(0, 9), node(0, -1)]

startPointer = 0
emptyList = 5

def addNode(currentPointer): # currentPointer is acting as a startpointer parameter


global linkedList
global emptyList

datatoadd = int(input("Enter the data to add: "))

# CHECK IF THE ARRAY IS FULL OR NOT


if emptyList < 0 or emptyList > 9:
return False
else:
# STORE emptylist in temp variables
freelist = emptyList
# Now increment the emptylist for another addition
emptyList = linkedList[emptyList].nextNode

# 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

#FIRST CHECK IF THE VALUE TO BE REMOVED IS IN THE LIST OR NOT

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

# NOW ADD THE REMOVED NODE IN THE EMPTYLIST

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

def FindItem(currentpointer, DataToSearch):


global linkedList
while currentpointer != -1:
if linkedList[currentpointer].data != DataToSearch:
currentpointer = linkedList[currentpointer].nextNode
else:
return currentpointer

return currentpointer

def OrderedInsertion(CurrentPointer):
global linkedList
global emptyList
global startPointer

DataToInsert = int(input("Enter the dat that you want to add: "))


#NOW YOU ARE SUPPOSE TO CHECK THAT WE HAVE SPACE OR NOT
if emptyList < 0 or emptyList > 9:
return False
else:
#NOW YOU ARE SUPPOSE TO INCREMENT emptylist
freelist = emptyList
emptyList = linkedList[emptyList].nextNode

# CREATE THE NODE


newNode = node(DataToInsert, -1)
linkedList[freelist] = newNode

#Now have to search for the currect position


while CurrentPointer != -1 and linkedList[CurrentPointer].data <
DataToInsert:
PreviousPointer = CurrentPointer
CurrentPointer = linkedList[CurrentPointer].nextNode

# 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()

You might also like