Pembuatan Single Linked List dapat menggunakan 2 metode:
– LIFO (Last In First Out), aplikasinya : Stack (Tumpukan)
– FIFO (First In First Out), aplikasinya : Queue (Antrean)
Berikut contoh programnya
class node:def __init__(self,initdata):self.data = initdataself.next = Nonedef getdata (self):return self.datadef getnext(self):return self.nextdef setdata (self,newdata):self.data = newdatadef setnext (self,newnext):self.next = newnextclass orderedlist:def __init__(self):self.head = Nonedef show (self):current = self.headprint("Head -> ", end = "")while current != None: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)self.head = tempdef size(self):current = self.headcount = 0while current != None :count += 1current = current.getnext()return countdef search(self):current = self.heads = int(input("masukkan data yang akan dicari : "))found = Falsewhile current != None and not found:if current.getdata() == s:found = Trueelse:current = current.getnext()if found == True :print("Data ditemukan")else :print("Data tidak ditemukan")def remove (self):current = self.heads = int(input("masukkan data yang akan dihapus : "))previous = Nonefound = Falsewhile not found and current != None:if current.getdata() == s:found = Trueelse:previous = currentcurrent = current.getnext()if found == True :if previous == None:self.head = current.getnext()else:previous.setnext(current.getnext())print("Data sudah dihapus")else :print("Data yang akan dihapus tidak ditemukan")def ganjil(self,item):for i in range(len(item)):if item[i] % 2 == 1:ganjil.append(item[i])ol.sort(ganjil)print(ganjil)def genap(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.ganjil(lis)ol.genap(lis)genap.reverse()merge = ganjil + genapfor i in merge:ol.add(i)ol = orderedlist()lis = [170411100001,170411100002,170411100003,170411100004,170411100005,170411100006]genap=[]ganjil=[]merge = ganjil + genapol.tambah(ol)ol.show()print("panjang datanya adalah",orderedlist.size(ol))orderedlist.search(ol)orderedlist.remove(ol)ol.show()
EmoticonEmoticon