sclgong
asked on
Visual Basic Arrays - Queues.
How do you write a program that adds items to and process them from a queue?
The queue is declared as an array of 10 integer values. To add items to the queue, you need to assign a non-zero value to the array element. To process items, assign a value of zero to the array elements. After adding or processing, display the position in the array of the current end of the queue, and the current processing position.
If the array is full, "queue full" will be displayed as the current position and if there ar no items left to be processed, "queue empty" will be displayed as the current position.
The queue is declared as an array of 10 integer values. To add items to the queue, you need to assign a non-zero value to the array element. To process items, assign a value of zero to the array elements. After adding or processing, display the position in the array of the current end of the queue, and the current processing position.
If the array is full, "queue full" will be displayed as the current position and if there ar no items left to be processed, "queue empty" will be displayed as the current position.
ASKER
The way you do it is different from the way I do it, so I don't really get what you mean.
The way I do it is as follow, but an error occurs evertime I add the item after 10. How can you modify this code to make it right?
(Just a rough copy of the adding code)
Private Sub CmdAdd_Click()
Dim Queue(1 to 10) as integer
Dim intA as integer
Queue(intA) = 1
intA = intA + 1
TxtEndOfQueue.text = intA
If intA > 10 then
TxtEndCurrentPosition.text = "queue full"
End if
End Sub
The way I do it is as follow, but an error occurs evertime I add the item after 10. How can you modify this code to make it right?
(Just a rough copy of the adding code)
Private Sub CmdAdd_Click()
Dim Queue(1 to 10) as integer
Dim intA as integer
Queue(intA) = 1
intA = intA + 1
TxtEndOfQueue.text = intA
If intA > 10 then
TxtEndCurrentPosition.text
End if
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This doesn't implement your requirements exactly in that it doesn't clear the array element to 0. Also, you never need to access the array by index - you should only use Push() and Pop().
I hope this does what you want.
-- George
I hope this does what you want.
-- George
Oh, to test the sample, build a test project and declare a new item of type QArray, set the size, then use Push, Pop. For example:
Dim Q As QArray
Set Q = New QArray
Q.Size = 10
Q.Push 5
Q.Push 76
Q.DumpQ
Debug.Print CStr(Q.Pop)
Q.DumpQ
If you have any questions feel free to email me at endeavor@gte.net. I could mail you the project with the test harness.
-- George
Dim Q As QArray
Set Q = New QArray
Q.Size = 10
Q.Push 5
Q.Push 76
Q.DumpQ
Debug.Print CStr(Q.Pop)
Q.DumpQ
If you have any questions feel free to email me at endeavor@gte.net. I could mail you the project with the test harness.
-- George
Endeavor,
Your class is quite the same as the mine.
What is the big difrerence?
Your class is quite the same as the mine.
What is the big difrerence?
You used a Collection as the internal storage - mine uses an array.
-- George
-- George
ASKER
Endeavor,
It is a good code (I think) but I still don't really understand. You see, I am only a beginner in Visual Basic 5.0, and this is the first time I have ever done arrays, so yours is a little bit too much for me. I'm wondering if you can check and modify my code I have done below, (this one is for stack instead of queue).
Option Explicit
Dim Stack(1 To 10) As Integer
Dim intB As Integer
Private Sub Form_Load()
intB = 1
End Sub
Private Sub CmdAdd_Click()
If intB <= 10 Then
Stack(intB) = 1
TxtTopStack.Text = intB
intB = intB + 1
Else
TxtTopStack.Text = "Stack Full"
End If
End Sub
'This Process part actually has got nothing to do with arrays 'which it should have but don't know how
Private Sub CmdProcess_Click()
If intB > 1 Then
intB = intB - 1
TxtTopStack.Text = intB
Else
TxtTopStack.Text = "Stack Empty"
End If
End Sub
Ta!
It is a good code (I think) but I still don't really understand. You see, I am only a beginner in Visual Basic 5.0, and this is the first time I have ever done arrays, so yours is a little bit too much for me. I'm wondering if you can check and modify my code I have done below, (this one is for stack instead of queue).
Option Explicit
Dim Stack(1 To 10) As Integer
Dim intB As Integer
Private Sub Form_Load()
intB = 1
End Sub
Private Sub CmdAdd_Click()
If intB <= 10 Then
Stack(intB) = 1
TxtTopStack.Text = intB
intB = intB + 1
Else
TxtTopStack.Text = "Stack Full"
End If
End Sub
'This Process part actually has got nothing to do with arrays 'which it should have but don't know how
Private Sub CmdProcess_Click()
If intB > 1 Then
intB = intB - 1
TxtTopStack.Text = intB
Else
TxtTopStack.Text = "Stack Empty"
End If
End Sub
Ta!
I also have a class for stack.
Option Explicit
'
' A FIFO (First In First Out) buffer.
'
Private vData As New Collection
'
' Adds an item to the FIFO
'
Public Sub Push(v As Variant)
If vData.count = 0 Then
vData.Add v
Else
vData.Add v, , , vData.count
End If
End Sub
'
' Clears all items from the FIFO
'
Public Sub Clear()
Set vData = New Collection
End Sub
'
' Returns the number of items in the FIFO
'
Public Property Get count() As Integer
count = vData.count
End Property
'
' Returns an item from the FIFO
'
Public Function Pop() As Variant
If vData.count = 0 Then
Pop = Null
Else
If VarType(vData(1)) = vbObject Then
Set Pop = vData(1)
Else
Pop = vData(1)
End If
vData.Remove 1
End If
End Function
'
' Returns the next item in the FIFO but does
' not remove it from the FIFO
'
Public Function Peek() As Variant
If vData.count = 0 Then
Peek = Null
Else
If VarType(vData(1)) = vbObject Then
Peek = vData(1)
Else
Peek = vData(1)
End If
End If
End Function