SHARE
TWEET

Stack Kelas SI 2A

a guest May 23rd, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import sys
  3.  
  4. #Beberapa Fungsi untuk operasi stack
  5.  
  6. def push(S, item) :
  7.     S.append(item)
  8.  
  9. def pop(S) :
  10.     return S.pop()
  11.  
  12. def size(S) :
  13.     return len(S)
  14.  
  15. def peek(S) :
  16.     return S[len(S)-1]
  17.  
  18. def menuAplikasi() :
  19.     print('===================')
  20.     print('Menu Aplikasi Stack')
  21.     print('===================')
  22.     print('1. Push')
  23.     print('2. Pop')
  24.     print('3. Cetak')
  25.     print('4. Peek')
  26.     print('5. Size')
  27.     print('6. Keluar')
  28.  
  29. def operasi(x) :
  30.     global ulang
  31.     global stack
  32.    
  33.     if x == 1 :
  34.         push(stack, int(input('Push : ')))
  35.         print('Elemen Stack Sekarang : ', stack,'\n')
  36.     elif x == 2 :
  37.         if len(stack) == 0 :
  38.             print('----------------------------------------')
  39.             print('Maaf, Stack Kosong, Silahkan Push Dulu !')
  40.             print('----------------------------------------')
  41.             print()
  42.         else :
  43.             pop(stack)
  44.             print('Elemen Stack Sekarang : ', stack,'\n')
  45.     elif x == 3 :
  46.         print('Elemen Stack Sekarang : ', stack,'\n')
  47.     elif x == 4 :
  48.         if len(stack) == 0 :
  49.             print('----------------------------------------')
  50.             print('Maaf, Stack Kosong, Silahkan Push Dulu !')
  51.             print('----------------------------------------')
  52.             print()
  53.         else :
  54.             print('Elemen stack terakhir adalah : ', peek(stack),'\n' )
  55.     elif x == 5 :
  56.         print('Elemen stack berjumlah     : ', size(stack),'\n')
  57.     elif x == 6 :
  58.         sys.exit(0)
  59.     else :
  60.         #os.system('cls')
  61.         print()
  62.         print('---------------------------')
  63.         print('Maaf, ada kesalahan input !')
  64.         print('---------------------------')
  65.         ulang = 't'
  66.  
  67. stack = []
  68. ulang = 'y'
  69. while ulang == 'y' :
  70.     menuAplikasi()
  71.     pilihan = int(input('Operasi yang Anda inginkan : '))
  72.     operasi(pilihan)
  73.  
  74.  
  75. #input()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top