• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 724
  • Last Modified:

Implement Backtracking in Visual basic

how can i implement backtracking in a Visual Basic program.
give answar with example
0
amit_mi2
Asked:
amit_mi2
  • 2
1 Solution
 
abosultanCommented:
what do you mean in backtracking , is it something from linear programming ,or backtracking trace???

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
    Wend
End Sub

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

Private Function stackPop() As Variant
    If stack.Count > 0 Then
        stackPop = stack.Item(1)
        stack.Remove (1)
    Else
        MsgBox "Stack is Empty", vbCritical, "Error"
    End If
End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry, I lost a "." in there somehow...

It should be:

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

~IM
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now