Advertisement
nein_yards

CircularQueue 1.0

May 19th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. Module Module1
  2. Structure CircularQueue
  3. Dim Rear As Integer
  4. Dim Front As Integer
  5. Dim Elements As String()
  6. Dim MaxLength As Integer
  7. End Structure
  8.  
  9. Function IsEmpty(MyQueue As CircularQueue) As Boolean
  10. If MyQueue.Rear = -1 And MyQueue.Front = -1 Then
  11. Return True
  12. Else : Return False
  13. End If
  14. End Function
  15.  
  16. Function IsFull(MyQueue As CircularQueue) As Boolean
  17. If MyQueue.Front = (MyQueue.Rear + 1) Mod MyQueue.MaxLength Then
  18. Return True
  19. Else : Return False
  20. End If
  21. End Function
  22.  
  23. Function Peek(MyQueue As CircularQueue) As String
  24. If Not IsEmpty(MyQueue) Then
  25. Return MyQueue.Elements(MyQueue.Front Mod MyQueue.MaxLength)
  26. End If
  27. Return ""
  28. End Function
  29.  
  30. Function Dequeue(ByRef MyQueue As CircularQueue) As String
  31. Dim RemovedElement As String = Peek(MyQueue)
  32. If IsEmpty(MyQueue) Then
  33. Console.WriteLine("Stack Underflow")
  34. ElseIf MyQueue.Rear = MyQueue.Front Then
  35. MyQueue.Rear = -1
  36. MyQueue.Front = -1
  37. Else : MyQueue.Front = (MyQueue.Front + 1) Mod MyQueue.MaxLength
  38. End If
  39. Return RemovedElement
  40. End Function
  41.  
  42. Sub Main()
  43. Dim DocumentsToPrint As CircularQueue
  44. InitialiseQueue(DocumentsToPrint, 5)
  45.  
  46. 'manipulate the print queue with any of
  47. 'these functions and procedures
  48.  
  49. Console.ReadLine()
  50. End Sub
  51.  
  52. Sub InitialiseQueue(ByRef MyQueue As CircularQueue, QueueSize As Integer)
  53. MyQueue.Rear = -1
  54. MyQueue.Front = -1
  55. ReDim MyQueue.Elements(QueueSize - 1)
  56. MyQueue.MaxLength = QueueSize
  57. End Sub
  58.  
  59. Sub Enqueue(ByRef MyQueue As CircularQueue, NewElement As String)
  60. If IsFull(MyQueue) Then
  61. Console.WriteLine("Stack Overflow")
  62. Exit Sub
  63. ElseIf IsEmpty(MyQueue) Then
  64. MyQueue.Rear = 0
  65. MyQueue.Front = 0
  66. Else
  67. MyQueue.Rear = (MyQueue.Rear + 1) Mod MyQueue.MaxLength
  68. End If
  69. MyQueue.Elements(MyQueue.Rear) = NewElement
  70. End Sub
  71.  
  72. Sub PrintCQueue(MyQueue As CircularQueue)
  73. If Not IsEmpty(MyQueue) Then
  74. Dim i As Integer = MyQueue.Front
  75. Do
  76. Console.WriteLine(MyQueue.Elements(i))
  77. If i = MyQueue.Rear Then
  78. Exit Sub
  79. End If
  80. i = (i + 1) Mod MyQueue.MaxLength
  81. Loop Until i = MyQueue.Front
  82. End If
  83. End Sub
  84.  
  85. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement