Visual Basic Queue
In visual basic, Queue is useful to represent a collection of objects which stores elements
in FIFO (First In, First out) style i.e. the element which added first will come out first. In queue,
elements are inserted from one end and removed from another end.
Generally, queues are useful when we want to access elements from the collection in the same
order that is stored and we can store a multiple null and duplicate values in the queue based on
our requirements.
By using Enqueue() and Dequeue() methods, we can add or delete elements from the queue.
Here, the Enqueue() method is useful to add elements at the end of the queue and
the Dequeue() method is useful to remove elements start from the queue.
Following is the pictorial representation of the queue process flow in a visual basic programming
language.
Visual Basic Queue Declaration
Generally, the visual basic will support both generic and non-generic types of queues. Here, we
will learn about non-generic queue collections by using the [Link] namespace.
As discussed, the collection is a class so to define a queue, we must need to declare an instance
of the queue class before we perform any operations like add, delete, etc. like as shown below.
Dim que As Queue = New Queue();
If you observe the above queue declaration, we created a new queue (que) with an instance of
the queue class without specifying any size.
Visual Basic Queue Properties
The following are some of the commonly used properties of a queue in visual basic
programming language.
Property Description
Count It will return the total number of elements in the queue
IsSynchronized It is used to get a value to indicate that access to the queue is synchronized (thread-safe)
or not.
Visual Basic Queue Methods
The following are some of the commonly used methods of the queue to perform operations like
add, delete, etc. on elements of a queue in c# programming language.
Method Description
Enqueue It is used to add elements at the end of the queue.
Dequeue It will remove and returns an item from the starting of queue.
Clear It will remove all the elements from the queue.
Clone It will create a shallow copy of the queue.
Contains It is used determine whether an element exists in queue or not.
Peek It is used to get the first element from the queue.
TrimToSize It is used to set the capacity of the queue to an actual number of elements in the queue.
Visual Basic Queue Example
Here, we are going to use a non-generic type of queue so we can add elements of different data
types to the queue using Enqueue() method. Following is the example of adding and accessing
elements of the queue in visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a queue
Dim que As Queue = New Queue()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link](100)
[Link]("******Queue Example******")
[Link]("Number of Elements in Queue: {0}", [Link])
[Link]("******Queue Elements******")
' Access Queue Elements
For Each item In que
[Link](item)
Next
[Link]()
End Sub
End Module
If you observe the above example, we created a new queue (que) and added different data
type elements to the queue (que) using Enqueue() method and we used a For Each loop to
iterate through a queue to get elements from it.
When we execute the above visual basic program, we will get the result like as shown below.
If you observe the above result, we got elements from the queue in the same order which we
added first.
Visual Basic Dequeue() Method to Access Queue Elements
As discussed, the queue Dequeue() method will always remove and return a first element of the
queue. Following is the example of accessing queue elements using the Dequeue() method in
visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a queue
Dim que As Queue = New Queue()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link](100)
[Link]("Number of Elements in Queue: {0}", [Link])
[Link]("******Queue Elements******")
' Access Queue Elements
While [Link] > 0
[Link]([Link]())
End While
[Link]("Number of Elements in Queue: {0}", [Link])
[Link]()
End Sub
End Module
If you observe the above example, we are accessing queue elements by using Dequeue() method
and calling a Dequeue method on the empty queue will throw an exception (InvalidOperation)
so to avoid that situation we are checking whether the count of queue elements greater than zero
or not using while loop.
When we execute the above visual basic program, we will get the result like as shown below.
If you observe the above result, every time the Dequeue() method has removed and returned the
first element of the queue that’s the reason in the end, we got a count of queue elements as 0.
In case, if you want to remove all the elements of the queue, then you need to use
the Clear() method.
Visual Basic Peek() Method to Access Queue Elements
As discussed, the queue Peek() method will always return the first element of the queue.
Following is the example of accessing queue elements using the Peek() method in visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a queue
Dim que As Queue = New Queue()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link]("Number of Elements in Queue: {0}", [Link])
[Link]("******Queue Elements******")
' Access Queue Elements
[Link]([Link]())
[Link]([Link]())
[Link]([Link]())
[Link]("Number of Elements in Queue: {0}", [Link])
[Link]()
End Sub
End Module
If you observe the above example, we are accessing queue elements by using the Peek() method.
When we execute the above visual basic program, we will get the result as shown below.
Number of Elements in Queue: 4
******Queue Elements******
Welcome
Welcome
Welcome
Number of Elements in Queue: 4
If you observe the above result, every time the Peek() method has returned a first element of the
queue without removing any element that’s the reason at the end also the queue elements count is
same as the starting of the queue.
Visual Basic Queue Contains() Method
In visual basic, by using queue Contains() method we can check whether an element exists in
the queue or not. In case, if element found in the queue, then it will return True otherwise False.
Following is the example of using queue Contains() method to check whether an item exists in
the queue or not in visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a queue
Dim que As Queue = New Queue()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link](100)
[Link]("******Queue Example******")
[Link]("Contains Element 4: {0}", [Link](4))
[Link]("Contains Element 100: {0}", [Link](100))
[Link]("Contains Key 'Hello': {0}", [Link]("Hello"))
[Link]()
End Sub
End Module
If you observe the above example, we used a Contains() method to check for particular elements
exists in the queue (que) or not.
When we execute the above visual basic program, we will get the result as shown below.
Visual Basic Queue Overview
Following are the important points which needs to remember about the queue in visual basic.
• In visual basic, queues are useful to store a collection of objects in FIFO (First In, First
out) style i.e. the element which added first will come out first.
• By using Enqueue() method, we can add elements at the end of queue.
• The Dequeue() method will remove and return the oldest (first) element from the queue.
• The queue Peek() method will always return a first element of the queue and it won’t
delete any element from the queue.
Stack
In visual basic, Stack is useful to represent a collection of objects which store elements
in LIFO (Last in, First out) style i.e. the element which added last will be the first to come
out.
Generally, stacks are useful when we want to access elements from the collection in last-in-
first-out style and we can store multiple nulls and duplicate values in the stack based on
our requirements.
By using Push() and Pop() / Peek() methods, we can add or retrieve elements from the
stack. Here, the Push() method is useful to add elements to the stack and
the Pop() / Peek() method is useful to retrieve elements from the stack.
Following is the pictorial representation of the stack process flow in a visual basic
programming language.
Visual Basic Stack Declaration
Generally, visual basic will support both generic and non-generic types of stacks. Here, we
will learn about non-generic queue collections by using the [Link] namespace
so we can add elements of different data types.
As discussed, the collection is a class so to define a stack, we must need to declare an
instance of the stack class before we perform any operations like add, delete, etc. like as
shown below.
Dim stk As Stack = New Stack();
If you observe the above stack declaration, we created a new stack (stk) with an instance of
stack class without specifying any size.
Visual Basic Stack Properties
The following are some of the commonly used properties of the stack in a visual basic
programming language.
Property Description
Count It will return the total number of elements in the stack.
Property Description
IsSynchronized It is used to get a value to indicate that access to stack is synchronized (thread-safe)
or not.
Visual Basic Stack Methods
Following are some of the commonly used methods of the stack to perform operations like
add, delete, etc. on elements of the stack in a visual basic programming language.
Method Description
Push It is used to insert an object at the top of the stack.
Pop It will remove and return an object at the top of the stack.
Clear It will remove all the elements from the stack.
Clone It will create a shallow copy of stack.
Contains It is used to determine whether an element exists in a stack or not.
Peek It is used to return a top element from the stack.
Visual Basic Stack Example
Here, we are going to use a non-generic collection of the stack so we can add elements of
different data types to the stack using Push() method. Following is the example of adding
and accessing the elements of the stack in visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a stack
Dim stk As Stack = New Stack()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link](Nothing)
[Link](100)
[Link]("Number of Elements in Stack: {0}", [Link])
[Link]("******Stack Elements******")
' Access Stack Elements
For Each item In stk
[Link](item)
Next
[Link]()
End Sub
End Module
If you observe the above example, we created a new stack (stk) and added different data
type elements to the stack (stk) using Push() method and we used For Each loop to iterate
through the stack to get elements from it.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, we got elements from the stack in LIFO (last in, first out)
order.
Visual Basic Stack Pop() Method to Access Elements
As discussed, the stack Pop() method will always remove and return a top element of the
queue. Following is the example of accessing stack elements using the Pop() method in
visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a stack
Dim stk As Stack = New Stack()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link](Nothing)
[Link](100)
[Link]("Number of Elements in Stack: {0}", [Link])
[Link]("******Stack Elements******")
' Access Stack Elements
While [Link] > 0
[Link]([Link]())
End While
[Link]("Number of Elements in Stack: {0}", [Link])
[Link]()
End Sub
End Module
If you observe the above example, we are accessing stack elements by using Pop() method
and calling a Pop method on the empty stack will throw an exception (InvalidOperation) so
to avoid that situation we are checking whether the count of stack elements are greater
than zero or not using while loop.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, every time the Pop() method has removed and returned a
top element of the stack that’s the reason at the end we got a count of stack elements as 0.
In case, if you want to remove all the elements from the stack, then you need to use
the Clear() method.
Visual Basic Stack Peek() Method to Access Elements
As discussed, the stack Peek() method will always return a last (top-most) inserted element
of the stack. Following is the example of accessing stack elements using Peek() method in
visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a stack
Dim stk As Stack = New Stack()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link](Nothing)
[Link](100)
[Link]("Number of Elements in Stack: {0}", [Link])
[Link]("******Stack Elements******")
' Access Stack Elements
[Link]([Link]())
[Link]([Link]())
[Link]([Link]())
[Link]("Number of Elements in Stack: {0}", [Link])
[Link]()
End Sub
End Module
If you observe the above example, we are accessing stack elements by using
the Peek() method.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, every time the Peek() method has returned a last (top-most)
element of the queue without removing any element that’s the reason at the end also the
stack elements count is same as starting of the stack.
Visual Basic Stack Contains() Method
In visual basic, by using stack Contains() method we can check whether an element exists
in the stack or not. In case, if the element found in the stack, it will
return True otherwise False.
Following is the example of using stack Contains() method to check whether an item exists
in the stack or not in visual basic.
Module Module1
Sub Main(ByVal args As String())
' Create and initialize a stack
Dim stk As Stack = New Stack()
[Link]("Welcome")
[Link]("Tutlane")
[Link](20.5F)
[Link](10)
[Link](Nothing)
[Link](100)
[Link]("******Stack Example******")
[Link]("Contains Element 4: {0}", [Link](4))
[Link]("Contains Element 100: {0}", [Link](100))
[Link]("Contains Key 'Hello': {0}", [Link]("Hello"))
[Link]()
End Sub
End Module
If you observe the above example, we used Contains() method to check for particular
elements that exist in the stack (stk) or not.
When we execute the above visual basic program, we will get the result as shown below.
Visual Basic Stack Overview
The following are the important points which needs to remember about the stack in visual
basic.
• In visual basic, the stack is useful to store a collection of objects in a LIFO (Last in,
First out) style i.e. the element which added last will come out first.
• By using the Push() method, we can add elements to the stack.
• The Pop() method will remove and return a top most element from the stack.
• The stack Peek() method will always return a last (top-most) inserted element of the
stack and it won’t delete any element from the stack.