Beranda > Programming, Visual Basic .NET > Stack (Tumpukan)

Stack (Tumpukan)


Ada temen kantor yang masih kuliah dan galau dengan tugas kuliahnya tentang Stack (Tumpukan) karena dia akan boring kalau belajar programming….hi…makanya sambil bantu sambil refresh ilmu waktu kuliah dulu.

Stack adalah suatu tumpukan dari benda. Konsep utamanya adalah LIFO (Last In First Out), benda terakhir masuk dalam stack akan menjadi benda pertama yang dikeluarkan dari stack. Di Pascal, ada 2 cara penerapan prinsip stack yakni dengan array dan linked list.

Langsung saja tidak pakai panjang x lebar ini source hasil belajar menggunakan VB.Net…he…sory kalo masih jelek and salah…hi…

Public Class fStack

  Private Const vMax As Integer = 14
  Private stc As stStack

  Structure stStack
    Public top_push, top_pop As Integer
    Public data_push(), data_pop() As String
  End Structure

  Private Sub prPush(ByVal pStr As String)
    stc.data_push(stc.top_push) = pStr
    stc.top_push += 1
  End Sub

  Private Sub prShowStack(ByVal pConPop As Boolean)
    lbPush.Items.Clear()
    For i As Integer = stc.top_push - 1 To 0 Step -1
      lbPush.Items.Add(stc.data_push(i))
    Next
    If pConPop Then
      lbPop.Items.Clear()
      For i As Integer = stc.top_pop - 1 To 0 Step -1
        lbPop.Items.Add(stc.data_pop(i))
      Next
    End If
  End Sub

  Private Function fnPop() As String
    Dim tTmp As String = String.Empty
    If stc.top_push <> 0 Then
      stc.top_push -= 1
      tTmp = stc.data_push(stc.top_push)

      ReDim Preserve stc.data_pop(stc.top_pop)
      stc.data_pop(stc.top_pop) = tTmp
      stc.top_pop += 1
      prShowStack(True)
    Else
      MsgBox("Stack telah Kosong...!", MsgBoxStyle.Exclamation, "Perhatian")
    End If
    Return tTmp
  End Function

  Private Sub btPush_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btPush.Click
    If txInput.Text = String.Empty Then
      MsgBox("Silahkan masukkan data yang akan di Push...!", MsgBoxStyle.Exclamation, "Perhatian")
      Exit Sub
    End If
    Try
      If stc.top_push <> vMax Then
        prPush(txInput.Text)
        prShowStack(False)
      Else
        MsgBox("Stack telah Penuh...!", MsgBoxStyle.Exclamation, "Perhatian")
      End If
    Finally
      txInput.Focus()
    End Try
  End Sub

  Private Sub btPop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btPop.Click
    Try
      fnPop()
    Finally
      txInput.Focus()
    End Try
  End Sub

  Private Sub fStack_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    With stc
      .top_pop = 0
      .top_push = 0
      ReDim .data_push(vMax - 1)
    End With
  End Sub
End Class

O iya di form designnya saya kasih 1 label, 1 textbox, 2 listbox dan 2 button.

Hasil running programnya :

view_stack

Terima Kasih…

Iklan
  1. 6 Desember 2013 pukul 19:32

    kalau utk vb 6.0 gimana gan?
    bantuin donk
    thx

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

w

Connecting to %s

%d blogger menyukai ini: