Doubly 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 doubly 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 Doubly Linked List.
Berikut contoh programnya:
class Node:def __init__(self,initdata):self.data = initdataself.next = Noneself.prev = Nonedef getData(self):return self.datadef getNext(self):return self.nextdef getPrev(self):return self.prevdef setData(self,newdata):self.data = newdatadef setNext(self,newnext):self.next = newnextdef setPrev(self,newprev):self.prev = newprevclass orderedlist:def __init__(self):self.head = Nonedef show(self):current = self.headprint ('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 == Nonedef add(self,item):temp = Node(item)temp.setNext(self.head)temp.setPrev(None)next = temp.getNext()if next != None:next.setPrev(temp)self.head = tempdef size(self):current = self.headcount = 0while current != None:count = count + 1current = current.getNext()return countdef gan(self,item):for i in range(len(item)):if item[i] % 2 == 1:ganjil.append(item[i])ol.sort(ganjil)print(ganjil)def gen(self,item):for i in range(len(item)):if item[i] % 2 == 0:genap.append(item[i])ol.sort(genap)print(genap)def sort(self,item):for x in range(len(item)-1, 0, -1):for y in range(x):if item[y] > item[y+1]:item[y], item[y+1] = item[y+1], item[y]def tambah(self,item):pil = "y"while pil == "y":tam = int(input("masukkan data : "))lis.append(tam)pil = str(input("apakah anda mau memasukkan data lagi ? (y/n)"))ol.gan(lis)ol.gen(lis)genap.reverse()merge = ganjil + genapfor i in merge:ol.add(i)def search(self):current = self.heads = int(input("masukkan data yang akan dicari : "))found = Falsewhile current != None and not found:if current.getData() == s:found = Trueprint('Data in List')else:current = current.getNext()if found == False:print('Data not in List')return founddef remove(self):current = self.heads = int(input("masukkan data yang akan dihapus : "))previous = Nonefound = Falsewhile current != None and not found:if current.getData() == s:found = Trueprint("Data deleted")else:previous = currentcurrent = current.getNext()if current == None:next = Noneelse:next = current.getNext()if previous == None:self.head = current.getNext()else:if current == None:previous.setNext(None)else:previous.setNext(current.getNext())if next != None:next.setPrev(current.getPrev)if current is None:print("Data not in list")ol = orderedlist()lis = [170411100001,170411100002,170411100003,170411100004,170411100005]genap=[]ganjil=[]merge = ganjil + genapol.tambah(ol)ol.show()print("panjang datanya adalah",orderedlist.size(ol))orderedlist.search(ol)orderedlist.remove(ol)
EmoticonEmoticon