Jumat, 12 Juli 2019

Program Python Mengurutkan data dengan linked list

Tutorial kali ini ,membuat program python Mengurutkan data dengan linked list,Python yang digunakan versi python 2.7


Berikut Source Code :

class Node :

    def __init__(self,data):
        self.data = data
        self.next = None

class linkedlist :

    def __init__(self):
        self.head = None


    def insert(self,new_data):
        new_node = Node(new_data)
        if self.head is None :
            self.head = new_node
            return
        last = self.head
        while(last.next):
            last = last.next

        last.next = new_node

    def showdata(self):
        current = self.head
        while current is not None :
            print current.data,
            current = current.next

    def sortlist(self):
        current =self.head
        index = None
        if(self.head == None):
            return
        else:
            while(current != None):
                index = current.next

                while(index != None):
                    if (current.data > index.data):
                        temp = current.data
                        current.data = index.data
                        index.data = temp
                    index = index.next
                current = current.next

llist1 = linkedlist()
llist1.insert(9)
llist1.insert(0)
llist1.insert(4)
llist1.insert(3)
print "Sebelum Diurutkan"
llist1.showdata()
print "\n"
print "Sesudah Diurutkan"
llist1.sortlist()
llist1.showdata()


Output :

Video bisa dilihat dibawah : 


Source code : github


Semoga Bermanfaat.... 

2 komentar: