Download presentation
Presentation is loading. Please wait.
Published byMariah Dennis Modified over 9 years ago
1
Python: Linked Lists and Recursion Damian Gordon
2
Linked Lists
3
23623731 HEAD
4
Linked Lists: Declaration class ListNode: def __init__(self, value, pointer): self.value = value self. pointer = pointer # END ListNode.
5
Linked Lists: Declaration node4 = ListNode(31, None) node3 = ListNode(37, node4) node2 = ListNode(62, node3) node1 = ListNode(23, node2)
6
Linked Lists: Printing print(node1.value) print(node2.value) print(node3.value) print(node4.value)
7
Linked Lists: Printing def PrintNodes(): print("[", nodeA.value, "] -> [",nodeB.value, "] -> [",nodeC.value, "] -> [",nodeD.value, "] -> NULL") # END PrintNodes.
8
Linked Lists: Printing def PrintNodes(): print("[", nodeA.value, "] -> [",nodeB.value, "] -> [",nodeC.value, "] -> [",nodeD.value, "] -> NULL") # END PrintNodes. [ 23 ] -> [ 62 ] -> [ 37 ] -> [ 31 ] -> NULL
9
Linked Lists: Printing def PrintNodesWithLoop(): global HeadNode Current = HeadNode 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 PrintNodesWithLoop.
10
Linked Lists: Printing 23 62 37 31 def PrintNodesWithLoop(): global HeadNode Current = HeadNode 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 PrintNodesWithLoop.
11
Linked Lists: Printing def PrintNodesWithLoopAndCount(): global HeadNode Current = HeadNode CountNodes = 0 if (Current != None): while (Current.pointer != None): print(Current.value) Current = Current.pointer CountNodes = CountNodes + 1 # ENDWHILE; # Print out and count for last node CountNodes = CountNodes + 1 print(Current.value) print("Count:", CountNodes) else: print("Empty list") # END PrintNodesWithLoopAndCount.
12
Linked Lists: Printing 23 62 37 31 Count: 4 def PrintNodesWithLoopAndCount(): global HeadNode Current = HeadNode CountNodes = 0 if (Current != None): while (Current.pointer != None): print(Current.value) Current = Current.pointer CountNodes = CountNodes + 1 # ENDWHILE; # Print out and count for last node CountNodes = CountNodes + 1 print(Current.value) print("Count:", CountNodes) else: print("Empty list") # END PrintNodesWithLoopAndCount.
13
Linked Lists: Create Empty List def CreateEmptyList(): global HeadNode HeadNode = None # END CreateEmptyList.
14
Linked Lists: Delete a List def DeleteAList(): global HeadNode HeadNode = None # END DeleteAList.
15
Linked Lists: Is the List Empty? def ListIsEmpty(): global HeadNode return HeadNode == None # END ListIsEmpty.
16
Linked Lists: Find A Node def FindANode(N): global HeadNode Current = HeadNode Continued
17
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 FindANode. Continued
18
Linked Lists: Insert A Node def InsertANode(Pos, N): global HeadNode Current = HeadNode nodeX = ListNode(N, None) PositionCounter = 1 CountNodes = 0 Continued
19
Linked Lists: Insert A Node if Pos == 0: HeadNode = nodeX nodeX.pointer = Current else: while (Pos > PositionCounter): Current = Current.pointer PositionCounter = PositionCounter + 1 nodeX.pointer = Current.pointer Current.pointer = nodeX # ENDIF; # END InsertANode. Continued
20
Linked Lists: Delete A Node def DeleteANode(N): global HeadNode Previous = None Current = HeadNode Continued
21
Linked Lists: Delete A Node if Current.value == N: # THEN HeadNode = Current.pointer else: while ((Current.pointer != None) and (Current.value != N)): # DO Previous = Current Current = Current.pointer # ENDWHILE; Previous.pointer = Current.pointer # ENDIF; # END DeleteANode. Continued
22
Recursion
23
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)!
24
Recursion: Factorial # PROGRAM RecursiveFactorial def RecursiveFact(n): if n==0: # THEN return 1 else: return n * RecursiveFact(n-1) # ENDIF; # END RecursiveFact. Continued
25
Recursion: Factorial ######## MAIN PROGRAM ######### InputVal = int(input("Enter number: ")) print (RecursiveFact(InputVal)) # END RecursiveFactorial. Continued
26
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
27
Recursion: Fibonacci # PROGRAM RecursiveFibonacci def RecursiveFib(n): if n==1 or n==2: # THEN return 1 else: return RecursiveFib(n-1)+ RecursiveFib(n-2) # ENDIF; # END RecursiveFibonacci. Continued
28
Recursion: Fibonacci ######## MAIN PROGRAM ######### InputVal = int(input("Enter number: ")) print (RecursiveFib(InputVal)) # END RecursiveFibonacci. Continued
29
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
30
Decimal to Binary Conversion def DecToBin(n): if n < 0: # THEN return 'Must be a positive integer' elif n == 0: return '0' else: return DecToBin(n//2) + str(n%2) # ENDIF; # END DecToBin. Continued
31
Decimal to Binary Conversion ########### MAIN PROGRAM ########### InputVal = int(input("Enter DECIMAL number: ")) print("The number", InputVal, "is",DecToBin(InputVal), "in BINARY") # END DecimalToBinaryConversion. Continued
32
Linked Lists: Recursion How do we count the number of nodes in a linked list recursively? def RecursiveCount(Current): : return 1 + RecursiveCount(Current.pointer) :
33
Linked Lists: Recursion def RecursiveCount(Current): if Current == None: # THEN return 0 else: return 1 + RecursiveCount(Current.pointer) # ENDIF; # END RecursiveCount. print("Recursive Count:", RecursiveCount(HeadNode))
34
Linked Lists: Recursion How do we print out the nodes in a linked list recursively? def RecursivePrint(Current): : print(Current.value) RecursivePrint(Current.pointer) :
35
Linked Lists: Recursion def RecursivePrint(Current): if Current == None: # THEN return else: print(Current.value) RecursivePrint(Current.pointer) # ENDIF; # END RecursiveCount. RecursivePrint(Current)
36
Linked Lists: Recursion How do we find a node in a linked list recursively? def FindANode(Current, N): : return FindANode(Current.pointer, N) :
37
Linked Lists: Recursion def FindANode(Current, N): if (Current.pointer == None): # THEN return None elif (Current.value == N): # THEN print(N, "was found") return N else: return FindANode(Current.pointer, N) # ENDIF; # END FindANode. FindANode(Current, 37)
38
Linked Lists: Recursion How do we insert a node in a linked list recursively? def InsertANode(Current, Pos, N): : nodeX = ListNode(N, None) nodeX.pointer = Current.pointer Current.pointer = nodeX : return InsertANode(Current.pointer, Pos, N)
39
Linked Lists: Recursion def InsertANode(Current, Pos, N): if (Current.pointer == None): # THEN return None elif (Current.value == Pos): # THEN nodeX = ListNode(N, None) nodeX.pointer = Current.pointer Current.pointer = nodeX return N else: return InsertANode(Current.pointer, Pos, N) # ENDIF; # END. InsertANode. RetValue = InsertANode(Current, 37, 12345) RecursivePrint(Current)
40
Linked Lists: Recursion How do we delete a node in a linked list recursively? def DeleteANode(Current, N): : Current.pointer = DeleteANode(Current.pointer, N) :
41
Linked Lists: Recursion def DeleteANode(Current, N): if (Current != None): # THEN if (Current.value == N): # THEN Current = Current.pointer else: Current.pointer = DeleteANode(Current.pointer, N) # ENDIF; return Current # END DeleteANode. RetValue = DeleteANode(Current, 12345) RecursivePrint(Current)
42
etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.