DOUBLE LINKED LIST
DOUBLE LINKED LIST
1.
PENGERTIAN DOUBLE LINKED LIST
Double Linked List sama
seperti Single Linked List merupakan sebuah tempat yang
disediakan pada satu area memori tertentu untuk menyimpan data yang dikenal
dengan sebutan node atau simpul. Akan tetapi, setiap node pada double
linked list selain memiliki pointer yang menunjuk ke simpul berikutnya
sehingga terbentuk satu untaian, juga memiliki pointer yang menunjuk ke simpul
sebelumnya. Susunan berupa untaian model ini disebut Double Linked
List.
2.
SOURCE CODE
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
self.previous = None
def getData(self):
return self.data
def getNext(self):
return self.next
def getPrevious(self):
return self.previous
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
def setPrevious(self, newprevious):
self.previous = newprevious
class Unorderedlist:
def __init__(self):
self.head = None
def show(self):
current = self.head
print('None <-')
print('Head ->', 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.setPrevious(None)
self.head = 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
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def remove(self,item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
next = current.getNext
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
mylist = Unorderedlist()
mylist.add(31)
mylist.add(11)
mylist.add(15)
mylist.add(30)
mylist.show()
print(mylist.size())
print(mylist.search(54))
print(mylist.remove(11))
Di post oleh :
1. FITRI ANISA (170411100014)
2. DHEA OKY SYAFITRI (170411100006)
Komentar
Posting Komentar