From c809f5bde3629ce66f206545baac318f2624627d Mon Sep 17 00:00:00 2001 From: ZakyHermawan Date: Fri, 17 May 2019 22:37:41 +0700 Subject: [PATCH] linked list operations --- operations.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ transverse.py | 31 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 operations.py create mode 100644 transverse.py diff --git a/operations.py b/operations.py new file mode 100644 index 0000000..4be7447 --- /dev/null +++ b/operations.py @@ -0,0 +1,73 @@ +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class Linked: + def __init__(self): + self.head = None + + #insert data di awal + def shift(self, newValue): + newNode = Node(newValue) + + newNode.next = self.head + self.head = newNode + + #insert data di akhir + def push(self, newValue): + newNode = Node(newValue) + + last = self.head + while last.next: + last = last.next + + last.next = newNode + + #mengambil data paling awal + def unshift(self): + return self.head + + #mengambil data paling akhir + def pop(self): + last = self.head + + while last.next != None: + last = last.next + + return last.data + + def printVal(self): + value = self.head + + while value != None: + print(value.data) + value = value.next + + + + +#inisiasi +first = Linked() +first.head = Node(1) +second = Node(2) +third = Node(3) +fourth = Node(4) + +#hubungkan antar node +first.head.next = second +second.next = third +third.next = fourth + +first.shift("Shifted") +first.push("Push Success") + +first.printVal() + +foremost = first.unshift() +lattermost = first.pop() + +print("") + +print("Data paling awal : " + foremost.data) +print("Data paling akhir : " + lattermost) diff --git a/transverse.py b/transverse.py new file mode 100644 index 0000000..3efcd7c --- /dev/null +++ b/transverse.py @@ -0,0 +1,31 @@ +class Node: + def __init__(self, data = None): + self.data = data + self.next = None + +class Linked: + def __init__(self): + self.head = None + + def printList(self): + value = self.head + + while value != None: + print(value.data) + value = value.next + + +first = Linked() + +#buat node +first.head = Node(1) +second = Node(2) +third = Node(3) +fourth = Node(4) + +#hubungkan antar node +first.head.next = second +second.next = third +third.next = fourth + +first.printList()