krbnldy
asked on
a stack using a linked list
Hi,
I'd like some help in using Visual Basic 6.0 to write some code to implement a stack using a linked list (not using arrays)
Anyone has any ideas of how to do this
Thank you
I'd like some help in using Visual Basic 6.0 to write some code to implement a stack using a linked list (not using arrays)
Anyone has any ideas of how to do this
Thank you
oops, typo:
...the Hewad pointer points ... should read ...the Head pointer points ....
so you will have a variable to hold the 'pointer' to the head of the list.
AW
...the Hewad pointer points ... should read ...the Head pointer points ....
so you will have a variable to hold the 'pointer' to the head of the list.
AW
ASKER
How do I write code for pointers in VB. This is new to me. I am just learning about stacks.
Before we go into detail, is this homework? If so, we can't provide full code, but we will do what we can to help you.
Also, you don't use pointers directly VB really. I think Arthur_Wood meant pointers as in general theory.
You'll need to design a class that represents the "stack" which is basically a collection of "elements" and give it functions to
-Add an Element (push) - this will add the specified element to the stack, and move the stack "pointer" to the top of the stack
-Get an Element (pop) - this will give back the element at the top of the stack, and move the stack "pointer" down one element.
If you imagine it like a tall box, into which you are putting plates and taking them out, it should be easy to visualise in terms of how to "push" and "pop" items.
Let us know if you need more help.
Also, you don't use pointers directly VB really. I think Arthur_Wood meant pointers as in general theory.
You'll need to design a class that represents the "stack" which is basically a collection of "elements" and give it functions to
-Add an Element (push) - this will add the specified element to the stack, and move the stack "pointer" to the top of the stack
-Get an Element (pop) - this will give back the element at the top of the stack, and move the stack "pointer" down one element.
If you imagine it like a tall box, into which you are putting plates and taking them out, it should be easy to visualise in terms of how to "push" and "pop" items.
Let us know if you need more help.
ASKER
Not looking for the code, just some directions as I had no idea how to begin.
Thank you
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is how to do it using a LinkedList implementation that can handle either primitives or objects. Both uses are shown in the demonstration app below:
' -------------------------- ---------- ---------- ----
' Class StackNode
' -------------------------- ---------- ---------- ----
Option Explicit
Public Item As Variant
Public NextNode As StackNode
' -------------------------- ---------- ---------- ----
' Class Stack
' -------------------------- ---------- ---------- ----
Option Explicit
Private HeadNode As StackNode
Private StackCount As Integer
Public Property Get Count() As Integer
Count = StackCount
End Property
Public Sub Push(ByRef Item As Variant)
On Error Resume Next ' handle both objects and primitives
Dim newNode As New StackNode
Set newNode.Item = Item
newNode.Item = Item
Set newNode.NextNode = HeadNode
Set HeadNode = newNode
StackCount = StackCount + 1
End Sub
Public Function Pop() As Variant
If Not (HeadNode Is Nothing) Then
On Error Resume Next ' handle both objects and primitives
Pop = HeadNode.Item
Set Pop = HeadNode.Item
Set HeadNode = HeadNode.NextNode
StackCount = StackCount - 1
Else
MsgBox "Nothing to Pop!", vbCritical, "Empty Stack"
End If
End Function
' -------------------------- ---------- ---------- ----
' Form1
' -------------------------- ---------- ---------- ----
Option Explicit
Private Sub Command1_Click()
' demonstrate the stack working with a primitive (Integer)
Dim i As Integer
Dim s As New Stack
Debug.Print "Pushing:"
For i = 1 To 5
s.Push i
Debug.Print i
Next i
Debug.Print "Popping:"
While s.Count > 0
Debug.Print s.Pop
Wend
End Sub
Private Sub Command2_Click()
' demonstrate the stack working with an object (StackNode)
Dim i As Integer
Dim s As New Stack
Dim sn As StackNode
Debug.Print "Pushing:"
For i = 1 To 5
Set sn = New StackNode
sn.Item = i
s.Push sn
Debug.Print sn.Item
Next i
Debug.Print "Popping:"
While s.Count > 0
Set sn = s.Pop
Debug.Print sn.Item
Wend
End Sub
' --------------------------
' Class StackNode
' --------------------------
Option Explicit
Public Item As Variant
Public NextNode As StackNode
' --------------------------
' Class Stack
' --------------------------
Option Explicit
Private HeadNode As StackNode
Private StackCount As Integer
Public Property Get Count() As Integer
Count = StackCount
End Property
Public Sub Push(ByRef Item As Variant)
On Error Resume Next ' handle both objects and primitives
Dim newNode As New StackNode
Set newNode.Item = Item
newNode.Item = Item
Set newNode.NextNode = HeadNode
Set HeadNode = newNode
StackCount = StackCount + 1
End Sub
Public Function Pop() As Variant
If Not (HeadNode Is Nothing) Then
On Error Resume Next ' handle both objects and primitives
Pop = HeadNode.Item
Set Pop = HeadNode.Item
Set HeadNode = HeadNode.NextNode
StackCount = StackCount - 1
Else
MsgBox "Nothing to Pop!", vbCritical, "Empty Stack"
End If
End Function
' --------------------------
' Form1
' --------------------------
Option Explicit
Private Sub Command1_Click()
' demonstrate the stack working with a primitive (Integer)
Dim i As Integer
Dim s As New Stack
Debug.Print "Pushing:"
For i = 1 To 5
s.Push i
Debug.Print i
Next i
Debug.Print "Popping:"
While s.Count > 0
Debug.Print s.Pop
Wend
End Sub
Private Sub Command2_Click()
' demonstrate the stack working with an object (StackNode)
Dim i As Integer
Dim s As New Stack
Dim sn As StackNode
Debug.Print "Pushing:"
For i = 1 To 5
Set sn = New StackNode
sn.Item = i
s.Push sn
Debug.Print sn.Item
Next i
Debug.Print "Popping:"
While s.Count > 0
Set sn = s.Pop
Debug.Print sn.Item
Wend
End Sub
does that give you enough to start with?
AW