0% found this document useful (0 votes)
10 views2 pages

Queue With Linked List

Uploaded by

Phazha Masaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Queue With Linked List

Uploaded by

Phazha Masaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Imports System

Module Program
Structure LinkedList
Dim Item As String
Dim NextNode As Integer
End Structure
Dim myQueue(9) As LinkedList
Dim FrontPointer As Integer = -1 'Points to front of queue
Dim BackPointer As Integer = -1 'Points to back of queue
Dim FreePointer As Integer = 0 'Points to the next free node
Dim ItemCount As Integer = 0 'Number of items in queue
Function QueueEmpty() As Boolean
If ItemCount = 0 Then
Return True
Else
Return False
End If
End Function
Function QueueFull() As Boolean
If ItemCount = 10 Then
Return True
Else
Return False
End If
End Function
Sub Enqueue(EnqueueItem As String)
If QueueFull() Then
Console.WriteLine("Queue is full")
Else
'Updates previous rear to point to new rear
If QueueEmpty() Then
FrontPointer = FreePointer
Else myQueue(BackPointer).NextNode = FreePointer
End If
'Fill new rear
myQueue(FreePointer).Item = EnqueueItem
myQueue(FreePointer).NextNode = -1
'Update back pointer
BackPointer = FreePointer
'Increment item count
ItemCount = ItemCount + 1
'Update free pointer
If ItemCount = 10 Then 'If queue is now full
FreePointer = -1
ElseIf BackPointer = 9 Then
FreePointer = 0 'To facilitate circular queue
Else FreePointer = FreePointer + 1
End If
End If
End Sub
Function Dequeue() As String
Dim DequeueItem As String = Nothing
'Check if queue is empty
If QueueEmpty() Then
Console.WriteLine("Stack is empty")
Else
DequeueItem = myQueue(FrontPointer).Item 'Retrieval
If FreePointer = -1 Then 'If queue was full
FreePointer = FrontPointer 'New free space
End If
'Update front pointer
FrontPointer = myQueue(FrontPointer).NextNode
'Decrement item count
ItemCount = ItemCount - 1
End If
Return DequeueItem
End Function
Sub DisplayQueue()
Dim CurrentNode As Integer = FrontPointer
'Traversing through the linked list
Do While CurrentNode <> -1
Console.Write(myQueue(CurrentNode).Item & " ")
Console.WriteLine(myQueue(CurrentNode).NextNode)
CurrentNode = myQueue(CurrentNode).NextNode
Loop
End Sub
Sub Main()
Call Enqueue("Test1")
Call Enqueue("Test2")
Call Enqueue("Test3")
Call Dequeue()
Call Enqueue("Test4")
Call DisplayQueue()
End Sub
End Module

You might also like