Double Linked List Python
from __future__ import print_function
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 Orderlist:
def __init__(self):
self.head = None
def show(self):
current = self.head
print ("Head ->", end = " ")
while current != None and current.getNext() != None:
print (current.getData(), end = "<->")
current = current.getNext()
else :
print (current.getData(), end = "-> None")
print ("\nNone <-")
def isEmpty(self):
return self.head == None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
temp.setPrev(None)
self.head = temp
prev1 = temp.getData()
next1 = temp.getNext()
if next1 != None:
next1.setPrev(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
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 current != None and not found:
if current.getData()== item:
found = True
else :
previous = current
current = current.getNext()
if current == None:
next1 = None
else:
next1 = current.getNext()
if previous == None:
self.head = current.getNext()
else:
if current == None:
previous.setNext(None)
else :
previous.setNext(current.getNext())
if next1 != None:
next1.setPrev(current.getPrev())
def mainmenu(self):
print ("=".center(80,"="))
print("Selamat Datang Di Aplikasi (Double Linked List)".upper().center(80))
print("By : Moh. Irsad 170411100024".upper().center(80))
print ("=".center(80,"="))
stop = False
while not stop:
print ("=".center(30,"="))
print ("Menu Aplikasi Linked List")
print ("=".center(30,"="))
print ("1. Tambah data")
print ("2. Remove data")
print ("3. Cari data")
print ("4. Jumlah data")
print ("5. Tampil data")
print ("=".center(30,"="))
opsi = int(input("Masukkan pilihan anda : "))
print ()
if opsi == 1 :
print ("=".center(30,"="))
print ("Tambah Data".center(30))
print ("=".center(30,"="))
n = int(input("Berapa banyak data yang ingin anda masukan : "))
for i in range (n):
i +=1
data = int(input("Masukkan data ke- %d : "% i))
angka.append(data)
angka.sort()
angka.reverse()
for j in angka:
self.add(j)
print ("=".center(40,"="))
print ("Data Berhasil Ditambahkan".center(40))
print ("=".center(40,"="))
elif opsi == 2:
print ("=".center(30,"="))
print ("Remove Data".center(30))
print ("=".center(30,"="))
data = int(input("masukkan data yang ingin anda remove : "))
self.remove(data)
if data in angka:
print ("=".center(40,"="))
print ("Data berhasil di remove!!!".center(40))
print ("=".center(40,"="))
else :
print ("=".center(40,"="))
print ("Data yang ingin anda remove tidak ada".center(40))
print ("=".center(40,"="))
elif opsi == 3:
print ("=".center(30,"="))
print ("Cari Data".center(30))
print ("=".center(30,"="))
data = int(input("Masukkan data yang ingin anda cari : "))
status = self.search(data)
if status == True:
print ("=".center(40,"="))
print ("Data Ditemukan".center(40))
print ("=".center(40,"="))
else :
print ("=".center(40,"="))
print ("Data yang anda cari tidak ada".center(40))
print ("=".center(40,"="))
elif opsi == 4:
print ("=".center(30,"="))
print ("Jumlah Data".center(30))
print ("=".center(30,"="))
print ("Panjang data : ", self.size())
print ("=".center(30,"="))
elif opsi == 5:
print ("=".center(30,"="))
print ("Tampilkan Data".center(30))
print ("=".center(30,"="))
if angka == []:
print ("Data Masih Kosong")
else :
self.show()
else :
print ("=".center(40,"="))
print ("Pilihan yang anda masukan tidak ada".center(40))
print ("=".center(40,"="))
if opsi != 5:
opsi2 =raw_input("Apakah anda ingin mencoba opsi lain? (Yes/No) :")
if opsi2 == 'No':
stop = True
print("=".center(80,"="))
print("Terimakasih telah mencoba aplikasi kami.".upper().center(80))
print("Moh. Irsad 170411100024".center(80))
print("=".center(80,"="))
#Main_Program
angka = []
mylist = Orderlist()
mylist.mainmenu()
Tidak ada komentar:
Posting Komentar