Skip to content

Instantly share code, notes, and snippets.

@bellalMohamed
Created November 25, 2017 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellalMohamed/6b7242c2a82216827a2ee80fbde3087d to your computer and use it in GitHub Desktop.
Save bellalMohamed/6b7242c2a82216827a2ee80fbde3087d to your computer and use it in GitHub Desktop.
LinkedList Task Code
class Node:
def __init__(self, data = None):
self.data = data
self.next = None
def setData(self, data = None):
self.data = data
def setNext(self, next = None):
self.next = next
def getData(self):
return self.data
def getNext(self):
return self.next
class LinkedList:
def __init__(self):
self.head = None
def add(self, item):
newNode = Node(item)
newNode.setNext(self.head)
self.head = newNode
def display(self):
elements = []
currentNode = self.head
while currentNode != None:
elements.append(currentNode.getData())
currentNode = currentNode.getNext()
print(elements)
def length(self):
count = 0
currentNode = self.head
while currentNode != None:
count += 1
currentNode = currentNode.getNext()
return count
myList = LinkedList()
myList.add(50)
myList.add(60)
myList.add(70)
myList.display()
print(myList.length())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment