Python Linked Lists and Recursion Damian Gordon Linked

  • Slides: 42
Download presentation
Python: Linked Lists and Recursion Damian Gordon

Python: Linked Lists and Recursion Damian Gordon

Linked Lists

Linked Lists

Linked Lists 23 HEAD 62 37 31 NULL

Linked Lists 23 HEAD 62 37 31 NULL

Linked Lists: Declaration class List. Node: def __init__(self, value, pointer): self. value = value

Linked Lists: Declaration class List. Node: def __init__(self, value, pointer): self. value = value self. pointer = pointer # END List. Node.

Linked Lists: Declaration node 4 node 3 node 2 node 1 = = List.

Linked Lists: Declaration node 4 node 3 node 2 node 1 = = List. Node(31, List. Node(37, List. Node(62, List. Node(23, None) node 4) node 3) node 2)

Linked Lists: Printing print(node 1. value) print(node 2. value) print(node 3. value) print(node 4.

Linked Lists: Printing print(node 1. value) print(node 2. value) print(node 3. value) print(node 4. value)

Linked Lists: Printing def Print. Nodes(): print("[", node. A. value, "] -> [", node.

Linked Lists: Printing def Print. Nodes(): print("[", node. A. value, "] -> [", node. B. value, "] -> [", node. C. value, "] -> [", node. D. value, "] -> NULL") # END Print. Nodes.

Linked Lists: Printing def Print. Nodes(): print("[", node. A. value, "] -> [", node.

Linked Lists: Printing def Print. Nodes(): print("[", node. A. value, "] -> [", node. B. value, "] -> [", node. C. value, "] -> [", node. D. value, "] -> NULL") # END Print. Nodes. [ 23 ] -> [ 62 ] -> [ 37 ] -> [ 31 ] -> NULL

Linked Lists: Printing def Print. Nodes. With. Loop(): global Head. Node Current = Head.

Linked Lists: Printing def Print. Nodes. With. Loop(): global Head. Node Current = Head. Node if (Current != None): # THEN while (Current. pointer != None): # DO print(Current. value) Current = Current. pointer # ENDWHILE; print(Current. value) else: print("Empty list") # ENDIF; # END Print. Nodes. With. Loop.

Linked Lists: Printing def Print. Nodes. With. Loop(): global Head. Node Current = Head.

Linked Lists: Printing def Print. Nodes. With. Loop(): global Head. Node Current = Head. Node if (Current != None): # THEN while (Current. pointer != None): # DO print(Current. value) Current = Current. pointer # ENDWHILE; print(Current. value) else: print("Empty list") # ENDIF; # END Print. Nodes. With. Loop. 23 62 37 31

Linked Lists: Printing def Print. Nodes. With. Loop. And. Count(): global Head. Node Current

Linked Lists: Printing def Print. Nodes. With. Loop. And. Count(): global Head. Node Current = Head. Node Count. Nodes = 0 if (Current != None): while (Current. pointer != None): print(Current. value) Current = Current. pointer Count. Nodes = Count. Nodes + 1 # ENDWHILE; # Print out and count for last node Count. Nodes = Count. Nodes + 1 print(Current. value) print("Count: ", Count. Nodes) else: print("Empty list") # END Print. Nodes. With. Loop. And. Count.

Linked Lists: Printing def Print. Nodes. With. Loop. And. Count(): global Head. Node Current

Linked Lists: Printing def Print. Nodes. With. Loop. And. Count(): global Head. Node Current = Head. Node Count. Nodes = 0 if (Current != None): while (Current. pointer != None): print(Current. value) Current = Current. pointer Count. Nodes = Count. Nodes + 1 # ENDWHILE; # Print out and count for last node Count. Nodes = Count. Nodes + 1 print(Current. value) print("Count: ", Count. Nodes) else: print("Empty list") # END Print. Nodes. With. Loop. And. Count. 23 62 37 31 Count: 4

Linked Lists: Create Empty List def Create. Empty. List(): global Head. Node = None

Linked Lists: Create Empty List def Create. Empty. List(): global Head. Node = None # END Create. Empty. List.

Linked Lists: Delete a List def Delete. AList(): global Head. Node = None #

Linked Lists: Delete a List def Delete. AList(): global Head. Node = None # END Delete. AList.

Linked Lists: Is the List Empty? def List. Is. Empty(): global Head. Node return

Linked Lists: Is the List Empty? def List. Is. Empty(): global Head. Node return Head. Node == None # END List. Is. Empty.

Linked Lists: Find A Node def Find. ANode(N): global Head. Node Current = Head.

Linked Lists: Find A Node def Find. ANode(N): global Head. Node Current = Head. Node Continued

 Continued Linked Lists: Find A Node while ((Current. pointer != None) and (Current.

Continued Linked Lists: Find A Node while ((Current. pointer != None) and (Current. value != N)): # DO Current = Current. pointer # ENDWHILE; # Print out and count for last node if (Current. pointer == None): # THEN print(N, "is not in the list") else: print("Found value: ", Current. value) # ENDIF; # END Find. ANode.

Linked Lists: Insert A Node def Insert. ANode(Pos, N): global Head. Node Current =

Linked Lists: Insert A Node def Insert. ANode(Pos, N): global Head. Node Current = Head. Node node. X = List. Node(N, None) Position. Counter = 1 Count. Nodes = 0 Continued

 Continued Linked Lists: Insert A Node if Pos == 0: Head. Node =

Continued Linked Lists: Insert A Node if Pos == 0: Head. Node = node. X. pointer = Current else: while (Pos > Position. Counter): Current = Current. pointer Position. Counter = Position. Counter + 1 node. X. pointer = Current. pointer = node. X # ENDIF; # END Insert. ANode.

Linked Lists: Delete A Node def Delete. ANode(N): global Head. Node Previous = None

Linked Lists: Delete A Node def Delete. ANode(N): global Head. Node Previous = None Current = Head. Node Continued

 Continued Linked Lists: Delete A Node if Current. value == N: # THEN

Continued Linked Lists: Delete A Node if Current. value == N: # THEN Head. Node = Current. pointer else: while ((Current. pointer != None) and (Current. value != N)): # DO Previous = Current. pointer # ENDWHILE; Previous. pointer = Current. pointer # ENDIF; # END Delete. ANode.

Recursion

Recursion

Recursion: Factorial 7! = 7 * (6 * 5 * 4 * 3 *

Recursion: Factorial 7! = 7 * (6 * 5 * 4 * 3 * 2 * 1) is 7! = 7 * 6! and 9! = 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 9!= 9 * (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) 9! = 9 * 8! and N! = N * (N-1)!

Recursion: Factorial # PROGRAM Recursive. Factorial def Recursive. Fact(n): if n==0: # THEN return

Recursion: Factorial # PROGRAM Recursive. Factorial def Recursive. Fact(n): if n==0: # THEN return 1 else: return n * Recursive. Fact(n-1) # ENDIF; # END Recursive. Fact. Continued

 Continued Recursion: Factorial #### MAIN PROGRAM ##### Input. Val = int(input("Enter number: "))

Continued Recursion: Factorial #### MAIN PROGRAM ##### Input. Val = int(input("Enter number: ")) print (Recursive. Fact(Input. Val)) # END Recursive. Factorial.

Recursion: Fibonacci • The Fibonacci numbers are numbers where the next number in the

Recursion: Fibonacci • The Fibonacci numbers are numbers where the next number in the sequence is the sum of the previous two. • The sequence starts with 1, 1, • And then it’s 2 • Then 3 • Then 5 • Then 8 • Then 13

Recursion: Fibonacci # PROGRAM Recursive. Fibonacci def Recursive. Fib(n): if n==1 or n==2: #

Recursion: Fibonacci # PROGRAM Recursive. Fibonacci def Recursive. Fib(n): if n==1 or n==2: # THEN return 1 else: return Recursive. Fib(n-1)+ Recursive. Fib(n-2) # ENDIF; # END Recursive. Fibonacci. Continued

 Continued Recursion: Fibonacci #### MAIN PROGRAM ##### Input. Val = int(input("Enter number: "))

Continued Recursion: Fibonacci #### MAIN PROGRAM ##### Input. Val = int(input("Enter number: ")) print (Recursive. Fib(Input. Val)) # END Recursive. Fibonacci.

Recursion: Decimal to Binary Conversion • How do we convert decimal numbers to binary?

Recursion: Decimal to Binary Conversion • How do we convert decimal numbers to binary? • • Let’s try the number 23… 23 / 2 = 11 and remainder is 1 11/2 = 5 and remainder is 1 5/2 = 2 and remainder is 1 2/2 = 1 and remainder is 0 1/2 = 0 and remainder is 1 >> So DEC 23 is BIN 10111

Decimal to Binary Conversion def Dec. To. Bin(n): if n < 0: # THEN

Decimal to Binary Conversion def Dec. To. Bin(n): if n < 0: # THEN return 'Must be a positive integer' elif n == 0: return '0' else: return Dec. To. Bin(n//2) + str(n%2) # ENDIF; # END Dec. To. Bin. Continued

 Continued Decimal to Binary Conversion ###### MAIN PROGRAM ###### Input. Val = int(input("Enter

Continued Decimal to Binary Conversion ###### MAIN PROGRAM ###### Input. Val = int(input("Enter DECIMAL number: ")) print("The number", Input. Val, "is", Dec. To. Bin(Input. Val), "in BINARY") # END Decimal. To. Binary. Conversion.

Linked Lists: Recursion • How do we count the number of nodes in a

Linked Lists: Recursion • How do we count the number of nodes in a linked list recursively? def Recursive. Count(Current): : : return 1 + Recursive. Count(Current. pointer) : :

Linked Lists: Recursion def Recursive. Count(Current): if Current == None: # THEN return 0

Linked Lists: Recursion def Recursive. Count(Current): if Current == None: # THEN return 0 else: return 1 + Recursive. Count(Current. pointer) # ENDIF; # END Recursive. Count. print("Recursive Count: ", Recursive. Count(Head. Node))

Linked Lists: Recursion • How do we print out the nodes in a linked

Linked Lists: Recursion • How do we print out the nodes in a linked list recursively? def Recursive. Print(Current): : : print(Current. value) Recursive. Print(Current. pointer) :

Linked Lists: Recursion def Recursive. Print(Current): if Current == None: # THEN return else:

Linked Lists: Recursion def Recursive. Print(Current): if Current == None: # THEN return else: print(Current. value) Recursive. Print(Current. pointer) # ENDIF; # END Recursive. Count. Recursive. Print(Current)

Linked Lists: Recursion • How do we find a node in a linked list

Linked Lists: Recursion • How do we find a node in a linked list recursively? def Find. ANode(Current, N): : : return Find. ANode(Current. pointer, N) : :

Linked Lists: Recursion def Find. ANode(Current, N): if (Current. pointer == None): # THEN

Linked Lists: Recursion def Find. ANode(Current, N): if (Current. pointer == None): # THEN return None elif (Current. value == N): # THEN print(N, "was found") return N else: return Find. ANode(Current. pointer, N) # ENDIF; # END Find. ANode(Current, 37)

Linked Lists: Recursion • How do we insert a node in a linked list

Linked Lists: Recursion • How do we insert a node in a linked list recursively? def Insert. ANode(Current, Pos, N): : node. X = List. Node(N, None) node. X. pointer = Current. pointer = node. X : return Insert. ANode(Current. pointer, Pos, N)

Linked Lists: Recursion def Insert. ANode(Current, Pos, N): if (Current. pointer == None): #

Linked Lists: Recursion def Insert. ANode(Current, Pos, N): if (Current. pointer == None): # THEN return None elif (Current. value == Pos): # THEN node. X = List. Node(N, None) node. X. pointer = Current. pointer = node. X return N else: return Insert. ANode(Current. pointer, Pos, N) # ENDIF; # END. Insert. ANode. Ret. Value = Insert. ANode(Current, 37, 12345) Recursive. Print(Current)

Linked Lists: Recursion • How do we delete a node in a linked list

Linked Lists: Recursion • How do we delete a node in a linked list recursively? def Delete. ANode(Current, N): : : Current. pointer = Delete. ANode(Current. pointer, N) : :

Linked Lists: Recursion def Delete. ANode(Current, N): if (Current != None): # THEN if

Linked Lists: Recursion def Delete. ANode(Current, N): if (Current != None): # THEN if (Current. value == N): # THEN Current = Current. pointer else: Current. pointer = Delete. ANode(Current. pointer, N) # ENDIF; return Current # END Delete. ANode. Ret. Value = Delete. ANode(Current, 12345) Recursive. Print(Current)

etc.

etc.