Advertisement
nein_yards

Circular Queue

Dec 8th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 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. End Structure
  7.  
  8. Function IsEmpty(MyQueue As CircularQueue) As Boolean
  9. If MyQueue.Rear = -1 And MyQueue.Front = -1 Then
  10. Return True
  11. Else : Return False
  12. End If
  13. End Function
  14.  
  15. Function IsFull(MyQueue As CircularQueue) As Boolean
  16. If MyQueue.Front = MyQueue.Rear + 1 Or
  17. MyQueue.Front = 0 And MyQueue.Rear = MyQueue.Elements.Length - 1 Then
  18. Return True
  19. Else : Return False
  20. End If
  21. End Function
  22. Function Peek(MyQueue As CircularQueue) As String
  23. If Not IsEmpty(MyQueue) Then
  24. Return MyQueue.Elements(MyQueue.Front)
  25. End If
  26. Return ""
  27. End Function
  28. Function Dequeue(ByRef MyQueue As CircularQueue) As String
  29. Dim RemovedElement As String = Peek(MyQueue)
  30. If IsEmpty(MyQueue) Then
  31. Console.WriteLine("Stack Underflow")
  32. ElseIf MyQueue.Rear = MyQueue.Front Then
  33. MyQueue.Rear = -1
  34. MyQueue.Front = -1
  35. ElseIf MyQueue.Front = MyQueue.Elements.Length - 1 Then
  36. MyQueue.Front = 0
  37. Else : MyQueue.Front += 1
  38. End If
  39. Return RemovedElement
  40. End Function
  41.  
  42. Sub Main()
  43. Dim DocumentsToPrint As CircularQueue
  44. InitialiseQueue(DocumentsToPrint, 5)
  45. Enqueue(DocumentsToPrint, "Hello")
  46. Console.WriteLine(Peek(DocumentsToPrint))
  47. Console.ReadLine()
  48. End Sub
  49.  
  50. Sub InitialiseQueue(ByRef MyQueue As CircularQueue, QueueSize As Integer)
  51. MyQueue.Rear = -1
  52. MyQueue.Front = -1
  53. ReDim MyQueue.Elements(QueueSize - 1)
  54. End Sub
  55.  
  56. Sub Enqueue(ByRef MyQueue As CircularQueue, NewElement As String)
  57. If IsFull(MyQueue) Then
  58. Console.WriteLine("Stack Overflow")
  59. Exit Sub
  60. ElseIf IsEmpty(MyQueue) Then
  61. MyQueue.Rear = 0
  62. MyQueue.Front = 0
  63. ElseIf MyQueue.Rear = MyQueue.Elements.Length - 1 Then
  64. MyQueue.Rear = 0
  65. Else
  66. MyQueue.Rear += 1
  67. End If
  68. MyQueue.Elements(MyQueue.Rear) = NewElement
  69. End Sub
  70.  
  71. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement