Implement Backtracking in Visual basic

Posted on 2004-11-28
Last Modified: 2013-12-26
how can i implement backtracking in a Visual Basic program.
give answar with example
Question by:amit_mi2
    LVL 1

    Expert Comment

    what do you mean in backtracking , is it something from linear programming ,or backtracking trace???

    LVL 85

    Accepted Solution

    One way is to use a stack.  You simply push your current position onto the stack each time you move.  If you need to backtrack then you simply start popping values off the stack.

    Here is an example of a collection being used as a stack.  Create a project and add two listboxes and a commandbutton.  Run the app and push the button.  The first listbox will show you the order the items went into the box.  The second listbox will show the order of the items as they come out of the stack...which should be in the reverse order.

    Option Explicit

    Private stack As New Collection

    Private Sub Command1_Click()
        Dim i As Integer
        For i = 1 To 10
            stackPush (i)
            List1.AddItem i
        Next i
        While stackCount() > 0
            List2.AddItem stackPop
    End Sub

    Private Sub stackPush(ByVal newValue As Variant)
        If stack.Count > 0 Then
            stack.Add newValue, , 1
            stack.Add newValue
        End If
    End Sub

    Private Function stackPop() As Variant
        If stack.Count > 0 Then
            stackPop = stack.Item(1)
            stack.Remove (1)
            MsgBox "Stack is Empty", vbCritical, "Error"
        End If
    End Function
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Sorry, I lost a "." in there somehow...

    It should be:

        While stack.Count() > 0
            List2.AddItem stackPop


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Adobe Photoshop CC 2015

    Photoshop has become the standard in image manipulation among professional photographers and graphic designers. This course covers exam objectives for the Adobe Certified Associate (ACA) certification.

    What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
    As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now