Materi Linked List - Single Link dan Double Link - Struktur Data
Materi Linked List
A. Single List
Single Linked List adalah sekumpulan dari node yang saling terhubung dengan node lain melalui sebuah pointer.
Rangkaian single linked list tersebut diawali dengan sebuah head untuk menyimpan alamat awal dan di akhiri dengan node yang mengarah pointer ke null.
Single Linked List hanya memiliki satu arah dan tidak memiliki dua arah atau bulak balik, dua arah tersebut disebut dengan double linked list.
Pada Implementasinya, Single Linked List terdapat dua variasi yaitu circular dan non-circular.
Berikut adalah ilustrasi single linked list Non-Circular
Single Linked List Non-Circular
sedangkan untuk single linked list Circular nya adalah sebagai berikut.
Single Linked List Circular
Single Linked List sendiri pun, terdapat beberapa metode yang dapat dilakukan yaitu :
- Creations
- Insert : 1. Depan
- Delete : 1. Depan
- Traversal
- Sorting
- Searching
- Termination
2. Belakang
3. Posisi
2. Belakang
3. Posisi
Souces Code :
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
class OrderedList:
def __init__(self):
self.head = None
def show(self):
current = self.head
print('Head -> ',end='')
while current != None:
print(current.getData(),end=' -> ')
current = current.getNext()
print('None')
def add(self,item):
temp = Node(item)
current = self.head
previous = None
stop = False
while current != None and not stop:
print('adding',item)
if current.getData() < item:
stop = True
else:
previous = current
current = current.getNext()
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
def size(self):
current = self.head
count = 0
while current != None:
count += 1
current = current.getNext()
return count
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
print('search',current.getData())
if current.getData() == item:
found = True
else:
if current.getData() < item:
stop = True
else:
current = current.getNext()
return found
def remove(self,item):
current = self.head
previous = None
found = False
stop = False
while current != None and not found and not stop:
print('remove',current.getData())
if current.getData() == item:
found = True
else:
if current.getData() < item:
stop = True
else:
previous = current
current = current.getNext()
if found:
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
else:
print('data tidak ada')
def isEmpty(self):
return self.head == None
def autoInsert(mylist,alist):
for i in alist:
mylist.add(i)
mylist = OrderedList()
alist = [1,2,3,4,5,6,7,8,9,10]
autoInsert(mylist,alist)
mylist.show()
print('Panjang List =',mylist.size())
print('found',mylist.search(3.5))
mylist.remove(10)
mylist.show()
Hasil Run :
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "copyright", "credits" or "license()" for more information.
>>>
=========== RESTART: /home/rejeb/Downloads/1orderedListMenurun.py ===========
adding 2
adding 3
adding 4
adding 5
adding 6
adding 7
adding 8
adding 9
adding 10
Head -> 10 -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> None
Panjang List = 10
search 10
search 9
search 8
search 7
search 6
search 5
search 4
search 3
found False
remove 10
Head -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> None
>>>
B. Double Link List
Double Link List adalah elemen-elemen yang dihubungkan dengan dua pointer dalam satu elemen dan list dapat melintas baik di depan atau belakang.
Elemen double link list terdiri dari tiga bagian:
- Bagian data informasi
- Pointer next yang menunjuk ke elemen berikutnya
- Pointer prev yang menunjuk ke elemen sebelumnya
Untuk menunjuk head dari double link list, pointer prev dari elemen pertama menunjuk NULL. Sedangkan untuk menunjuk tail, pointer next dari elemen terakhir menunjuk NULL.
Jenis-jenis Double Link :
- Double Linked List Circular
- 1 field pointer yang menunjuk pointer berikutnya “next“,
- 1 field menunjuk pointer sebelumnya ” prev “,
- 1 field yang berisi data untuk node tersebut .
- Double Linked List Non Circular
Pengertian secara umumnya DLLC itu Linked list yang menggunakan pointer, dimana setiap node memiliki 3 field, yaitu:
Double Linked List Circular pointer next dan prev nya menunjuk kedirinya sendiri secara circular. Bentuk Node DLLC
DLLNC "Double linked list non circular" adalah Double Linked List yang memiliki 2 buah pointer yaitu pointer next dan prev. Pointer next menunjuk pada node setelahnya dan pointer prev menunjuk pada node sebelumnya.
Pengertian:
Double : artinya field pointer-nya dua buah dan dua arah, ke node sebelum dan sesudahnya.
Linked List : artinya node-node tersebut saling terhubung satu sama lain.
Non Circular : artinya pointer prev dan next-nya akan menunjuk pada NULL.
Sources Code :
Hasil Run :
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
self.prev = None
def getData(self):
return self.data
def getNext(self):
return self.next
def getPrev(self):
return self.prev
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
def setPrev(self,newprev):
self.prev = newprev
class OrderedList:
def __init__(self):
self.head = None
def show(self):
current = self.head
print ('None <- -="" ead="" end="" print="">', end='')
while current != None:
if current.getNext() == None:
print (current.getData(), end='->')
else:
print (current.getData(), end='<->')
current = current.getNext()
print ('None')
def isEmpty(self):
return self.head == None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
temp.setPrev(None)
current = self.head
previous = None
stop = False
while current != None and not stop:
print('adding',item)
if current.getData() <= item:
stop = True
else:
previous = current
current = current.getNext()
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == item:
found = True
else:
if current.getData() < item:
stop = True
else:
current = current.getNext()
return found
def remove(self,item):
current = self.head
previous = None
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == item:
found = True
else:
if current.getData() < item:
stop = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
if current == None:
previous.setNext(None)
else:
previous.setNext(current.getNext())
if current is None:
print("Data not in list")
def autoInsert(mylist,alist):
for i in alist:
mylist.add(i)
mylist = OrderedList()
alist = [1,2,3,4,5,6,7,8,9,10]
autoInsert(mylist,alist)
mylist.show()
print('Panjang List =',mylist.size())
print('found',mylist.search(3))
print('found',mylist.search(3.5))
mylist.remove(10)
mylist.show()
->->
Hasil Run :
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "copyright", "credits" or "license()" for more information.
>>>
============ RESTART: /home/rejeb/Downloads/2DoubleLinkMenurun.py ============
adding 2
adding 3
adding 4
adding 5
adding 6
adding 7
adding 8
adding 9
adding 10
None <-head -="">10 <-> 9 <-> 8 <-> 7 <-> 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1-> None
Panjang List = 10
found True
found False
None <-head -="">9 <-> 8 <-> 7 <-> 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1-> None
>>>
->->->->->->->->-head>->->->->->->->->->-head>
Comments
Post a Comment