Queue Implementation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amolbehl
    New Member
    • May 2007
    • 63

    Queue Implementation

    Hi.

    Does anyone know how to implement queue in VB6.0 ?

    I just need the push function to push end of queue and pop to remove an item from top of the queue.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by amolbehl
    Does anyone know how to implement queue in VB6.0 ?
    Sure, just write a couple of functions to add and remove things in an array.

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Hi,

      You can also use non-sorted ListBox as a Queue,

      To Add :
      List1.AddItem "ABCD", 0
      'This Adds on Top

      To PopOut Last item :
      str1 = List1.List(List 1.ListCount-1)
      List1.RemoveIte m List1.ListCount-1

      Same thing you can do with array as Killer suggested.
      But ease with ListBox is, you don't have to loop through all the items to Push Down, just AddItem at 0, ensures, other Listitems are pushed down.

      REgards
      Veena

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by QVeen72
        Hi, You can also use non-sorted ListBox as a Queue ...
        You have to use Array or pointer for Queue operation. Controls will not be a better example (of course we can use practically). But logically, in Listbox we can remove items from the middle of list using index value and change value for next index which should not happen in queue (FIFO).

        Comment

        • QVeen72
          Recognized Expert Top Contributor
          • Oct 2006
          • 1445

          #5
          Originally posted by hariharanmca
          In List we can remove items from the middle of list using index value and change value for next index which should not happen in queue (FIFO).
          Hide the ListBox. Don't allow user to control List Items,
          programatically add/remove items from list like a Queue.

          Regards
          Veena

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            While a control such as a ListBox may not match the ideal logical model of a queue, I agree that it's quite a clever and practical (not to mention convenient) way of providing this sort of functionality. An array isn't perfect either, and a control like the ListBox does a lot of the work for you.

            Naturally you would keep the ListBox hidden, so the user cannot play with it.

            In fact, it occurs to me that using a ListBox would have one nice advantage. Any time you do want to see the queue, during development or debugging, all you have to do is make it visible. :)

            Comment

            • deepthi1234
              New Member
              • Oct 2007
              • 2

              #7
              Hi,

              When i try the below piece of code, I get a run-time error 91 saying object variable not set. does the listbox haveto be initialized in some way?

              thanks,
              deepthi

              Originally posted by QVeen72
              Hi,

              You can also use non-sorted ListBox as a Queue,

              To Add :
              List1.AddItem "ABCD", 0
              'This Adds on Top

              To PopOut Last item :
              str1 = List1.List(List 1.ListCount-1)
              List1.RemoveIte m List1.ListCount-1

              Same thing you can do with array as Killer suggested.
              But ease with ListBox is, you don't have to loop through all the items to Push Down, just AddItem at 0, ensures, other Listitems are pushed down.

              REgards
              Veena

              Comment

              • hopalongcassidy
                New Member
                • Oct 2007
                • 2

                #8
                I don't like the idea of a listbox. It requires a form. Who knows if the application even has a form. I suggest a Collection.

                [CODE=vb]Dim stack as New Collection

                Sub push(whatever as Object)
                stack.add(whate ver)
                End Sub

                Function pop()
                if stack.Count = 0 then
                Call MsgBox("Error stack is empty!")
                Set pop = Nothing
                Else
                Set pop = stack.item(stac k.Count)
                stack.Remove(st ack.Count) ' This reduces the Count
                End If
                End Function[/CODE]
                Last edited by Killer42; Oct 22 '07, 03:00 AM.

                Comment

                • deepthi1234
                  New Member
                  • Oct 2007
                  • 2

                  #9
                  thanks! the Collection worked

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by hopalongcassidy
                    I don't like the idea of a listbox. It requires a form. Who knows if the application even has a form. I suggest a Collection.
                    Thanks Hop. :)

                    I agree that the Collection is probably the better method. Especially since it allows you to stack pretty much anything, rather than just strings.

                    However, are you sure that you have to have a form to use a listbox? I thought you could just create one in code. (Note, I'm just asking, not arguing.)

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Oh! Just thought of something.

                      The OP asked for a queue, which would be FIFO. What hopalongcassidy provided was a stack, or LIFO. The difference is not huge, of course, but I think it should be pointed out so people can implement whichever is appropriate.

                      To convert this to a queue I believe that both references to stack.Count in the pop() function should be changed to 1.

                      Comment

                      • oducille
                        New Member
                        • Nov 2013
                        • 1

                        #12
                        This may be a little bit stale, but for people sumbling upon this thread, I have added a few sentences to clarify the proper understanding of the destictions called Stacks and Queues.

                        Please remember that a Stack data structure as mentioned by others is a list where nodes (or items) are added in a last in first out fashion LIFO. This is conceptually similar to an actual stack of coins where the rules only allow you to add coins (Push) or remove (Pop) coins to and from only the top of the stack respectively.

                        On the other hand, the QUEUE data structure allows one to ENQUEUE an item at the beginning of the list where in this case the beginning signifies the location of the most recently added or to be added item. And, the end of the list signifies the oldest item in the list. The oldest items in a Queue are ALWAYS the first to be removed or in this case DEQUEUED. A Queue, therefore, describes a First In First Out or FIFO list.

                        In summary, STACKS are list datastructures where items are PUSHEDed to be added to the beginning of the list. And items are POPed to be removed from the beginning. A STACK can be said to describe a LIFO list. Queues are list datastructures where items are Enqueued at the beginning and Dequeued from the end of the list.

                        Comment

                        Working...