-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcercise.py
46 lines (33 loc) · 880 Bytes
/
excercise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insertHead(self, newNode):
temporaryNode = self.head
self.head = newNode
self.head.next = temporaryNode
del temporaryNode
def insertEnd(self, newNode):
if self.head is None:
self.head = newNode
else:
lastNode = self.head
while lastNode.next:
lastNode.next = newNode
lastNode.next = newNode
def printList(self):
currentNode = self.head
while currentNode:
print(currentNode.data)
currentNode = currentNode.next
firstNode = Node('Faruk')
linkedList = LinkedList()
linkedList.insertEnd(firstNode)
secondNode = Node('Idris')
linkedList.insertEnd(secondNode)
thirdNode = Node('Ahmad')
linkedList.insertHead(thirdNode)
linkedList.printList()