Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
LinkedList in Python
# This program is an example of how to create a LinkedList in Python. # # # # # NODE CLASS class Node: def __init__(self, data): # Method to initialise the node object. self.data = data # The data to be stored in the node. self.next = None # Variable to store the object of the next node. def add(self, data): if self.next != None: # Checking if the 'next' object has some value in it. self.next.add(data) # If the 'next' node has already been initialised then pass the data to it. else: self.next = Node(data) # If the 'next' node has no value assigned to it, then assign an object to it. def nodePrint(self): ending = " -> " if self.next else "" # Deciding whether to print an arrow or not. (Depends on whether their is another node after this). type_of_data = str(type(self.data)) # Getting the type of the data and converting to string. # In Python a string has the type: <class 'str'> # The line below takes only the useful part and prints it.. type_of_data = type_of_data[7 : len(type_of_data) - 1] print(self.data, type_of_data, sep = " : ", end = ending) # If their is another node after this, then print it as well. if self.next: self.next.nodePrint() # LINKEDLIST CLASS class LinkedList: # Method to create the def __init__(self): self.head = None # Method to add a new node to the def add(self, data): if self.head: self.head.add(data) else: self.head = Node(data) # Method to print the linked-list. def listPrint(self): self.head.nodePrint() # MAIN PROGRAM START linked = LinkedList() linked.add(5) linked.add("name") linked.add(True) linked.listPrint() # MAIN PROGRAM END # PROGRAM BY 'JAMES COLLINS'
run
|
edit
|
history
|
help
1
RANDOM NUMBER
Creating Person class
Gift_Card Interview SQL Analysis Conducted by Miranda Zhao
mbti
lambda_filter_map_reduce.py
Python Unit Test Case Sample
normal,*args,**kwargs
square cube function
(P3) Scrabble
Single and double qoute